简体   繁体   English

使用SQL CE / LINQ进行批量操作

[英]Bulk Operations with SQL CE / LINQ

I'm trying to write a program to convert a large amount of data from a legacy SQL Express system to a newer system based on SQL CE. 我正在尝试编写一个程序,以将大量数据从旧版SQL Express系统转换为基于SQL CE的较新系统。 Here's a quick snapshot of what's going on: 以下是发生的情况的快速快照:

  • Most of the tables in the SQL Express install are small (< 10K records) SQL Express安装中的大多数表都很小(<1万条记录)
  • One table is --extremely-- large, and is well over 1 million records 一张桌子非常大,记录超过一百万

For the smaller tables I can use LINQ just fine -- but the large table gives me problems. 对于较小的表,我可以使用LINQ很好-但是较大的表给我带来了问题。 The standard way of: 标准方式:

foreach(var dataRow in ...) 
{ 
    table.InsertOnSubmit(dataRow); 
}

database.SubmitChanges();

Is painfully slow and takes several hours to complete. 非常缓慢,需要几个小时才能完成。 I've even tried doing some simple "bulk" operations to try and eliminate one giant insertion at the end of the loop, ie: 我什至尝试执行一些简单的“批量”操作来尝试消除循环末尾的一个巨大插入,即:

foreach(var dataRow in ...)
{
   if(count == BULK_LIMIT)
   {
      count = 0;
      database.SubmitChanges(); 
   }

   count++;       
   table.InsertOnSubmit(dataRow);
}

// Final submit, to catch the last BULK_LIMIT item block
database.SubmitChanges();

I've tried a variety of bulk sizes, from relatively small values like 1K-5K to larger sizes up to 300K. 我尝试了各种散装尺寸,从相对较小的值(如1K-5K)到较大的尺寸(最大为300K)。

Ultimately I'm stuck and the process takes roughly the same amount of time (several hours) regardless of the bulk size. 最终,我陷入了困境,无论批量大小如何,该过程花费的时间大致相同(几个小时)。

So - does anyone know of a way to crank up the speed? 所以-有人知道提高速度的方法吗? The typical solution would be to use SqlBulkCopy, but that isn't compatible with SQL CE. 典型的解决方案是使用SqlBulkCopy,但这与SQL CE不兼容。

A couple of notes: 一些注意事项:

  • Yes I really do want all the records in SQL CE, and yes I've setup the connection to allow the database to max out at 4 GB. 是的,我确实想要SQL CE中的所有记录,是的,我已经设置了连接以使数据库最大容量为4 GB。
  • Yes I really do need every last of the 1M+ records. 是的,我确实确实需要1M +记录中的每条记录。
  • The stuff in each data row is all primitive, and is a mix of strings and timestamps. 每个数据行中的内容都是原始的,是字符串和时间戳的混合。
  • The size of the legacy SQL Express database is ~400 MB. 旧版SQL Express数据库的大小约为400 MB。

Thanks in advance - all help is appreciated! 在此先感谢-感谢您的帮助!

-- Dan -丹

Use a parameterised INSERT statement: Prepare a command, set the parameter values in a loop and reuse the same command for each INSERT. 使用参数化的INSERT语句:准备命令,在循环中设置参数值,并对每个INSERT重用同一命令。

Remove any indexes and re-apply after you have performed all INSERTs. 执行所有INSERT后,请删除所有索引并重新应用。

Update : Chris Tacke has the fastest solution here using SqlCeResultset: Bulk Insert In SQLCE 更新 :Chris Tacke在这里使用SqlCeResultset具有最快的解决方案: SQLCE中的批量插入

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

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