Why is BCP so fast?

放肆的年华 提交于 2019-12-04 13:03:51

In SQL Server, BCP input is logged very differently than traditional insert statements. How SQL decides to handle things depends on a number of factors and some are things most developers never even consider like what recovery model the database is set to use.

bcp uses the same facility as BULK INSERT and the SqlBulkCopy classes.

More details here http://msdn.microsoft.com/en-us/library/ms188365.aspx

The bottom line is this, these bulk operations log less data than normal operations and have the ability to instruct SQL Server to ignore its traditional checks and balances on the data coming in. All those things together serve to make it faster.

It cheats.

It has intimate knowledge of the internals and is able to map your input data more directly to those internals. It can skip other heavyweight operations (like parsing, optimization, transactions, logging, deferring indexes, isolation). It can make assumptions that apply to every row of data that a normal insert sql statement can not.

Basically, it's able to skip a bulk of the functionality that makes a database a database, and then clean up after itself en masse at the end.

The main difference I know between bcp and a normal insert is that bcp doesn't need to keep a separate transaction log entry for each individual transaction.

The speed is because they use of BCP API of the SQL Server Native Client ODBC driver. According to Microsoft:

http://technet.microsoft.com/en-us/library/aa337544.aspx

The bcp utility (Bcp.exe) is a command-line tool that uses the Bulk Copy Program (BCP) API...

Bulk Copy Functions reference:

http://technet.microsoft.com/en-us/library/ms130922.aspx

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