Best way to update 40 million rows in batch

后端 未结 4 973
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 07:56

Basically I need to run this on a table with 40 million rows, updating every row at once will crash, so I want to batch the query so that if it crash, it can re-run the quer

4条回答
  •  心在旅途
    2021-01-05 08:53

    Declare @Rowcount INT = 1;
    
    WHILE (@Rowcount > 0)   
    BEGIN
            UPDATE TOP (100000) [table]   --<-- define Batch Size in TOP Clause
               SET [New_ID] = [Old_ID]
            WHERE [New_ID] <> [Old_ID]
    
            SET @Rowcount = @@ROWCOUNT;
    
           CHECKPOINT;   --<-- to commit the changes with each batch
    END
    

提交回复
热议问题