I’m using ADO to save data to an MS Access database. It was taking quite a while to save the data to file (about 7 seconds – which is too long for our purposes). I looked at the number of SQL queries being run, and it’s about 4200; although there isn’t a whole heap of data.
The database connection seems to be the bottleneck. Do you know of any way to decrease the amount of time this takes; either by somehow combining multiple statements into one to reduce overhead, or some ADO/MS-Access trick?
Can you, for instance, insert multiple rows into a table at once, and would this be noticeably faster?
Extra info:
One reason we have so many queries is that we insert a row, and then have another query to retrieve its autoincremented ID; then use this ID to insert several more rows, linking them to the first
In response to several comments and responses: I am leaving the connection open the entire time, and performing it as a single transaction with BeginTransaction() and CommitTransaciton()
Some folk have posted that
@@IDENTITYwould be fast, so here’s a proof (using VBA) of how myINSERT INTOtwo tables at once via aVIEWtrick is about three times faster than doing two INSERTS and grabbing the@@IDENTITYvalues each time… which is hardly surprising because the latter involves threeExecutestatements and the former only involves one 🙂On my machine for the 4200 iterations, the
VIEWtrick took 45 seconds and the@@IDENTITYapproach took 127 seconds:What this shows is the the bottleneck now is the overhead associated with executing multiple statements. What if we could do it in just one statement? Well, guess what, using my contrived example, we can. First, create a Sequence table of unique integers, being a standard SQL trick (every database should have one, IMO):
Then use the Sequence table to enumerate the values from 1 to 42000 and construct rows in a single INSERT INTO..SELECT statement:
That executes on my machine in 0.2 of a second!