我试图运行以下更新,但继续遇到假脱机空间问题。有什么方法可以优化吗?
UPDATE dp_wedw_snd.platinumsaves
SET package_name = (SELECT package_name
FROM dp_wedw_snd.selfpaysubs_agid_platsaves
WHERE trim(dp_wedw_snd.platinumsaves.esn1) = trim(cast(dp_wedw_snd.selfpaysubs_agid_platsaves.esn as varchar(255)))
and abs(dp_wedw_snd.platinumsaves.shortdate - dp_wedw_snd.selfpaysubs_agid_platsaves.dateasof) =
(select min(abs(dp_wedw_snd.platinumsaves.shortdate - dp_wedw_snd.selfpaysubs_agid_platsaves.dateasof))
from dp_wedw_snd.platinumsaves, dp_wedw_snd.selfpaysubs_agid_platsaves)
)
WHERE
EXISTS(SELECT esn
FROM dp_wedw_snd.selfpaysubs_agid_platsaves
WHERE trim(dp_wedw_snd.platinumsaves.esn1) =trim(cast(dp_wedw_snd.selfpaysubs_agid_platsaves.esn as varchar(255)))
);
答案 0 :(得分:0)
此查询存在多个问题。
from dp_wedw_snd.platinumsaves, dp_wedw_snd.selfpaysubs_agid_platsaves
会产生交叉联接。 dp_wedw_snd.platinumsaves.esn1
中有领先的空白吗?dp_wedw_snd.selfpaysubs_agid_platsaves.esn
数字列?为什么?为什么你也要修剪它?此查询应该执行相同操作(删除交叉连接并将MIN重写为QUALIFY):
UPDATE dp_wedw_snd.platinumsaves
FROM
(
SELECT package_name, esn
FROM dp_wedw_snd.platinumsaves AS ps JOIN dp_wedw_snd.selfpaysubs_agid_platsaves AS sap
ON TRIM(ps.esn1) = TRIM(CAST(sap.esn AS VARCHAR(255)))
QUALIFY ROW_NUMBER() OVER (PARTITION BY ps.esn1 ORDER BY ABS(ps.shortdate - sap.dateasof)) = 1
) AS src
SET package_name = src.package_name
WHERE TRIM(dp_wedw_snd.platinumsaves.esn1) = TRIM(CAST(src.esn AS VARCHAR(255)))
当然你应该测试它是否完全一样。并检查你是否可以摆脱TRIM。