用SQL快速更新

时间:2016-03-18 12:33:53

标签: sql sql-server

我有以下查询,需要几天才能执行

DECLARE @cnt BIGINT
SET @cnt = 1

WHILE @cnt * 1000 < 92746339
BEGIN

    UPDATE TOP (1000) [dbo].[Vente]
    SET Promo =
        CASE
            WHEN [Code Article] IN (
                    SELECT [Code Article]
                    FROM [Promotion]
                    WHERE datecol BETWEEN [Date Debut Promo] AND [Date Fin Promo]
                )
                THEN 1
            ELSE 0
        END

    WHERE promo IS NULL
    PRINT '@cnt: ' + CONVERT(VARCHAR, @cnt)
    SET @cnt = @cnt + 1
END

行数:Vente = 92百万,促销= 419187 执行计划:

enter image description here

如何加快速度?

1 个答案:

答案 0 :(得分:0)

为什么不使用JOIN代替IN条款?

UPDATE v
    SET v.Promo = CASE 
                     WHEN p.[Code Article] IS NOT NULL THEN 1
                     ELSE 0
                  END
FROM [dbo].[Vente] v
LEFT JOIN [Promotion] p
    ON v.[Code Article] = p.[Code Article]
       AND (datecol between [Date Debut Promo] AND [Date Fin Promo])
WHERE v.Promo IS NULL;

表格中有多少行?