简体   繁体   中英

Purge by deleting all records older than 180 days from the current date in SQL Server

在此输入图像描述

In SQL Server 2008 R2 I have a table (database size is 450 GB) with over a billion rows, which I'd like to purge by deleting all records older than 180 days counting from the current date. Any help here would be appreciated!

I'm using the following query :

DELETE FROM table name 
WHERE column name < '2015-01-01' 

But it is taking too much time. Is there any maintenance plan or any query so that I can delete the data fast?

Yes. When you want to delete records, regularly, that are old, then the right approach is to use partitioning.

This is a large topic. You can start to learn about it through the documentation .

The key idea is that each partition is a separate store of the data. An entire partition can be dropped without logging, incurring very little overhead.

In your case, I would probably suggest a separate partition for each month.

It's taking a long time because (in part) all of those deletes are going into one gargantuan transaction. You need to break it down into smaller chunks (transactions) and periodically commit. It'll still take a long time but the impact on your server will be lessened. See https://stackoverflow.com/a/28324562/1324345 and the blog post it references, http://sqlperformance.com/2013/03/io-subsystem/chunk-deletes

SET NOCOUNT ON;

DECLARE @r INT;

SET @r = 1;

WHILE @r > 0
BEGIN
  BEGIN TRANSACTION;

  DELETE TOP (100000) -- this will change
    table
    WHERE column name < '2015-01-01' ;

  SET @r = @@ROWCOUNT;

  COMMIT TRANSACTION;

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

If your table is partitioned, it can be much easier and faster.

One approach that might save you some time:

  • Start off with taking a backup (you never know)
  • Insert the rows you want to keep into a temporary table (make sure you have enough room on disk for tempdb )
  • TRUNCATE the table to remove all rows quickly (this statement will execute instantly)
  • Insert the rows from the temporary table back into your source table

INSERT INTO #keep SELECT * FROM table_name WHERE column_name>='2015-01-01';
TRUNCATE TABLE table_name;
INSERT INTO table_name SELECT * FROM #keep;
DROP TABLE #keep;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM