How to efficiently delete rows while NOT using Truncate Table in a 500,000+ rows table

后端 未结 8 1431
死守一世寂寞
死守一世寂寞 2020-11-30 01:30

Let\'s say we have table Sales with 30 columns and 500,000 rows. I would like to delete 400,000 in the table (those where \"toDelete=\'1\'\").

8条回答
  •  无人及你
    2020-11-30 02:03

    My own take on this functionality would be as follows. This way there is no repeated code and you can manage your chunk size.

    DECLARE @DeleteChunk INT = 10000
    DECLARE @rowcount INT = 1
    
    WHILE @rowcount > 0
    BEGIN
    
      DELETE TOP (@DeleteChunk) FROM Sales WITH(ROWLOCK)
    
      SELECT @rowcount = @@RowCount
    END
    

提交回复
热议问题