我遇到了性能问题SQLite database (.db)
我正在尝试更新数据库 (.db) 中的 1,00,000 条记录,这大约需要 50 分钟。太慢了。
我的代码如下::
for (int q = 0; q < list.Count; q++)
{
ArrayList castarraylist = new ArrayList();
castarraylist = (ArrayList)(list[q]);
using (var cmd = new SQLiteCommand(con))
using (var transaction = con.BeginTransaction())
{
cmd.Transaction = transaction;
for (int y = 0; y < castarraylist.Count; y++)
{
cmd.CommandText = Convert.ToString(castarraylist[y]);
cmd.ExecuteNonQuery();
}
transaction.Commit();
GC.Collect();
}
}
这里每个 castarraylist 包含 5000 条记录。使用事务更新到数据库中。所以循环遍历20次并完成全部更新。虽然我手动检查时间,但它会在每次迭代中增加 5000 条记录的时间。喜欢
1st 5000 records processing time > 1:11 minute
2nd 5000 records processing time > 1:25 minute
3rd 5000 records processing time > 1:32 minute
4th 5000 records processing time > 1:40 minute
5th 5000 records processing time > 1:47 minute
6th 5000 records processing time > 1:52 minute
...
...
...
17th 5000 records processing time > 3:32 minute
18th 5000 records processing time > 3:44 minute
19th 5000 records processing time > 4:02 minute
20th 5000 records processing time> 4:56 minute
为什么会发生这种情况我无法理解。我用 C# 编写的源代码和我的笔记本电脑配置是i5 2.6 GHz
, 4 GB RAM
, 500 GB HD
.
我建立了如下连接::
SQLiteConnection con = new SQLiteConnection("Data Source=" + fullPath + ";Version=3;Count Changes=off;Journal Mode=off;Pooling=true;Cache Size=10000;Page Size=4096;Synchronous=off");
(*fullpath - 是我的数据库路径)
我正在创建如下表...
sqlquery2="Select LINK_ID from RDF_LINK
string createLinkToPoly = "create table temp2 AS " + sqlquery2;
这将创建一个表并插入由 sqlquery2 通过的记录。
下面的语句扩展了 SQLite 上的 Spatialite
ExecuteStatement("select load_extension('spatialite.dll')", con);
我的Update
陈述如下::
UPDATE temp2 SET GEOM = Transform(LineStringFromText('LINESTRING(4.38368 51.18109,4.38427 51.18165)',4326),32632)WHERE LINK_ID= 53841546
所以这种100000条语句在不同的线程中构建并插入LIST
最后执行UPDATE
上述代码中的语句(现在使用 Larry 建议的代码)