在末尾连接一个where语句的表或在子查询中使用where语句

时间:2013-06-21 18:23:42

标签: sql oracle10g

我有以下查询,它连接多个表,每个表包含超过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,就像我在第二个中所做的那样是正确的吗?

由于

0 个答案:

没有答案