【问题标题】:Update in Batches Never Finishes批量更新永远不会完成
【发布时间】:2020-07-04 10:28:35
【问题描述】:

作为对我在此处发布的原始问题的跟进

UPDATE in Batches Does Not End and Remaining Data Does Not Get Updated

如果您使用下面的逻辑,您会发现更新永远不会完成。如果您有任何想法,请告诉我为什么...

表 1

IF OBJECT_ID('tempdb..#Table2') IS NOT NULL
        BEGIN 
            DROP TABLE #Table2;
        END

        CREATE TABLE #Table2 (ID INT);

        DECLARE @Count int = 0;

        WHILE (select count(*) from #Table2) < 10000 BEGIN
            INSERT INTO #Table2 (ID) 
            VALUES (@Count)

            -- Make sure we have a unique id for the test, else we can't identify 10 records
            set @Count = @Count + 1;
        END

表 2

IF OBJECT_ID('tempdb..#Table1') IS NOT NULL
    BEGIN 
        DROP TABLE #Table1;
    END

    CREATE TABLE #Table1 (ID INT);

    DECLARE @Count int = 0;

    WHILE (select count(*) from #Table1) < 5000 BEGIN
        INSERT INTO #Table1 (ID) 
        VALUES (@Count)

        -- Make sure we have a unique id for the test, else we can't identify 10 records
        set @Count = @Count + 1;
    END


     /****************** UPDATE ********************/

    select count (*) from #Table2 t2 where Exists (select * from #Table1 t1 where t1.ID = t2.ID)
    select count (*) from #Table2 where ID = 0
    select count (*) from #Table1 where ID = 0
       -- While exists an 'un-updated' record continue
    WHILE exists (select 1 from #Table2 t2 where Exists (select * from #Table1 t1 where t1.ID = t2.ID) ) 
    BEGIN

        -- Update any top 10 'un-updated' records
        UPDATE t2
        SET ID = 0
        FROM #Table2 t2
        WHERE ID IN (select top 10 id from #Table2 where Exists (select * from #Table1 t1 where t1.ID = t2.ID) )
    END

【问题讨论】:

    标签: sql-server loops while-loop sql-update batch-updates


    【解决方案1】:

    您的UPDATE 语句引用了#Table2 上的错误实例。您需要以下内容:

    UPDATE t2 SET
        ID = 0
    FROM #Table2 t2
    WHERE ID IN (
        SELECT TOP 10 ID
        -- note this alias is t2a, and is what the `exists` needs to reference
        -- not the table being updated (`t2`)
        FROM #Table2 t2a
        WHERE EXISTS (SELECT 1 FROM #Table1 t1 WHERE t1.ID = t2a.ID)
    )
    

    注意:对于测试,请确保 @Count 从 1 而不是 0 开始,否则您仍然会以无限循环结束。

    【讨论】:

    • 这不起作用。我已经修复了别名。我也试过你的代码。仍然 UPDATE 永远不会结束。加上最重要的是,原始问题仍然存在(无论已经完成了多少次更新迭代,只有 10 条记录得到更新)
    • 还有一个错误我忘了提及,将DECLARE @Count int = 0; 更改为DECLARE @Count int = 1; - 从零开始,当您更新到零时当然会失败。但我再次假设这不是现实生活中的场景。
    • @enigma6205 有什么快乐吗?
    • 不确定@Count 会如何影响更新语句,如果它甚至没有在其中使用...如果你运行它,你会看到我有 5000 条记录要更新。 select count (*) from #Table2 t2 where Exists (select * from #Table1 t1 where t1.ID = t2.ID)
    • @enigma6205 因为您使用它来生成测试 ID,从零开始,然后将它们设置为零,这意味着此测试 select 1 from #Table1 t1 where t1.ID = t2.ID 将始终通过,因为您从#table1 中的零,无论有任何更新,它都将始终匹配。从一个开始,它可以解决问题。正如我在现实生活中所说,我怀疑你的 id 是否为零,如果你这样做,你需要一个不同的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多