在生产应用程序中使用 SqlBulkInsert

发布于 2024-12-25 13:59:59 字数 186 浏览 2 评论 0原文

我们目前正在开发一个应用程序,该应用程序可为每个用户会话在特定表中生成多达 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 技术交流群。

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

发布评论

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

评论(3

娜些时光,永不杰束 2025-01-01 13:59:59

我从未遇到过设置了 tableLock 选项的 SqlBulkCopy 问题以及另一个用户因此被阻止的问题。 TableLock 选项提高了插入的效率,正如许多人所谈论的那样,并且简单地使用它已经向我展示了这一点。

我的典型方法:

public void Bulk(String connectionString, DataTable data, String destinationTable)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlBulkCopy bulkCopy =
            new SqlBulkCopy
            (
            connection,
            SqlBulkCopyOptions.TableLock |
            SqlBulkCopyOptions.FireTriggers |
            SqlBulkCopyOptions.UseInternalTransaction,
            null
            ))
        {
            bulkCopy.BatchSize = data.Rows.Count;
            bulkCopy.DestinationTableName = String.Format("[{0}]", destinationTable);
            connection.Open();
            bulkCopy.WriteToServer(data);
        }
    }
}

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:

public void Bulk(String connectionString, DataTable data, String destinationTable)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlBulkCopy bulkCopy =
            new SqlBulkCopy
            (
            connection,
            SqlBulkCopyOptions.TableLock |
            SqlBulkCopyOptions.FireTriggers |
            SqlBulkCopyOptions.UseInternalTransaction,
            null
            ))
        {
            bulkCopy.BatchSize = data.Rows.Count;
            bulkCopy.DestinationTableName = String.Format("[{0}]", destinationTable);
            connection.Open();
            bulkCopy.WriteToServer(data);
        }
    }
}
莫多说 2025-01-01 13:59:59

在使用 SqlBulkInsert 实现之前,请尝试动态创建 INSERT 查询,如下所示:

insert into MyTable (Column1, Column2)
select 123, 'abc'
union all
select 124, 'def'
union all
select 125, 'yyy'
union all
select 126, 'zzz'

这将只是一次数据库调用,运行速度应该快得多。对于 SQL 字符串连接,请确保使用 StringBuilder 类。

Before implementing using SqlBulkInsert, try creating your INSERT query dynamically to look like this:

insert into MyTable (Column1, Column2)
select 123, 'abc'
union all
select 124, 'def'
union all
select 125, 'yyy'
union all
select 126, 'zzz'

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.

梦归所梦 2025-01-01 13:59:59

我认为如果您的应用程序确实需要在每个会话中生成那么多记录,那么这是正确的方法。

I think it's the right way to go, if your application really needs to produce that many records per session.

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