Saturday, October 18, 2014

DELETE millions of records in batches


Sometimes you need to delete millions of records from table. when you try to delete record all millions of records at same time it will cause log file growing and it will cause excessive locking and eventually log file disk space may get of space. so best way to delete records is in batches. please check below script to delete record in batches. 
If you're using simple recovery model for database then use CHECKPOINT at the end of script
and if you are using full recovery model for database then backup log.

Best way , create SQL Job with below script and execute instead of executing in query window. SQL job will notify you once DELETE job is complete


SET NOCOUNT ON;

DECLARE @r INT;

SET @r = 1;

WHILE @r > 0
BEGIN
  BEGIN TRANSACTION;

  DELETE TOP (100000) -- this will change
    dbo.salesorderdetail
    WHERE salesorderdate < '10/18/2012'

  SET @r = @@ROWCOUNT;

  COMMIT TRANSACTION;

  -- CHECKPOINT;    -- if simple
  -- BACKUP LOG ... -- if full
END



Option B:-  count no. of records against no. of delete record. If no. of records are smaller than no. of records that we are going to delete then I would create Temp new table and move recrods in it and then delete old table/truncate old table and you can either rename temp new table with old table name or you can use INSERT INTO from new temp table to old table.