tags:

views:

101

answers:

2

I need to perform a very large sql server insert from a c# application. Somewhere in the range of 20,000 through 50,000 records.

What is the fastest way through SQL server to perform the insert?

There are several options I know of, but I don't know which is the fastest.

   insert into MyTable(column1, column2, ..., column*)
   select 'value','value',...,'value'
   union
   select 'value','value',...,'value'

OR

   insert into MyTable(column1, column2, ..., column*)
   exec('select ''value'',''value'',...,''value'''
        'select ''value'',''value'',...,''value''')

OR

   bulk insert from a data file

OR

   Any better way that you know of :)
+6  A: 

From C#, you can use SqlBulkCopy, but for raw speed overall, I expect you can get faster with bcp and you can really tune SSIS the most. In both those cases, you would effectively be using external executables - bcp or dtexec.

Cade Roux
A sentence in the linked documentation really says it all (and implies it's the fastest *managed* way to do it): 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
Austin Salonen
If you can use bcp directly from a text file, it's definitely faster; if the data is already in memory, then `SqlBulkCopy` goes straight over the pipe so it's very efficient. It actually uses the undocumented `INSERT BULK` (**not** `BULK INSERT`) statement and writes directly to the TDS stream.
Aaronaught
I would recommend bcp - works quite well.
John M
+4  A: 

using SqlBulkCopy class is your best bet if you are going to use C# for bulk copying. I have used it to transfer 15K rows of data and it finishes off in a flash.

HTH

Raja
SqlBulkCopy is probably the best bet.
Yogendra
Agreement here, I use it to regularly load .5 million records and it flys
Simon Wilson