以下查询需要10秒以上。如何微调此查询?
SELECT
COALESCE(A.KEY1, B.KEY1) as KEY1,
COALESCE(A.KEY2, B.KEY2) as KEY2,
COALESCE(A.KEY3, B.KEY3) as KEY3,
A.NUMBER, A.SERVICE_DATE, A.SERVICE_TYPE, B.CLERK_NAME,
(SELECT TOP 1 VALUE FROM C
WHERE ((A.KEY1 = C.KEY1 AND A.KEY2 = C.KEY2 AND A.KEY3 = C.KEY3) OR
(B.KEY1 = C.KEY1 AND B.KEY2 = C.KEY2 AND B.KEY3 = C.KEY3))
AND C.CODE LIKE '*_SABC') AS XXXSVTM,
(SELECT TOP 1 VALUE FROM C
WHERE ((A.KEY1 = C.KEY1 AND A.KEY2 = C.KEY2 AND A.KEY3 = C.KEY3) OR
(B.KEY1 = C.KEY1 AND B.KEY2 = C.KEY2 AND B.KEY3 = C.KEY3))
AND C.CODE LIKE '*_SBCD') AS XXXSVCM,
(SELECT TOP 1 VALUE FROM C
WHERE ((A.KEY1 = C.KEY1 AND A.KEY2 = C.KEY2 AND A.KEY3 = C.KEY3) OR
(B.KEY1 = C.KEY1 AND B.KEY2 = C.KEY2 AND B.KEY3 = C.KEY3))
AND C.CODE LIKE '*_SCDE') AS XXXCSRV,
(SELECT TOP 1 VALUE FROM C
WHERE ((A.KEY1 = C.KEY1 AND A.KEY2 = C.KEY2 AND A.KEY3 = C.KEY3) OR
(B.KEY1 = C.KEY1 AND B.KEY2 = C.KEY2 AND B.KEY3 = C.KEY3))
AND C.CODE LIKE '*_SDEF') AS XXXSRFN,
(select TOP 1 VALUE from C where ((A.KEY1=C.KEY1 and A.KEY2=C.KEY2 and A.KEY3=C.KEY3) OR (B.KEY1 = C.KEY1 AND B.KEY2 = C.KEY2 AND B.KEY3 = C.KEY3)) and C.CODE like '*_SEFG') as XXXCSRV
,(select TOP 1 VALUE from C where ((A.KEY1=C.KEY1 and A.KEY2=C.KEY2 and A.KEY3=C.KEY3) OR (B.KEY1 = C.KEY1 AND B.KEY2 = C.KEY2 AND B.KEY3 = C.KEY3)) and C.CODE like '*_SFGH') as XXXSRLN
,(select TOP 1 VALUE from C where ((A.KEY1=C.KEY1 and A.KEY2=C.KEY2 and A.KEY3=C.KEY3) OR (B.KEY1 = C.KEY1 AND B.KEY2 = C.KEY2 AND B.KEY3 = C.KEY3)) and C.CODE like '*_SXYZ') as XXXSRCY
,(select TOP 1 VALUE from C where ((A.KEY1=C.KEY1 and A.KEY2=C.KEY2 and A.KEY3=C.KEY3) OR (B.KEY1 = C.KEY1 AND B.KEY2 = C.KEY2 AND B.KEY3 = C.KEY3)) and C.CODE like '*_SVFN') as XXXSVFN
FROM
A
FULL JOIN
B ON (A.KEY1 = B.KEY1 AND A.KEY2 = B.KEY2 AND A.KEY3 = B.KEY3);
更新:是打字错误,现在调整了where子句
1条答案
按热度按时间y53ybaqx1#
看起来像是a和c之间的内部连接。
你两次加入a和c之间。
对每条记录运行8个相关子查询。
你要的是c的第一张唱片,没有特别的顺序。
这将在更短的时间内提供相同的结果。
如果每个子查询中的或的右侧
B.KEY1=C.KEY1
等等。。。