If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > Sticking several thousand INSERTS into one call

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-09-06, 04:55
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Sticking several thousand INSERTS into one call

I may have been a bit daft here. We've got a thing on our intranet where people can upload CSV data files - contact records and stuff - into our SQL Server. Being a bit of a plank, I just wrote a page that'll loop through the CSV file and fire a stored proc to do the insert for each row it finds.

These CSV files can be fairly large - ten or fifteen thousand lines, say. My upload page can take several minutes to process one file.

Now I just wrote it like that because (a) I forgot you can run several SQL statements in one call, and (b) I assumed that, since stored procs are fastest, calling a stored proc several thousand times would be the best way to do it.

This may not be the case. Does anyone know, before I go changing my code? If I loop through a CSV file, adding INSERT statements to one huge SQL string and then fire that, is it likely to be faster than hitting a stored proc several thousand times?
Reply With Quote
  #2 (permalink)  
Old 10-12-06, 04:04
vich vich is offline
Registered User
 
Join Date: Oct 2006
Location: CA
Posts: 194
Interesting question.

I don't know how to do it, but it sure seems a natural for an XML transfer, since SQL Server has native XML support. Maybe post a question on that forum too...
Reply With Quote
  #3 (permalink)  
Old 10-12-06, 09:43
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
SQLServer 2005 has XML support. MSSQL 2000 has little XML support.

Don't concatenate all these records into a single insert. That would be a mess. Either continue to use procedure calls or write code to BCP the file into the database.

You MAY get a slight improvement by loading the data into a staging table with no indexes, foreign keys, or constraints, and moving all the data from the staging table to production at the end of the process. A general rule of thumb is not to load data directly into production tables.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #4 (permalink)  
Old 10-12-06, 14:06
vich vich is offline
Registered User
 
Join Date: Oct 2006
Location: CA
Posts: 194
It's clunky but you could create a disk file on the server (using naming that'll avoid conflict) then use BULK INSERT into a temp table, then subsequently do an INSERT INTO yourtable SELECT col1, col2 FROM temptable.

BULK INSERT will circumvent tons of integrity overhead surrounding each insert, and you avoid the overhead surrounding thousands of network trips.

BULK INSERT also accepts UNC names so you don't have to create the file on your database server, but you'll have to pass your stored procedure the fully qualified name. Most web servers provide directories for temporary files.

Last edited by vich; 10-12-06 at 19:08.
Reply With Quote
  #5 (permalink)  
Old 10-12-06, 18:18
jezemine jezemine is offline
another indirection layer
 
Join Date: May 2004
Location: Seattle
Posts: 1,312
I agree with vich, BULK INSERT is the way to go here.
__________________
elsasoft.org
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On