'Deleting 1 millions rows in SQL Server
I am working on a client's database and there is about 1 million rows that need to be deleted due to a bug in the software. Is there an efficient way to delete them besides:
DELETE FROM table_1 where condition1 = 'value' ?
Solution 1:[1]
BEGIN TRANSACTION
DoAgain:
DELETE TOP (1000)
FROM <YourTable>
IF @@ROWCOUNT > 0
GOTO DoAgain
COMMIT TRANSACTION
Solution 2:[2]
Maybe this solution from Uri Dimant
WHILE 1 = 1
BEGIN
DELETE TOP(2000)
FROM Foo
WHERE <predicate>;
IF @@ROWCOUNT < 2000 BREAK;
END
Solution 3:[3]
Here is something I have used:
If the bad data is mixed in with the good-
INSERT INTO #table SELECT columns FROM old_table WHERE statement to exclude bad rows TRUNCATE old_table INSERT INTO old_table SELECT columns FROM #table
Solution 4:[4]
Not sure how good this would be but what if you do like below (provided table_1 is a stand alone table; I mean no referenced by other table)
create a duplicate table of
table_1liketable_1_dupinsert into table_1_dup select * from table_1 where condition1 <> 'value';drop table table_1sp_rename table_1_dup table_1
Solution 5:[5]
If you cannot afford to get the database out of production while repairing, do it in small batches. See also: How to efficiently delete rows while NOT using Truncate Table in a 500,000+ rows table
If you are in a hurry and need the fastest way possible:
- take the database out of production
- drop all non-clustered indexes and triggers
- delete the records (or if the majority of records is bad, copy+drop+rename the table)
- (if applicable) fix the inconsistencies caused by the fact that you dropped triggers
- re-create the indexes and triggers
- bring the database back in production
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sansalk |
| Solution 2 | Tung Nguyen |
| Solution 3 | |
| Solution 4 | Rahul |
| Solution 5 | Community |
