Reputation: 31
Insert Query
vs SqlBulkCopy
- which one is best as performance wise for insert records from One DataBase
to Another DataBase
I know SQLBulkcopy
used for large no of records.
If records less than 10 then which one is better.
Please share your views.
Upvotes: 1
Views: 9454
Reputation: 8019
IMO, in your case, using SqlBulkCopy
is way overkill..
You can use a User-defined Type (UDT) as a table-valued parameter (TVP) passed to a stored procedure if you have less than 1,000 rows or so. After that threshold, the performance gain of SqlBulkCopy
begins to outweigh its initial overhead. SqlBulkCopy works best with Heap Tables (tables without clustered indexes).
I found this article helpful for my own solution - https://www.sentryone.com/blog/sqlbulkcopy-vs-table-valued-parameters-bulk-loading-data-into-sql-server.
Upvotes: 1
Reputation: 60987
Here's a link to a site with some benchmarks.
I've used SqlBulkCopy
extensively in the past and it's very efficient. However, you may need to redesign your tables to be able to use it effectively. For example, when you use SqlBulkCopy
you may want to use client side generated identifiers, so you will have to invent some scheme that allows you to generate stable, robust and predictable IDs. This is very different from your typical insert into table with auto generated identity column.
As an alternative to SqlBulkCopy
you can use the method discussed in the link I provided where you use a combination of union all
and insert into
statements, it has excellent performance as well. Altough, as the dataset increases I think SqlBulkCopy
will be the faster option. A hybrid approach is probably warranted here where you switch to SqlBulkCopy
when record count is high.
I recon SqlBulkCopy
will win out for larger data sets but you need to understand that a SqlBulkCopy
operation can be used to forgo some checks. This will of course speed things up even more but also allow you to violate conditions that you have imposed on your database.
Upvotes: 0
Reputation: 6508
As you are asking for less than 10 records,I would suggest you that use simple insert query.
But if you want to use SQLBulkCopy then first you should know when to use it.
BULK INSERT
The BULK INSERT command is the in-process method for bringing data from a text file into SQL Server. Because it runs in process with Sqlservr.exe, it is a very fast way to load data files into SQL Server.
Upvotes: 1