Deleting 1 millions rows in SQL Server

Peter Sun

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' ?
Dave Cullum

Here is a structure for a batched delete as suggested above. Do not try 1M at once...

The size of the batch and the waitfor delay are obviously quite variable, and would depend on your servers capabilities, as well as your need to mitigate contention. You may need to manually delete some rows, measuring how long they take, and adjust your batch size to something your server can handle. As mentioned above, anything over 5000 can cause locking (which I was not aware of).

This would be best done after hours... but 1M rows is really not a lot for SQL to handle. If you watch your messages in SSMS, it may take a while for the print output to show, but it will after several batches, just be aware it won't update in real-time.

Edit: Added a stop time @MAXRUNTIME & @BSTOPATMAXTIME. If you set @BSTOPATMAXTIME to 1, the script will stop on it's own at the desired time, say 8:00AM. This way you can schedule it nightly to start at say midnight, and it will stop before production at 8AM.

Edit: Answer is pretty popular, so I have added the RAISERROR in lieu of PRINT per comments.

DECLARE @BATCHSIZE INT, @WAITFORVAL VARCHAR(8), @ITERATION INT, @TOTALROWS INT, @MAXRUNTIME VARCHAR(8), @BSTOPATMAXTIME BIT, @MSG VARCHAR(500)
SET DEADLOCK_PRIORITY LOW;
SET @BATCHSIZE = 4000
SET @WAITFORVAL = '00:00:10'
SET @MAXRUNTIME = '08:00:00' -- 8AM
SET @BSTOPATMAXTIME = 1 -- ENFORCE 8AM STOP TIME
SET @ITERATION = 0 -- LEAVE THIS
SET @TOTALROWS = 0 -- LEAVE THIS

WHILE @BATCHSIZE>0
BEGIN
    -- IF @BSTOPATMAXTIME = 1, THEN WE'LL STOP THE WHOLE JOB AT A SET TIME...
    IF CONVERT(VARCHAR(8),GETDATE(),108) >= @MAXRUNTIME AND @BSTOPATMAXTIME=1
    BEGIN
        RETURN
    END

    DELETE TOP(@BATCHSIZE)
    FROM SOMETABLE
    WHERE 1=2

    SET @BATCHSIZE=@@ROWCOUNT
    SET @ITERATION=@ITERATION+1
    SET @TOTALROWS=@TOTALROWS+@BATCHSIZE
    SET @MSG = 'Iteration: ' + CAST(@ITERATION AS VARCHAR) + ' Total deletes:' + CAST(@TOTALROWS AS VARCHAR)
    RAISERROR (@MSG, 0, 1) WITH NOWAIT
    WAITFOR DELAY @WAITFORVAL 
END

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Server : deleting rows by month name

From Dev

How to update large table with millions of rows in SQL Server?

From Java

Deleting rows by batches. How to open / reuse SQL Server connection?

From Dev

Deleting User in SQL Server

From Dev

SQL Server : merge 2 rows into 1 with condition

From Dev

Deleting millions of files

From Dev

SQL Server - Deleting rows between a date range using SQL. Date conversion fails

From Dev

Format Currency into millions of dollars SQL Server

From Dev

Format Currency into millions of dollars SQL Server

From Dev

Updating millions of records in SQL Server 2012

From Dev

Deleting SQL Rows and DataGrid Rows in Visual Basic

From Dev

Deleting pairwise duplicates in SQL Server

From Dev

Sql query for deleting rows before a certain year

From Dev

SQL Stored Procedure Deleting all rows in table

From Dev

Fastest way to insert 1 million rows in SQL Server

From Dev

SQL Server Delete rows when timestamp is older than 1 month

From Dev

Select distinct duplicated rows from 1:n tables in SQL Server

From Java

Python - For loop millions of rows

From Dev

Python - For loop millions of rows

From Dev

Working with millions of rows in Ruby

From Dev

SQL: How to join tables with 1+ millions of records

From Java

SQL Server : Columns to Rows

From Dev

Duplicate rows in SQL Server

From Dev

Aggregate Rows in SQL Server

From Dev

Duplicate rows in SQL Server

From Dev

Formatting rows in SQL Server

From Dev

Transpose rows in SQL Server

From Dev

Deleting and replacing entries in a table using SQL Server

From Dev

Checking for (and Deleting) Complex Object Duplicates in SQL Server

Related Related

HotTag

Archive