可以使用 plinq ForAll 批量插入数据库吗?

发布于 2024-09-11 06:48:42 字数 130 浏览 3 评论 0原文

我这样做是这样的:

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

这样好吗?我能用它获得更多的性能吗?

I'm doing like this:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-09-18 06:48:42

不。

这个可以更快,因为它利用了 SQL 的并行性,但最终 SQL 必须在进行插入时对表(页)进行锁定。
因此,每个并行请求都会再次执行。

如果您想进行批量插入,请让 SP 接受所有条目(例如使用 SQL 2008 的表)或使用 Linq2SQL 进行操作。

这将是正确的设计解决方案。

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.

Smile简单爱 2024-09-18 06:48:42

可能不会。每个插入实际上都在单独的线程上进行,而批量插入通过一次从单个线程传输大量数据来很好地工作。

PS:SqlBulkCopy 比并行插入要好得多。如果可能的话使用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文