0

我有一个表:VIEWING_17to31_aug_server,它有 393085432 行。

我试图通过连接条件更新字段(连接条件是正确的)。但查询无法执行,因为它超出了查询临时空间。

然后我尝试使用没有中断但已经运行了很长时间的while循环。

CREATE VARIABLE @var_period_start               datetime;
CREATE VARIABLE @var_period_end                 datetime;
CREATE VARIABLE @scanning_day                   datetime;

SET @var_period_start  = '2013-08-17';
SET @var_period_end    = '2013-08-31';
SET @scanning_day = @var_period_start;

while @scanning_day <= dateadd(dd,0,@var_period_end)
begin
    UPDATE VIEWING_17to31_aug_server
    SET A.calculated_scaling_weight = B.calculated_scaling_weight
    FROM VIEWING_17to31_aug_server AS A
    LEFT JOIN sk_prod.viq_viewing_data_scaling AS B
    ON A.ACCOUNT_NUMBER = B.ACCOUNT_NUMBER
    AND A.VIEWING_DAY = B.ADJUSTED_EVENT_START_DATE_VESPA
    AND A.VIEWING_DAY = @SCANNING_DAY
End

有没有其他方法可以让我更快地完成它?动态执行会有帮助吗?

4

1 回答 1

1

我认为您需要在循环中增加 @scanning_day ,此时while条件将永远不会满足,因为@scanning_day保持不变为@var_period_start. 例如

SET @scanning_day = dateadd(dd, 1, @scanning_day);

因此,您的完整脚本将变为:

CREATE VARIABLE @var_period_start               datetime;
CREATE VARIABLE @var_period_end                 datetime;
CREATE VARIABLE @scanning_day                   datetime;

SET @var_period_start  = '2013-08-17';
SET @var_period_end    = '2013-08-31';
SET @scanning_day = @var_period_start;

while @scanning_day <= dateadd(dd,0,@var_period_end)
begin
    UPDATE VIEWING_17to31_aug_server
    SET A.calculated_scaling_weight = B.calculated_scaling_weight
    FROM VIEWING_17to31_aug_server AS A
    LEFT JOIN sk_prod.viq_viewing_data_scaling AS B
    ON A.ACCOUNT_NUMBER = B.ACCOUNT_NUMBER
    AND A.VIEWING_DAY = B.ADJUSTED_EVENT_START_DATE_VESPA
    AND A.VIEWING_DAY = @SCANNING_DAY;

    SET @scanning_day = dateadd(dd, 1, @scanning_day);
End
于 2013-09-27T11:23:08.613 回答