Deleting 1 millions rows in SQL Server
Deleting 1 millions rows in SQL Server
I am working on a client's database and there is about 1 million rows that need to be deleted due to a bug in the software. Is there an efficient way to delete them besides:
DELETE FROM table_1 where condition1 = 'value' ?
回答1
Here is a structure for a batched delete as suggested above. Do not try 1M at once...
The size of the batch and the waitfor delay are obviously quite variable, and would depend on your servers capabilities, as well as your need to mitigate contention. You may need to manually delete some rows, measuring how long they take, and adjust your batch size to something your server can handle. As mentioned above, anything over 5000 can cause locking (which I was not aware of).
This would be best done after hours... but 1M rows is really not a lot for SQL to handle. If you watch your messages in SSMS, it may take a while for the print output to show, but it will after several batches, just be aware it won't update in real-time.
Edit: Added a stop time @MAXRUNTIME
& @BSTOPATMAXTIME
. If you set @BSTOPATMAXTIME
to 1, the script will stop on it's own at the desired time, say 8:00AM. This way you can schedule it nightly to start at say midnight, and it will stop before production at 8AM.
Edit: Answer is pretty popular, so I have added the RAISERROR
in lieu of PRINT
per comments.
DECLARE @BATCHSIZE INT, @WAITFORVAL VARCHAR(8), @ITERATION INT, @TOTALROWS INT, @MAXRUNTIME VARCHAR(8), @BSTOPATMAXTIME BIT, @MSG VARCHAR(500) SET DEADLOCK_PRIORITY LOW; SET @BATCHSIZE = 4000 SET @WAITFORVAL = '00:00:10' SET @MAXRUNTIME = '08:00:00' -- 8AM SET @BSTOPATMAXTIME = 1 -- ENFORCE 8AM STOP TIME SET @ITERATION = 0 -- LEAVE THIS SET @TOTALROWS = 0 -- LEAVE THIS WHILE @BATCHSIZE>0 BEGIN -- IF @BSTOPATMAXTIME = 1, THEN WE'LL STOP THE WHOLE JOB AT A SET TIME... IF CONVERT(VARCHAR(8),GETDATE(),108) >= @MAXRUNTIME AND @BSTOPATMAXTIME=1 BEGIN RETURN END DELETE TOP(@BATCHSIZE) FROM SOMETABLE WHERE 1=2 SET @BATCHSIZE=@@ROWCOUNT SET @ITERATION=@ITERATION+1 SET @TOTALROWS=@TOTALROWS+@BATCHSIZE SET @MSG = 'Iteration: ' + CAST(@ITERATION AS VARCHAR) + ' Total deletes:' + CAST(@TOTALROWS AS VARCHAR) RAISERROR (@MSG, 0, 1) WITH NOWAIT WAITFOR DELAY @WAITFORVAL END
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-06-23 HttpContext.Current.ApplicationInstance.Application vs HttpContext.Current.Application
2020-06-23 What Is a Replay Attack?
2020-06-23 ASP.NET's Data Storage Objects
2020-06-23 JSON Web Token (JWT) RFC7519
2020-06-23 Session-State Modes
2015-06-23 一个解决方案下的多个项目共享一个AssemblyInfo
2015-06-23 8 Things Every Person Should Do Before 8 A.M.