is it ok to use plinq ForAll for a bulk insert into database?

元气小坏坏 提交于 2019-12-06 03:10:00

问题


I'm doing like this:

 entities.AsParallel().ForAll(o => repository.Insert(o));

is this good, am I going to have more performance with this ?


回答1:


No.

This one can be faster, as it leverages the paralellism to the SQL, but in the end the SQL has to make a lock for the table (page), as it makes an insert. therefore each paralell request is executed after another again.

If you want to make a bulk insert, than make a SP accepting all entries (e.g. a table with SQL 2008.) or do it with Linq2SQL.

that would be the correct design solution.




回答2:


Probably not. Each insert would actually take place on a seperate thread, while bulk insert work well by transferring large amounts of data from a single thread, at a single time.

PS: SqlBulkCopy would work much, much better than a parallel insert. Use that if possible.



来源:https://stackoverflow.com/questions/3290353/is-it-ok-to-use-plinq-forall-for-a-bulk-insert-into-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!