循环或动态执行

时间:2013-09-27 10:59:05

标签: sql sybase-iq

我有一张桌子: 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

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

1 个答案:

答案 0 :(得分:1)

我认为您需要在循环内增加@scanning_day,此时while条件永远不会被满足,因为@scanning_day保持不变为@var_period_start。 e.g。

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