需要Oracle查询优化(已用时间= 2.37秒)

时间:2016-10-28 06:31:19

标签: sql oracle performance oracle10g query-optimization

以下查询需要花费时间(2.37秒)才能执行。我分别在FORWARD_INFO和G_PROFILE表中有大约5,00,00条记录和2,00,00条记录。

我需要优化此查询。它应该需要〜.20秒。你能建议吗?

使用Oracle10g DB。

SELECT S_NUM, STATUS, TRANSLATION_NUM, ANN, NOTIFY_ANN, ID, NAME, 
  PROFILE_ID, ANN_ID, RES_PROFILE_ID, R_ANN, RES_ANN_ID 
FROM(
  SELECT CSC.S_NUM,CSC.STATUS, CSC.TRANSLATION_NUM, CSC.ANN, CSC.NOTIFY_ANN, CSC.ID, CGP.NAME, CGP.PROFILE_ID, CGP.ANN_ID, CGP.RES_PROFILE_ID, CGP.R_ANN, CGP.RES_ANN_ID, 
     RANK() OVER(ORDER BY LENGTH(CSC.S_NUM) DESC) RANKING 
  FROM 
    FORWARD_INFO CSC, G_PROFILE CGP 
  WHERE '0120913005' LIKE CSC.S_NUM||'%' AND 
         CGP.NAME LIKE 'ase' ||'%' AND 
         CSC.ACCOUNT_NUMBER=10 AND CSC.ID= CGP.ID AND 
         CSC.ACTIVE = 1 AND CGP.ACTIVE = 1) 
WHERE RANKING = 1 AND 
         ROWNUM = 1;

2 个答案:

答案 0 :(得分:0)

稍微重新格式化后

SELECT S_NUM, STATUS, TRANSLATION_NUM, 
    ANN, NOTIFY_ANN, ID, NAME, 
    PROFILE_ID, ANN_ID, RES_PROFILE_ID , R_ANN,
    RES_ANN_ID 
FROM
    (SELECT CSC.S_NUM,CSC.STATUS, CSC.TRANSLATION_NUM, CSC.ANN, CSC.NOTIFY_ANN, CSC.ID, 
        CGP.NAME, CGP.PROFILE_ID, CGP.ANN_ID, CGP.RES_PROFILE_ID , CGP.R_ANN, CGP.RES_ANN_ID, 
        RANK() OVER (ORDER BY LENGTH(CSC.S_NUM) DESC) RANKING 
    FROM FORWARD_INFO CSC
        JOIN G_PROFILE CGP ON CSC.ID = CGP.ID 
    WHERE '0120913005' LIKE CSC.S_NUM||'%' 
        AND CGP.NAME LIKE 'ase' ||'%' 
        AND CSC.ACCOUNT_NUMBER = 10          
        AND CSC.ACTIVE = 1 
        AND CGP.ACTIVE = 1) 
WHERE RANKING = 1 
    AND ROWNUM = 1;

你看到了瓶颈:

'0120913005' LIKE CSC.S_NUM||'%' AND CGP.NAME LIKE 'ase' ||'%' 

您可以创建基于函数的索引。

CREATE INDEX NAME_LIKE_ASE ON G_PROFILE (SUBSTR(NAME, 1, 3));
CREATE INDEX S_NUM_LIKE ON G_PROFILE (S_NUM||'%');

您的WHERE条件

'0120913005' LIKE CSC.S_NUM||'%' AND SUBSTR(NAME, 1, 3) = 'ase'

然后你应该在FORWARD_INFO.IDG_PROFILE.ID上建立索引。

答案 1 :(得分:-3)

IN WHERE CLAUSE

WHERE '0120913005' LIKE 
  CSC.S_NUM||'%' AND CGP.NAME LIKE 'ase' ||'%' AND 
  CSC.ACCOUNT_NUMBER=10 AND CSC.ID= CGP.ID AND 
  CSC.ACTIVE = 1 AND CGP.ACTIVE = 1

更改此条件的顺序

WHERE CSC.ACTIVE = 1 AND CGP.ACTIVE = 1  AND 
  CSC.ACCOUNT_NUMBER=10 AND CSC.ID= CGP.ID  AND 
 '0120913005' LIKE 
  CSC.S_NUM||'%' AND CGP.NAME LIKE 'ase' ||'%'

字符串比较需要很多费用