为什么批量导入比一堆插入更快?
Why Bulk Import is faster than bunch of INSERTs?
我正在写关于将数据从文件导入到 SQL 服务器 table 的方法的毕业论文。我已经创建了自己的程序,现在我将它与一些标准方法进行比较,例如 bcp、BULK INSERT、INSERT ... SELECT * FROM OPENROWSET(BULK...) 等。我的程序从一个源文件,解析它们并使用普通的 INSERT 将它们一个一个地导入。该文件包含 100 万行,每行 4 列。现在我的程序需要 160 秒,而标准方法需要 5-10 秒。
所以问题是为什么 BULK 操作更快?他们使用特殊手段吗?你能解释一下吗,或者给我一些有用的链接之类的?
我想你可以在上面找到很多文章,只需搜索"why bulk insert is faster"。例如,这似乎是一个很好的分析:
通常,任何数据库都需要为单个插入执行大量工作:检查约束、构建索引、刷新到磁盘。这种复杂的操作可以通过数据库在一次操作中进行多个优化,而不是一个一个地调用引擎。
BULK INSERT can be a minimally logged operation (depending on various
parameters like indexes, constraints on the tables, recovery model of
the database etc). Minimally logged operations only log allocations
and deallocations. In case of BULK INSERT, only extent allocations are
logged instead of the actual data being inserted. This will provide
much better performance than INSERT.
实际的好处是减少事务日志中记录的数据量。
在 BULK LOGGED 或 SIMPLE 恢复模型的情况下,优势是显着的。
Optimizing BULK Import Performance
您还应该考虑阅读这个答案:Insert into table select * from table vs bulk insert
顺便说一下,有一些因素会影响 BULK INSERT 的性能:
Whether the table has constraints or triggers, or both.
The recovery model used by the database.
Whether the table into which data is copied is empty.
Whether the table has indexes.
Whether TABLOCK is being specified.
Whether the data is being copied from a single client or copied in
parallel from multiple clients.
Whether the data is to be copied between two computers on which SQL
Server is running.
首先,逐行插入并不是最优的。请参阅 this article on set logic and this article 了解将数据加载到 SQL 服务器的最快方法。
其次,BULK 导入针对大负载进行了优化。这与 SQL 服务器中的页面刷新、写入日志、索引和各种其他事情有关。有一篇关于如何优化 BULK INSERTS 的 technet 文章,这阐明了 BULK 如何更快。但我不能 link 超过两次,所以你必须 google 才能 "Optimizing Bulk Import Performance"。
我正在写关于将数据从文件导入到 SQL 服务器 table 的方法的毕业论文。我已经创建了自己的程序,现在我将它与一些标准方法进行比较,例如 bcp、BULK INSERT、INSERT ... SELECT * FROM OPENROWSET(BULK...) 等。我的程序从一个源文件,解析它们并使用普通的 INSERT 将它们一个一个地导入。该文件包含 100 万行,每行 4 列。现在我的程序需要 160 秒,而标准方法需要 5-10 秒。
所以问题是为什么 BULK 操作更快?他们使用特殊手段吗?你能解释一下吗,或者给我一些有用的链接之类的?
我想你可以在上面找到很多文章,只需搜索"why bulk insert is faster"。例如,这似乎是一个很好的分析:
通常,任何数据库都需要为单个插入执行大量工作:检查约束、构建索引、刷新到磁盘。这种复杂的操作可以通过数据库在一次操作中进行多个优化,而不是一个一个地调用引擎。
BULK INSERT can be a minimally logged operation (depending on various parameters like indexes, constraints on the tables, recovery model of the database etc). Minimally logged operations only log allocations and deallocations. In case of BULK INSERT, only extent allocations are logged instead of the actual data being inserted. This will provide much better performance than INSERT.
实际的好处是减少事务日志中记录的数据量。
在 BULK LOGGED 或 SIMPLE 恢复模型的情况下,优势是显着的。
Optimizing BULK Import Performance
您还应该考虑阅读这个答案:Insert into table select * from table vs bulk insert
顺便说一下,有一些因素会影响 BULK INSERT 的性能:
Whether the table has constraints or triggers, or both.
The recovery model used by the database.
Whether the table into which data is copied is empty.
Whether the table has indexes.
Whether TABLOCK is being specified.
Whether the data is being copied from a single client or copied in parallel from multiple clients.
Whether the data is to be copied between two computers on which SQL Server is running.
首先,逐行插入并不是最优的。请参阅 this article on set logic and this article 了解将数据加载到 SQL 服务器的最快方法。
其次,BULK 导入针对大负载进行了优化。这与 SQL 服务器中的页面刷新、写入日志、索引和各种其他事情有关。有一篇关于如何优化 BULK INSERTS 的 technet 文章,这阐明了 BULK 如何更快。但我不能 link 超过两次,所以你必须 google 才能 "Optimizing Bulk Import Performance"。