Best way to update 40 million rows in batch

后端 未结 4 1044
佛祖请我去吃肉
佛祖请我去吃肉 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:30

    Select 1;  -- this will set a rowcount
    WHILE (@@Rowcount > 0)   
    BEGIN
      UPDATE TOP (1000000) [table]   
        SET [New_ID] =  [Old_ID]
      WHERE [New_ID] <> [Old_ID] 
        or ([New_ID] is null and [Old_ID] is not null)
    END
    

    100000 may work better for the top.

    Since NewID and OldID is not null then the is null check is not necessary.

提交回复
热议问题