SQL Server SQL Server 分批删除大量数据

xinggang · 2019年12月12日 · 138 次阅读

当删除一个数据量在千万及以上的大表中的部分数据时,使用 delete from 表名 where 条件删除记录时执行的时间会比较长,日志文件急速增长,通过以下分批删除的方式可以稍微好一点。

--分批删除大量数据
declare @count int
set @count=100000 --每批删除的记录数

print convert(varchar(50),getdate(),120)
while 1=1
begin
     delete top(@count) from KHData_History where DataTime >'2019/12/08';
     if (@@rowcount<@count) break;

     print convert(varchar(50),getdate(),120)
     WaitFor DELAY '00:00:02' --等待几秒再继续
end
print '删除完成'

说明:@count 为每次删除的数据量,此处设置10w,可根据实际情况调整。

参考:https://weiku.co/article/484/

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册