清理大批量数据例子
最近有一个新任务,在一个表里一下更新700W条数据。由于这个表业务正在使用,而且SQL Server做了完整日志记录。
修改了一下更新sql,分批更新,每次60W。
DECLARE @num INT
SET @num=0
WHILE @num<7000000
BEGIN
UPDATE t SET UseCustomerSysNo=c.sysno,ValidTimeFrom='2011-09-07',rowmodifydate=GETDATE(),ValidTimeTo='2011-09-12 23:59:59.000'
FROM (SELECT ValidTimeTo,rowmodifydate, ValidTimeFrom, sysno,UseCustomerSysNo,ROW_NUMBER() over(ORDER BY sysno) rn
FROM dbo.Coupon WHERE BatchNo=623) t
INNER JOIN (SELECT sysno,ROW_NUMBER() over(ORDER BY sysno) rn FROM customer WHERE RegisterTime<'2011-9-6') c
ON c.rn=t.rn AND t.rn>=@num AND t.rn<@num+600000
SET @num=@num+600000
END
SET @num=0
WHILE @num<7000000
BEGIN
UPDATE t SET UseCustomerSysNo=c.sysno,ValidTimeFrom='2011-09-07',rowmodifydate=GETDATE(),ValidTimeTo='2011-09-12 23:59:59.000'
FROM (SELECT ValidTimeTo,rowmodifydate, ValidTimeFrom, sysno,UseCustomerSysNo,ROW_NUMBER() over(ORDER BY sysno) rn
FROM dbo.Coupon WHERE BatchNo=623) t
INNER JOIN (SELECT sysno,ROW_NUMBER() over(ORDER BY sysno) rn FROM customer WHERE RegisterTime<'2011-9-6') c
ON c.rn=t.rn AND t.rn>=@num AND t.rn<@num+600000
SET @num=@num+600000
END
更新过程中,的确存在堵塞前台更新表的情况,但由于这个时间比较快,堵塞也就不多!
删除历史数据:
WHILE 1=1
BEGIN
DELETE TOP(500000) FROM
Product_DailyClick where clickdate<'2011-01-01'
IF @@ROWCOUNT<500000
BREAK;
END
BEGIN
DELETE TOP(500000) FROM
Product_DailyClick where clickdate<'2011-01-01'
IF @@ROWCOUNT<500000
BREAK;
END