I have a table in SQL Server 2005 which has approx 4 billion rows in it. I need to delete approximately 2 billion of these rows. If I try and do it in a single transaction, th
In addition to putting this in a batch with a statement to truncate the log, you also might want to try these tricks:
For the first point above, for example, if your PK is clustered then find a range which approximately matches the number of rows that you want to delete each batch and use that:
DECLARE @max_id INT, @start_id INT, @end_id INT, @interval INT
SELECT @start_id = MIN(id), @max_id = MAX(id) FROM My_Table
SET @interval = 100000 -- You need to determine the right number here
SET @end_id = @start_id + @interval
WHILE (@start_id <= @max_id)
BEGIN
DELETE FROM My_Table WHERE id BETWEEN @start_id AND @end_id AND
SET @start_id = @end_id + 1
SET @end_id = @end_id + @interval
END