【发布时间】:2014-08-25 08:42:44
【问题描述】:
我遇到了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
so 这种 100000 条语句在不同的线程中构建并插入到LIST
最后在上面的代码中执行UPDATE语句(现在使用Larry建议的代码)
【问题讨论】:
-
实际执行的 SQL 命令是什么?
-
@CL。实际命令在 castarraylist
-
@Larry .. 我认为内存是这里的问题,所以我使用了 GC.Collect()
-
我没有问它们存储在哪里,而是它们是什么。越来越多的时间表明您在那里有一些非索引查找。显示一些 SQL 示例。
-
现在是不是更好,没有 GC.Collect,包含整个处理的事务?
标签: c# sql database sqlite spatialite