我正在使用SQLite数据库,并将记录插入它。这需要很长时间!我看到有人说他们可以在一分钟内处理几千人。我有大约2400条记录。每条记录需要30s-2m才能完成。重新创建数据库不是一个选项。我试图以不同的方式创建一个事务。我需要使用计时器,因为我正在使用ProgressBar
向我显示发生了某些事情。这里是我使用的代码:SQLite数据库插入非常缓慢
string con;
con = string.Format(@"Data Source={0}", documentsFolder);
SQLiteConnection sqlconnection = new SQLiteConnection(con);
SQLiteCommand sqlComm = sqlconnection.CreateCommand();
sqlconnection.Open();
SQLiteTransaction transaction = sqlconnection.BeginTransaction();
Timer timer2 = new Timer();
timer2.Interval = 1000;
timer2.Tick += (source, e) =>
{
URL u = firefox.URLs[count2];
string newtitle = u.title;
form.label1.Text = count2 + "/" + pBar.Maximum;
string c_urls = "insert or ignore into " + table + " (id,
url, title, visit_count, typed_count, last_visit_time, hidden) values (" + dbID + ",'" + u.url + "','"
+ newtitle + "',1,1, " + ToChromeTime(u.visited) + ", 0)";
string c_visited = "insert or ignore into " + table2 + " (id,
url,
visit_time, transition) values (" + dbID2 + "," + dbID + "," +
ToChromeTime(u.visited) + ",805306368)";
sqlComm = new SQLiteCommand(c_urls, sqlconnection);
sqlComm.ExecuteNonQuery();
sqlComm = new SQLiteCommand(c_visited, sqlconnection);
sqlComm.ExecuteNonQuery();
dbID++;
dbID2++;
pBar.Value = count2;
if (pBar.Maximum == count2)
{
pBar.Value = 0;
timer.Stop();
transaction.Commit();
sqlComm.Dispose();
sqlconnection.Dispose();
sqlconnection.Close();
}
count2++;
};
timer2.Start();
我在做什么错?
您是否试图直接对数据库运行insert命令(而不是通过C#)?您要插入的表格中有多少项目?它的模式是什么样的(字段+键+索引)?你在这张桌子上有触发器吗? – StriplingWarrior 2012-07-19 22:59:57
好的,我插入到标准的Chrome历史记录数据库中。我插入了大约2400条记录。 – 2012-07-19 23:04:38
@StriplingWarrior问你一些具体的问题(其中4个),你在回复中没有提到的问题。 “一个标准的Chrome历史记录数据库”不会回答“您是否尝试过直接对数据库运行?”,“表中有多少项?” (这与“插入多少项目”不同),或者向那些不熟悉“标准Chrome历史记录数据库”模式但熟悉运行SQLite插入语句的用户提供任何信息。 – 2012-07-19 23:09:35