|
Hi Whats the best (and fastest) way to insert a large amount of data from table 1 to table 2. Both having identical table structures. I am using SQL Server 2000. Thanks
(comments are locked)
|
|
If table 2 is empty, it should be faster to use another statement:
Notice, this will create the table for you, so it should not exist at the moment of the operation. It works faster because SELECT INTO is minimally logged operation. You should also switch the database to Bulk Logged recovery model for the time of copying. Then you'll be able to switch it back to the original model. Other options to decrease the time are:
(comments are locked)
|
|
Are they both within the same database? If so, you could use This should work in most cases, but it is not the fastest and it probably will fail if the tables are a couple of hundred millions of rows and a row size of 1kb. It will take too long and will consume a lot of transaction log space. I have filled the disks with the log files more than once. :)
Nov 12 '09 at 04:28 AM
Håkan Winther
(comments are locked)
|
|
The fastest way is to drop the indexes and constraints on table 2, insert the data in small chunks (say 10,000 rows at a time), then re-create the indexes on table 2. You'll only see the benefits of this method in the million+ row instances, and it's not something I'd advise to do on anything but a one-time basis. Disable any constraints will also increase the performance.
Nov 11 '09 at 02:58 PM
Håkan Winther
Yep - good shout, i'll edit my answer...
Nov 11 '09 at 03:12 PM
Matt Whitfield ♦♦
Depends whether the insert needs to be transactionally consistent. If so, then Yuriy's answer looks better. If there's a failure partway through you can't rollback - admittedly you can just truncate the table, but still it doesn't satisfy the ACID properties of a transaction.
Nov 12 '09 at 06:08 AM
Mark Allison
Which part of that do you think can't go in a transaction? The only bits of DDL which can't be transacted are listed at http://msdn.microsoft.com/en-us/library/ms191544.aspx...
Nov 12 '09 at 06:24 AM
Matt Whitfield ♦♦
(comments are locked)
|

