简体   繁体   English

插入时间过长,需要代码优化

[英]Insert takes too long, code optimization needed

I've some code I use to transfer a table1 values to another table2 , they are sitting in different database. 我有一些代码用于将一个table1值传输到另一个table2 ,它们位于不同的数据库中。

It's slow when I have 100.000 records. 当我有100.000条记录时,速度很慢。 It takes forever to finish, 10+ minutes. 永远需要10余分钟才能完成。

(Windows Mobile smartphone) (Windows Mobile智能手机)

What can I do? 我能做什么?

cmd.CommandText = "insert into " + TableName + " select * from sync2." + TableName+"";  
cmd.ExecuteNonQuery();

EDIT 编辑

The problem is not resolved. 问题没有解决。 I'm still after answers. 我仍在寻找答案。

1] You can set the following parameters in your connectionString 1]您可以在connectionString中设置以下参数

string connectionString = @"Data Source=E:\myDB.db3; New=true; Version=3; PRAGMA cache_size=20000; PRAGMA page_size=32768; PRAGMA synchronous=off";

which has its own limitations. 这有其自身的局限性。 check this link for details 检查此链接以获取详细信息

The above will increase the cache size ultimately(cache_size & page_size) but you could lose some data in case of force shutdown of your system(synchronous=off). 上面的内容最终会增加缓存的大小(cache_size和page_size),但是如果强制关闭系统(synchronous = off),您可能会丢失一些数据。

2] You can wrap your insert statements inside a transaction like this 2]您可以将插入语句包装在这样的事务中

dbTransaction = dbConnection.BeginTransaction();
dbCommand.Transaction = dbTransaction;
// your individual insert statements here
dbTransaction.Commit();

3] There is one more trick which is well explained in this page . 3] 此页面上还有另外一个技巧可以解释。 Check it out 看看这个

Hope that helps 希望能有所帮助
Cheers! 干杯!

As far as I can tell, you are using two SQL statements per row - one to insert it, and then another when you update the entire table. 据我所知,您每行使用两个SQL语句-一个用于插入它,然后在更新整个表时另一个。 Do you need to update the entire table, or just the rows you are inserting? 您需要更新整个表还是仅更新要插入的行? If not, you could just insert the row with dirty set to 0 in the first place. 如果不是这样,您可以只插入dirty设置为0的行。

You can also try changing your ad-hoc insert statement into a prepared/compiled/parametrized statement. 您也可以尝试将临时插入语句更改为准备好的/编译的/参数化的语句。 In some database engines that provides a small speed boost. 在某些数据库引擎中,可以提供较小的速度提升。

There are a few options for improvment listed in the SQLite FAQ, here . SQLite常见问题解答( 此处)中列出了一些改进的选择。

Finally, have you figured out what your bottleneck is? 最后,您知道瓶颈了吗? I don't know much about the performance of mobile phone applications - is your profiling showing that you are CPU bound or "disk" bound, whatever passes for a disk on that particular phone? 我对移动电话应用程序的性能了解不多-您的配置文件是否显示您受CPU约束或“磁盘”约束,无论该​​特定电话上的磁盘经过什么?

我认为您可以使用DataTable (我想-还是DataSet吗?抱歉,仍然是.NET的初学者),然后将Reader的结果.Fill()放入该DataTable ,然后执行BulkCopyBulkInsert操作,可以将其全部推送到另一个数据库(连接)中的表中。

One suggestion, that may help (although you'd need to profile this), would be to call your insert or replace command a single time for multiple records, with multiple values. 可能会有所帮助的一个建议(尽管您需要对此进行概要分析)是对具有多个值的多个记录一次调用您的insert或replace命令。 This would batch the calls to the DB, which potentially would speed things up. 这将批量调用数据库,这可能会加快处理速度。

Also, it sounds like you're copying from one DB to another - if the records are not going to exist in the new DB, you can use INSERT instead of INSERT OR REPLACE, which is (at least theoretically) faster. 同样,这听起来就像您正在从一个数据库复制到另一个数据库一样-如果记录将不存在于新数据库中,则可以使用INSERT而不是INSERT OR REPLACE,这(从理论上讲至少是更快的)。

Neither of these is going to be dramatic, however - this is likely going to still be a slow operation. 但是,这些都不会很引人注目-这很可能仍然是一个缓慢的操作。

Instead of executing the statements individually you could build up the string then execute it at once as a batch, alternatively look into batch updates. 无需单独执行语句,您可以构建字符串,然后一次成批执行一次,或者查看批处理更新。

Alternatively you could do this all as one statement which would probably be more efficient, something like this is what I mean: 或者,您可以将所有操作作为一条语句执行,这可能会更高效,这就是我的意思:

http://forums.mysql.com/read.php?61,15029,15029 http://forums.mysql.com/read.php?61,15029,15029

I hope this helps. 我希望这有帮助。

Well you're using the query processor for each insert. 好吧,您正在为每个插入使用查询处理器。 It's going to be much better to use a command, add Parameters to it, Prepare it, and then just set the Parameters in your loop. 使用命令,向其添加参数,准备它,然后仅在循环中设置参数会更好。

I don't know if SQLite supports TableDirect commands. 我不知道SQLite是否支持TableDirect命令。 If it does that would be way, way, way faster (it's a few orders of magnitude faster in SQL Compact for example). 如果这样做的话,将会更快,更快速(例如,在SQL Compact中快几个数量级)。 It would be certainly worth a try. 当然值得一试。

  1. Does the sql statement need also 10minutes+ if you do this directly in the SQL management studio? 如果直接在SQL Management Studio中执行sql语句,是否还需要10分钟以上?
  2. If not, did you try make it with a SQL procedure and execute it? 如果没有,您是否尝试过使用SQL过程并执行它?
  3. Did you try setup the connection pool and/or make the cursor server sided? 您是否尝试设置连接池和/或使游标服务器成为服务器端?
  4. run the SQL profiler (you can call it from the management studio) if 1. is slow and add transaction queue there. 如果1.运行缓慢,请运行SQL事件探查器(可以从Management Studio调用它),然后在其中添加事务队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM