c# - SQLite 数据库 (.db) 的性能问题

标签 c# sql database sqlite spatialite

我在使用 SQLite 数据库 (.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 条记录。使用事务更新到数据库中。 so循环遍历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 GHz4 GB RAM500 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="从 RDF_LINK 中选择 LINK_ID 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建议的代码)

最佳答案

目前,事务是按查询运行的,这是没有意义的。

在事务中包含您的主循环代码,并删除此 GC.Collect()。

编辑:

据我了解,您不希望在出现错误时回滚全局更新。所以我稍微更改了代码。

此外,我不确定是否可以通过更改 CommandText 并再次运行查询来重用命令对象。这就是为什么我建议每次都创建它。

using (var transaction = con.BeginTransaction()) 
{ 
    for (int q = 0; q < list.Count; q++) 
    { 
        var castarraylist = (ArrayList)(list[q]); 

        for (int y = 0; y < castarraylist.Count; y++) 
        { 
            using (var cmd = new SQLiteCommand(con)) 
            {
                cmd.Transaction = transaction; 
                cmd.CommandText = Convert.ToString(castarraylist[y]);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch(Exception ex)
                {
                    // Log the update problem
                    Console.WriteLine("Update problem " + cmd.CommandText + " - Reason: " + ex.Message);
                }
            }
        }
    }

    transaction.Commit();
}

关于c# - SQLite 数据库 (.db) 的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24567011/

相关文章:

database - RDBMS 如何处理表更新?

用于按列选择系列中第一个的 SQL 查询

SQL查询其中列值是唯一的并且不包含

c# - 对于 Windows Embedded Standard 7 上的 WinForms 应用程序,如何在 c# 中检测设备是否启用了触摸

c# - 如何使用javascript检查按钮是否被点击?

c# - 有没有办法在wpf包装面板中显示不同宽度的项目

python - 如何在 Flask/Python 中循环表单?

c# - WPF ListBoxItem ControlTemplate 破坏了一些 MouseDown/Selection

sql GROUP 有选择

SQL Server 2008 - 搜索查询