在测试中发现 SqlBulkCopy 在数据量大的时候会出现超时报错。
http://stackoverflow.com/questions/4535536/timeout-expired-with-sqlbulkcopy
Timeout expired with SqlBulkCopy
...
There are two ways to fix this error:
1) Increase Timeout by default it is 30 second
sqlBulkCopy.BulkCopyTimeout = {time in seconds}
2) Decrease BatchSize by default it try to insert all rows in one batch
sqlBulkCopy.BatchSize = {no of rows you want to insert at once}
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.batchsize.aspx
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.bulkcopytimeout.aspx
说得很清楚。BulkCopyTimeout 的缺省值为 30 秒,这样处理数据量大了容易发生超时。碰巧 BatchSize 的缺省值为 0,意思是会把所有操作塞到一轮中处理,这样更加剧了一轮处理所耗费的时间长度。解决办法就是修改上述两个量中的一个即可。比如 batch 尺寸变小了,每轮的时间也就缩短了。
那么每轮处理多少量合适呢?这涉及到性能问题:
http://stackoverflow.com/questions/779690/what-is-the-recommended-batch-size-for-sqlbulkcopy
What is the recommended batch size for SqlBulkCopy?
...
Given this scenario, I found a batch size of 5,000 to be the best compromise of speed and memory consumption. I started with 500 and experimented with larger. I found 5000 to be 2.5x faster, on average, than 500. Inserting the 6 million rows takes about 30 seconds with a batch size of 5,000 and about 80 seconds with batch size of 500.
...
I found that 500 was the optimal value for my application. The 500 value for SqlBulkCopy.BatchSize is also recommended here.