我有以下查询,它连接多个表,每个表包含超过 100 万行:
Select * From TableA A
LEFT JOIN TableB B ON A.col1 = B.col1
INNER JOIN TableC C ON C.col2 = B.col2
INNER JOIN TableD D ON D.col2 = C.col2
INNER JOIN TableE E ON E.col1 = B.col1
INNER JOIN (Select * From TableF F JOIN TableG G ON F.ID=G.ID) FG
ON FG.cId = E.cId AND FG.custID = C.custID
WHERE C.ACCEPTED = 'Y'
AND D.colx IS NULL
AND D.coly IS NULL
And D.START_DATE <= A.SD
And D.END_DATE >= A.SD
由于这花费了太长时间,我决定对其进行优化并提出以下建议:
Select * From TableA A
INNER JOIN TableE E ON E.col1 = A.col1
INNER JOIN (Select * From TableF F JOIN TableG G ON F.ID=G.ID) FG
ON FG.cID = C.cID AND FG.custID =
(Select custID From
TableB B
TableC C ON C.col2 = B.col2
TableD D ON D.col2 = C.col2
WHERE C.ACCEPTED = 'Y'
AND D.colx IS NULL
AND D.colx IS NULL
And D.START_DATE <= A.SD
And D.END_DATE >= A.SD
AND B.col1 = A.col1
AND rownum = 1
)
这改进了查询,但我不确定这两个查询是否会产生相同的结果(我已经测试了一些场景,结果似乎相同,但由于所有表都包含数百万行,我无法遍历每一行并进行比较),而不是像我在第二个中那样简单地从子查询中获取 custId 来加入所有这些表是正确的吗?
谢谢