Hi,
I need to upload a massive (16GB, 65+ million records) CSV file to a single table in a SQL server 2005 database. Does anyone have any pointers on the best way to do this?
Details
I am currently using a C# console application (.NET framework 2.0) to split the import file into files of 50000 records, then process each file. I upload the records into the database from the console application using the SqlBulkCopy class in batches of 5000. To split the files takes approximately 30 minutes, and to upload the entire data set (65+ million records) takes approximately 4.5 hours. The generated file size and the batch upload size are both configuration settings, and I am investigating increasing the value of both to improve performance. To run the application, we use a quad core server with 16GB RAM. This server is also the database server.
Update
Given the answers so far, please note that prior to the import:
- The database table is truncated, and all indexes and constraints are dropped.
- The database is shrunk, and disk space reclaimed.
After the import has completed:
- The indexes are recreated
If you can suggest any different approaches, or ways we can improve the existing import application, I would appreciate it. Thanks.
Related Question
The following question may be of use to others dealing with this problem:
Solution
I have investigated the affect of altering batch size, and the size of the split files, and found that batches of 500 records, and split files of 200,000 records work best for my application. Use of the SqlBulkCopyOptions.TableLock
also helped. See the answer to this question for further details.
I also looked at using a SSIS DTS package, and a BULK INSERT
SQL script. The SSIS package appeared quicker, but did not offer me the ability to record invalid records, etc. The BULK INSERT
SQL script whilst slower than the SSIS package, was considerably faster than the C# application. It did allow me to record errors, etc, and for this reason, I am accepting the BULK INSERT
answer from ConcernedOfTunbridgeWells as the solution. I'm aware that this may not be the best answer for everyone facing this issue, but it answers my immediate problem.
Thanks to everyone who replied.
Regards, MagicAndi