Author |
Topic |
raky
Aged Yak Warrior
767 Posts |
Posted - 2010-03-09 : 03:33:43
|
Hi,I need to delete a large number of records ( around 8 Crores ) from a table. What is the best way ( Quicker Way) to do it? |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-03-09 : 03:36:33
|
If you want to delete all the record then use truncate table <tablename>or recreate the tableVabhav T |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-09 : 03:40:21
|
quote: Originally posted by raky Hi,I need to delete a large number of records ( around 8 Crores ) from a table. What is the best way ( Quicker Way) to do it?
If you want to delete all data, use truncate otherwise delete in batchesMadhivananFailing to plan is Planning to fail |
 |
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2010-03-09 : 03:43:27
|
quote: Originally posted by madhivanan
quote: Originally posted by raky Hi,I need to delete a large number of records ( around 8 Crores ) from a table. What is the best way ( Quicker Way) to do it?
If you want to delete all data, use truncate otherwise delete in batchesMadhivananFailing to plan is Planning to fail
Iam not deleting the entire data from table only some part of it. |
 |
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2010-03-09 : 03:48:37
|
quote: Originally posted by madhivanan
quote: Originally posted by raky Hi,I need to delete a large number of records ( around 8 Crores ) from a table. What is the best way ( Quicker Way) to do it?
If you want to delete all data, use truncate otherwise delete in batchesMadhivananFailing to plan is Planning to fail
Is the below query will perform goodwhile 1 = 1begindelete top ( 1000 ) from testwhere keyval is null if @@rowcount = 0 BREAKendPls reply madhi.. |
 |
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-09 : 03:50:25
|
quote: Originally posted by raky
quote: Originally posted by madhivanan
quote: Originally posted by raky Hi,I need to delete a large number of records ( around 8 Crores ) from a table. What is the best way ( Quicker Way) to do it?
If you want to delete all data, use truncate otherwise delete in batchesMadhivananFailing to plan is Planning to fail
Iam not deleting the entire data from table only some part of it.
Could you please refer below threadhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=91179 |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-09 : 03:54:32
|
quote: Originally posted by raky
quote: Originally posted by madhivanan
quote: Originally posted by raky Hi,I need to delete a large number of records ( around 8 Crores ) from a table. What is the best way ( Quicker Way) to do it?
If you want to delete all data, use truncate otherwise delete in batchesMadhivananFailing to plan is Planning to fail
Is the below query will perform goodwhile 1 = 1begindelete top ( 1000 ) from testwhere keyval is null if @@rowcount = 0 BREAKendPls reply madhi..
Yes. Increase it to 10000 and runMadhivananFailing to plan is Planning to fail |
 |
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2010-03-09 : 03:55:43
|
quote: Originally posted by haroon2k9
quote: Originally posted by raky
quote: Originally posted by madhivanan
quote: Originally posted by raky Hi,I need to delete a large number of records ( around 8 Crores ) from a table. What is the best way ( Quicker Way) to do it?
If you want to delete all data, use truncate otherwise delete in batchesMadhivananFailing to plan is Planning to fail
Iam not deleting the entire data from table only some part of it.
As per my concern..(Though i am a beginner of sql)If you are using sql server 2005 and above..please try this appraoch of TOP Operator..DELETE TOP (200) tbl WHERE ...
iam also trying with top operator but it ran for around 1 hour( No record deleted) so i stopped. |
 |
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-09 : 04:01:30
|
quote: Originally posted by raky
quote: Originally posted by haroon2k9
quote: Originally posted by raky
quote: Originally posted by madhivanan
quote: Originally posted by raky Hi,I need to delete a large number of records ( around 8 Crores ) from a table. What is the best way ( Quicker Way) to do it?
If you want to delete all data, use truncate otherwise delete in batchesMadhivananFailing to plan is Planning to fail
Iam not deleting the entire data from table only some part of it.
As per my concern..(Though i am a beginner of sql)If you are using sql server 2005 and above..please try this appraoch of TOP Operator..DELETE TOP (200) tbl WHERE ...
iam also trying with top operator but it ran for around 1 hour( No record deleted) so i stopped.
see thred below.HTHhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=77084http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=83525 |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-03-09 : 04:10:14
|
Note that if the number of rows to be deleted is MOST of the table then this would be better:Copy rows-to-keep to new, temporary tableDrop original tableRename temporary table to original table's nameI don't think that DELETE TOP nnnn is a good choice. The SELECT has to be performed numerous times and, assuming a non-trial WHERE clause, the query will most likely take a measurable amount of time to run, and as more and more data is deleted the "shape" of the table will change - which may trigger statistics to be rebuilt multiple times and so on.My preference is to get the PKs (actually the keys for the Clustered Index - usually the PK) for the rows-to-be-deleted into a Temp table, and then JOIN that to the main table (in batches) for deletion:DECLARE @intRowCount int, @intRowNo int, @intBatchSizeSELECT @intBatchSize = 10000 -- Set initial batch sizeDECLARE @TempTable TABLE( MyID INT IDENTITY(1,1) NOT NULL, MyPK INT NOT NULL, PRIMARY KEY ( MyID, MyPK ))RAISERROR (N'Building @TempTable', 10, 1) WITH NOWAITINSERT INTO @TempTable(MyPK)SELECT MyPKFROM MyTableWHERE MyKeyValue IS NULL -- Full set of criteria identifying the rows-to-be-delete hereORDER BY MyPKSELECT @intRowCount = @@ROWCOUNTRAISERROR (N'Rows to delete: %d', 10, 1, @intRowCount) WITH NOWAITSELECT @intRowNo = 1 -- Set Initial start pointWHILE @intRowCount > 0BEGIN RAISERROR (N'Loop %d', 10, 1, @intRowNo) WITH NOWAIT DELETE D FROM MyTable AS D JOIN @TempTable AS T ON T.MyPK = D.MyPK AND T.MyID >= @intRowNo AND T.MyID <= @intRowNo + @intBatchSize WHERE MyKeyValue IS NULL -- Repeat the full set of criteria here IF the data could have changed SELECT @intRowCount = @@ROWCOUNT -- Optionally adjust @intBatchSize Up/Down if the batch is runner faster/slower than allowed SELECT @intRowNo = @intRowNo + @intBatchSize WAITFOR DELAY '000:00:05' -- Optional loop delay to allow other processes to continue running -- Backup TLog every N iterations (to prevent it growing exceptionally large)ENDRAISERROR (N'Completed', 10, 1) WITH NOWAIT |
 |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-03-09 : 07:06:02
