Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLBulkCopy or Bulk Insert

I have about 6500 files for a sum of about 17 GB of data, and this is the first time that I've had to move what I would call a large amount of data. The data is on a network drive, but the individual files are relatively small (max 7 MB).

I'm writing a program in C#, and I was wondering if I would notice a significant difference in performance if I used BULK INSERT instead of SQLBulkCopy. The table on the server also has an extra column, so if I use BULK INSERT I'll have to use a format file and then run an UPDATE for each row.

I'm new to forums, so if there was a better way to ask this question feel free to mention that as well.

like image 849
SeanVDH Avatar asked Feb 08 '11 16:02

SeanVDH


People also ask

What is the use of SqlBulkCopy?

The SqlBulkCopy class lets you write managed code solutions that provide similar functionality. There are other ways to load data into a SQL Server table (INSERT statements, for example), but SqlBulkCopy offers a significant performance advantage over them.

What is bulk insert in C#?

Inserting multiple records in a database is the most common and important task in almost all application. There are inbuilt classes in . NET which support bulk insert which helps to insert multiple records in Database.

What is the best and fast way to insert 2 million rows of data into SQL Server?

You can try with SqlBulkCopy class. Lets you efficiently bulk load a SQL Server table with data from another source.

Is C# faster than SQL?

They are both as fast as possible, if you make good code and good queries.


1 Answers

By test, BULK INSERT is much faster. After an hour using SQLBulkCopy, I was maybe a quarter of the way through my data, and I had finished writing the alternative method (and having lunch). By the time I finished writing this post (~3 minutes), BULK INSERT was about a third of the way through.

For anyone who is looking at this as a reference, it is also worth mentioning that the upload is faster without a primary key.

It should be noted that one of the major causes for this could be that the server was a significantly more powerful computer, and that this is not an analysis of the efficiency of the algorithm, however I would still recommend using BULK INSERT, as the average server is probably significantly faster than the average desktop computer.

like image 69
SeanVDH Avatar answered Sep 29 '22 20:09

SeanVDH