分批删除大量数据

-- specify the number of rows you want 
--    to delete per 'gulp':
DECLARE @count int
SET @count = 2000

-- keep track of the number of rows
--    impacted by each gulp... once it
--  drops below the intended number of rows
--    then you're done... 
DECLARE @rowcount int
SET @rowcount = @count

-- keep date same through entire operation
DECLARE @cutoff datetime
SET @cutoff = GETDATE() - 60 

WHILE @count = @rowcount BEGIN

    -- archiving logic goes here... 
    -- (or it can take place BEFORE you 
    --    start with this 'nibbling operation')
    
    -- remember, nibbling deletes aren't the 
    --    only thing you can do, you can also do
    --    UPDATEs, INSERTs, MERGEs, etc. 

    DELETE FROM LorryLocations
    WHERE locationId IN (
        SELECT TOP (@count) locationId 
        FROM LorryLocations WITH(NOLOCK)
        WHERE
            [timestamp] < @cutoff
    )
    
    -- update the number of rows modified
    -- if it's less than @count, then this was
    -- our last pass... and the WHILE loop
    -- will break
    SET @rowcount = @@ROWCOUNT
            
    -- a few milliseconds is typically all you'll
    -- need for most operations, but toggle this
    -- value as needed
    WAITFOR DELAY '000:00:00.400'
END

转载于:https://www.cnblogs.com/geass/archive/2011/05/12/2043950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值