|
quote: Originally posted by Kristen Note that if the number of rows to be deleted is MOST of the table then this would be better:Copy rows-to-keep to new, temporary tableDrop original tableRename temporary table to original table's nameI don't think that DELETE TOP nnnn is a good choice. The SELECT has to be performed numerous times and, assuming a non-trial WHERE clause, the query will most likely take a measurable amount of time to run, and as more and more data is deleted the "shape" of the table will change - which may trigger statistics to be rebuilt multiple times and so on.My preference is to get the PKs (actually the keys for the Clustered Index - usually the PK) for the rows-to-be-deleted into a Temp table, and then JOIN that to the main table (in batches) for deletion:DECLARE @intRowCount int, @intRowNo int, @intBatchSizeSELECT @intBatchSize = 10000 -- Set initial batch sizeDECLARE @TempTable TABLE( MyID INT IDENTITY(1,1) NOT NULL, MyPK INT NOT NULL, PRIMARY KEY ( MyID, MyPK ))RAISERROR (N'Building @TempTable', 10, 1) WITH NOWAITINSERT INTO @TempTable(MyPK)SELECT MyPKFROM MyTableWHERE MyKeyValue IS NULL -- Full set of criteria identifying the rows-to-be-delete hereORDER BY MyPKSELECT @intRowCount = @@ROWCOUNTRAISERROR (N'Rows to delete: %d', 10, 1, @intRowCount) WITH NOWAITSELECT @intRowNo = 1 -- Set Initial start pointWHILE @intRowCount > 0BEGIN RAISERROR (N'Loop %d', 10, 1, @intRowNo) WITH NOWAIT DELETE D FROM MyTable AS D JOIN @TempTable AS T ON T.MyPK = D.MyPK AND T.MyID >= @intRowNo AND T.MyID <= @intRowNo + @intBatchSize WHERE MyKeyValue IS NULL -- Repeat the full set of criteria here IF the data could have changed SELECT @intRowCount = @@ROWCOUNT -- Optionally adjust @intBatchSize Up/Down if the batch is runner faster/slower than allowed SELECT @intRowNo = @intRowNo + @intBatchSize WAITFOR DELAY '000:00:05' -- Optional loop delay to allow other processes to continue running -- Backup TLog every N iterations (to prevent it growing exceptionally large)ENDRAISERROR (N'Completed', 10, 1) WITH NOWAIT
If most of the data you want to delete and only few data you want to keep then you can go by red quoted.Vabhav T |
 |
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2010-03-09 : 23:32:33
|
Thanx to all for your replies..I Used Kristen solution and it worked fine. |
 |
|
|