简体   繁体   English

将大量记录插入数据库

[英]Inserting massive number of records into database

I need to run some algorithms over data massive number of times and store each result in database. 我需要在数据上大量运行一些算法并将每个结果存储在数据库中。

Number of algorithm runs is 80,000-90,000, each cycle takes about 2 seconds (just the algorithm ). 算法运行的数量是80,000-90,000,每个周期大约需要2秒(只是算法)。 So it's very time consuming. 所以这非常耗时。

My database SQL server 2008. I want to use ado.net entity framework (is it good for this task it's not good?) 我的数据库SQL server 2008.我想使用ado.net实体框架(它对这项任务有好处吗?)
Right now the output data (that needs to be stored in DB) is plain raw (not very big), plus some maintain columns like date and time. 现在输出数据(需要存储在DB中)是纯粹的原始(不是很大),还有一些维护列,如日期和时间。

What is the best practice for that? 最佳做法是什么?
Insert row by row, as each algorithms completes? 每个算法完成后,逐行插入? store the results in memory and after work is finished insert the data? 将结果存储在内存中,工作完成后插入数据?

Could you not try BulkInsert after running your algorithm against all records first? 首先针对所有记录运行算法后,你能不能尝试BulkInsert? It is very efficient at getting the data into the database. 将数据导入数据库非常有效。

http://msdn.microsoft.com/en-us/library/ms188365.aspx http://msdn.microsoft.com/en-us/library/ms188365.aspx

You could use SqlBulkCopy class and use a DataTable as source data. 您可以使用SqlBulkCopy类并使用DataTable作为源数据。 It's realy fast compared with multiple INSERTs. 与多个INSERT相比,它真的很快。

In case you didn't use sqlbulkcopy, you could do next: 如果你没有使用sqlbulkcopy,你可以做下一步:

  1. Store data to local variables 将数据存储到本地变量
  2. After all data is collected, begin SQL transaction and insert each row to db. 收集所有数据后,开始SQL事务并将每行插入db。 After all insert querys are executed, commit. 执行完所有插入查询后,提交。

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

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