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:
tempdb
) TRUNCATE
the table to remove all rows quickly (this statement will execute instantly) 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.