在生产应用程序中使用 SqlBulkInsert
我们目前正在开发一个应用程序,该应用程序可为每个用户会话在特定表中生成多达 5-10,000 行数据。目前,我们使用 sql 文本命令一次插入每行数据,因此保存操作可能需要长达一分钟的时间。我们正在尝试使用 SqlBulkInserts,发现时间已降至不到 500 毫秒。是否有人反对在许多用户将使用该系统的生产应用程序中使用 SqlBulkInserts?
We are currently developing an application that generates upwards of 5-10,000 rows of data in a particular table for each user session. Currently we are using sql text commands to insert each row of data at a time so a save operation could take up to a minute. We are playing around with the use of SqlBulkInserts and have seen the time go down to less than 500ms. Does anyone have any objection with the use of SqlBulkInserts in a production application where many users will be using the system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我从未遇到过设置了 tableLock 选项的 SqlBulkCopy 问题以及另一个用户因此被阻止的问题。 TableLock 选项提高了插入的效率,正如许多人所谈论的那样,并且简单地使用它已经向我展示了这一点。
我的典型方法:
I have never ran into an issue with SqlBulkCopy with the tableLock option set and another user being blocked due to it. The TableLock option increases the efficiency of the insert from what many people have talked about and just plain using it have shown me.
My typical method:
在使用 SqlBulkInsert 实现之前,请尝试动态创建
INSERT
查询,如下所示:这将只是一次数据库调用,运行速度应该快得多。对于 SQL 字符串连接,请确保使用
StringBuilder
类。Before implementing using SqlBulkInsert, try creating your
INSERT
query dynamically to look like this:This will be only one database call, which should run much more quickly. For the SQL string concatenation, make sure you use the
StringBuilder
class.我认为如果您的应用程序确实需要在每个会话中生成那么多记录,那么这是正确的方法。
I think it's the right way to go, if your application really needs to produce that many records per session.