【基本优化实践】【1.5】如何在线稳定的删除/更新大量数据?
思路:必须要分批删除/更新,否则事务体量太大,删除时间太久,且为了保证不出问题应当有备份表
最佳实践如下:删除案例,更新相同这里就不特地写了
while 1=1 begin WAITFOR DELAY '00:00:01' DELETE TOP (3000) A output deleted.* into Db_Del..del_sys_users_goods FROM Db_Tank..sys_users_goods A where isexist=0 IF @@ROWCOUNT<3000 OR @@ERROR<>0 OR convert(varchar(12),getdate(),24) > '18:00:00' BREAK end
set rowcount 1000 delete from 表 where set rowcount 0