需要帮助来微调SQL内部联接查询 - Oracle

时间:2013-01-08 16:46:07

标签: oracle

我需要帮助来调整Oracle SQL查询,这是长时间运行的。

 SELECT 
     MIN( E.MKT_PRC),
     MAX( E.MKT_PRC)
 FROM 
     GL_VESTINGPRIME_RPT VP 
     INNER JOIN GRANTZ G 
         ON G.GRANT_NUM = VP.GRANT_NUM 
         OR G.XFER_ORIG = VP.GRANT_NUM 
     INNER JOIN EXERCISE E 
         ON E.GRANT_NUM = G.GRANT_NUM
 WHERE 
     VP.RUNTIME_ID = :B4 
     AND VP.PLAN_NUM = NVL(:B3, VP.PLAN_NUM) 
     AND E.EXER_DT BETWEEN :B2 + 1 
     AND :B1 

1 个答案:

答案 0 :(得分:2)

问题可能是连接条件中的“或”。这往往很难优化。

这个版本明确地结合了两个结果:

SELECT MIN( E.MKT_PRC), MAX( E.MKT_PRC)
from ((select E.MKT_PRC, E.MKT_PRC
       FROM GL_VESTINGPRIME_RPT VP INNER JOIN GRANTZ G 
            ON G.GRANT_NUM = VP.GRANT_NUM OR
            EXERCISE E
            ON E.GRANT_NUM = G.GRANT_NUM
       WHERE VP.RUNTIME_ID = :B4 AND
             VP.PLAN_NUM = NVL(:B3, VP.PLAN_NUM) AND
             E.EXER_DT BETWEEN :B2 + 1 AND :B1
     ) union all
     (select E.MKT_PRC, E.MKT_PRC
       FROM GL_VESTINGPRIME_RPT VP INNER JOIN GRANTZ G 
            ON G.XFER_ORIG = VP.GRANT_NUM OR
            EXERCISE E
            ON E.GRANT_NUM = G.GRANT_NUM
       WHERE VP.RUNTIME_ID = :B4 AND
             VP.PLAN_NUM = NVL(:B3, VP.PLAN_NUM) AND
             E.EXER_DT BETWEEN :B2 + 1 AND :B1
     )) t

如果表上有适当的索引,这应该非常快。