我有这个不想运行的SQL Update Query。这很简单,我想用子选择E中给出的值更新T. 子选择E返回3323行,T具有相同的3323行。
它们由dim01,dim02,dim03和时间唯一链接。 (T的来源与E相同) 看到执行计划我没有看到任何错误,看起来它应该是高性能的。
update T
set T.ACT04 = E.ACT04,
T.ACT05 = E.ACT05
from ( select left(A.PERIOD_ID,4) + right(A.PERIOD_ID,2) as PERIOD_ID, A.[d_1],A.[d_3],A.[d_5],
sum(C.[V_AANREKENING]) as ACT04 ,sum(C.[V_VORDERING]) as ACT05
FROM [CVL].dbo.[C_DS_DB_FI_SOK] A INNER JOIN [CVD].[dbo].[REF_YTD] B
ON left(A.PERIOD_ID,4) + right(A.PERIOD_ID,2) = B.ref_period
INNER JOIN [CVL].[dbo].[C_DS_DB_FI_SOK] C
ON B.agg_period = left(C.PERIOD_ID,4) + right(C.PERIOD_ID,2) and
left(A.PERIOD_ID,4) = left(C.PERIOD_ID,4) and
A.d_1 = C.d_1 and
A.d_2 = C.d_2 and
A.d_3 = C.d_3 and
A.D_BELEIDSDOMEIN = C.D_BELEIDSDOMEIN and
A.D_BELEIDSVELD = C.D_BELEIDSVELD and
A.D_BELEIDSITEM = C.D_BELEIDSITEM and
A.D_7 = C.D_7
WHERE LEFT(A.d_7, 1) = 2 or LEFT(A.d_7, 1) = 6 or LEFT(A.d_7, 1) = 7 or LEFT(A.d_7, 2) = 42 or LEFT(A.d_7, 2) = 17
or LEFT(A.d_7, 4) = 4943 or LEFT(A.d_7, 4) = 4944 or LEFT(A.d_7, 4) = 4949 or LEFT(A.d_7, 4) = 4959 or LEFT(A.d_7, 3) = 102
group by A.PERIOD_ID, A.d_1, A.d_3, A.d_5 ) E
inner join [CVD].[dbo].[FACT_QUANTITY_TMP] T
on E.d_1 = T.dim01 and
E.d_3 = T.dim02 and
E.d_5 = T.dim03 and
E.PERIOD_ID = T.YEAR + RIGHT('00' + CAST(DATEPART(mm, T.FULL_DATE) AS varchar(2)), 2)
令我感到惊讶的是,内部加入3323记录与另外3323条记录将花费超过15分钟。
答案 0 :(得分:0)
如果是我的查询,请查看
Sql Server擅长优化,但有些事情太复杂了。
(经过研究"在评论字段中找到的答案"我看到我应该发布作为答案而不是评论。我希望这不会做错任何事。)