views:

241

answers:

2

I am about to run a massive data insert into my DB. I have managed to work out how to enable and rebuild non-clustered indexes on my tables but I also want to disable/enable primary keys as I believe this will speed up the insertion process.

NOTE: This is over numerous tables and so I assume I need some loop to get the primary key information and run the following to drop it but I'm not sure about recreating it:

ALTER TABLE Table1
DROP CONSTRAINT PK_Table1_Col1
+5  A: 

IIRC clustered indexes cannot be disabled as they govern where the actual data is stored in the pages.

I'm pretty sure you would have to drop the key and re-create it after your insert. Depending on the size of the tables, indexes and insert this may not save you any time.

Barry
I was under the impression that disabling indexes before huge inserts and reenabling them afterwards would be a better process
Jon
Yes for nonclustered indexes I believe it is. But by dropping and recreating a clustered index it may not be so beneficial. recreating the clustered index will then reorganise all of the data at page level?
Barry
With a clustered index the data is actually stored inside of the index, so dropping the index is actually deleting the table.Thus dropping a clustered index means creating a new table without the index and copying all the data to this new table, then deleting the original and renaming the new. Quite a process.
Cobusve
A: 

Run This : //For disable all constraint of your all tables exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' //Your insert query here ...................... //After Insert Enable all the constraint exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'

Manish Kumar