Best way to update 40 million rows in batch

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

    M.Ali's suggestion will work, but you will end up with degrading performance as you work through the 40M records. I would suggest a better filter to find the records to update in each pass. This would assume you have a primary key (or other index) on your identity column:

    DECLARE @Rowcount INT = 1
        ,   @BatchSize INT = 100000
        ,   @StartingRecord BIGINT = 1;
    
    WHILE (@Rowcount > 0)   
    BEGIN
        UPDATE [table]
            SET [New_ID] = [Old_ID]
        WHERE [table_ID] BETWEEN @StartingRecord AND @StartingRecord + @BatchSize - 1;
    
        SET @Rowcount = @@ROWCOUNT;
    
        CHECKPOINT;
    
        SELECT @StartingRecord += @BatchSize
    END
    

    This approach will allow each iteration to be as fast as the first. And if you don't have a valid index you need to fix that first.

提交回复
热议问题