Oracle View的优化查询

时间:2019-04-02 08:31:36

标签: oracle performance subquery query-optimization

我认为Query的成本如此之高:

CREATE OR REPLACE FORCE EDITIONABLE VIEW "TMI_ISD_WV_LINK_REV4" ("P_DATA_NO", "DATA_KR_NO", "R_DATA_NO", "R_WF_ST", "SYSTEM_MATTER_ID") AS 
  SELECT wvl."P_DATA_NO"
    ,wvl."DATA_KR_NO"
    ,wvl."R_DATA_NO"
    ,wvl."R_WF_ST"
    ,(
      select system_matter_id from (select system_matter_id from TMI_ISD_ALL_WORKFLOW wfl WHERE wfl.DATA_NO IN (wvl.R_DATA_NO,wvl.P_DATA_NO) order by wfl.create_date_TIME desc )
      where rownum <= 1--fetch first 1 row only
    )SYSTEM_MATTER_ID
FROM TMI_ISD_WV_LINK_REV3 wvl;

此视图所花费的最高费用: The cost

TMI_ISD_WV_LINK_REV3和TMI_ISD_ALL_WORKFLOW也是视图:

 CREATE OR REPLACE FORCE EDITIONABLE VIEW "TMI_ISD_ALL_WORKFLOW" ("KS_CD", "DATA_NO", "SYSTEM_MATTER_ID", "USER_DATA_ID", "MATTER_NUMBER", "FLOW_ID", "KI_WF_ST", "PROCESS_DATE", "APPLY_AUTH_USER_CODE", "CREATE_DATE_TIME", "CREATER_ID", "UPDATE_DATE_TIME", "UPDATER_ID") AS 
  SELECT "KS_CD","DATA_NO","SYSTEM_MATTER_ID","USER_DATA_ID","MATTER_NUMBER","FLOW_ID","KI_WF_ST","PROCESS_DATE","APPLY_AUTH_USER_CODE","CREATE_DATE_TIME","CREATER_ID","UPDATE_DATE_TIME","UPDATER_ID"
    FROM NWA7_WF_HZ_KGH_KSH_SS

    UNION ALL

    SELECT "KS_CD","DATA_NO","SYSTEM_MATTER_ID","USER_DATA_ID","MATTER_NUMBER","FLOW_ID","KI_WF_ST","PROCESS_DATE","APPLY_AUTH_USER_CODE","CREATE_DATE_TIME","CREATER_ID","UPDATE_DATE_TIME","UPDATER_ID"
    FROM NWA7_WF_HZ_KGH_KSH_YT

  union all 

    SELECT "KS_CD","DATA_NO","SYSTEM_MATTER_ID","USER_DATA_ID","MATTER_NUMBER","FLOW_ID","KI_WF_ST","PROCESS_DATE","APPLY_AUTH_USER_CODE","CREATE_DATE_TIME","CREATER_ID","UPDATE_DATE_TIME","UPDATER_ID"
    FROM NWA7_WF_HZ_SCRH_SS

    UNION ALL

    SELECT "KS_CD","DATA_NO","SYSTEM_MATTER_ID","USER_DATA_ID","MATTER_NUMBER","FLOW_ID","KI_WF_ST","PROCESS_DATE","APPLY_AUTH_USER_CODE","CREATE_DATE_TIME","CREATER_ID","UPDATE_DATE_TIME","UPDATER_ID"
    FROM NWA7_WF_HZ_SC_YT
  UNION ALL

    SELECT "KS_CD","DATA_NO","SYSTEM_MATTER_ID","USER_DATA_ID","MATTER_NUMBER","FLOW_ID","KI_WF_ST","PROCESS_DATE","APPLY_AUTH_USER_CODE","CREATE_DATE_TIME","CREATER_ID","UPDATE_DATE_TIME","UPDATER_ID"
    FROM NWA7_WF_HZ_SKH_YT

    UNION ALL

    SELECT "KS_CD","DATA_NO","SYSTEM_MATTER_ID","USER_DATA_ID","MATTER_NUMBER","FLOW_ID","KI_WF_ST","PROCESS_DATE","APPLY_AUTH_USER_CODE","CREATE_DATE_TIME","CREATER_ID","UPDATE_DATE_TIME","UPDATER_ID"
    FROM NWA7_WF_HZ_SKH_SS;

TMI_ISD_WV_LINK_REV3:

CREATE OR REPLACE FORCE EDITIONABLE VIEW "TMI_ISD_WV_LINK_REV3" ("P_DATA_NO", "DATA_KR_NO", "R_DATA_NO", "R_WF_ST", "SYSTEM_MATTER_ID") AS 
SELECT J1.data_no AS P_DATA_NO
    ,J1.data_kr_no
    ,J2.data_no AS R_DATA_NO
    ,J2.KI_WF_ST AS R_WF_ST
    ,J3.SYSTEM_MATTER_ID
FROM NWJ2_T_KGH_KSH_YT_HZ J1
LEFT JOIN (
    SELECT a.KS_CD
        ,a.DATA_KR_NO
        ,a.DATA_NO
        ,b.KI_WF_ST
    FROM NWJ2_T_KGH_KSH_SS_HZ a
    JOIN NWA8_T_KS_SK_KGH_KSH_SS b ON a.KS_CD = b.KS_CD
        AND a.DATA_NO = b.DATA_NO
    WHERE a.KGH_KSH_SS_YK_KBN = '1'
    ) J2 ON J1.KS_CD = J2.KS_CD
    AND J1.DATA_KR_NO = J2.DATA_KR_NO
LEFT JOIN (
    SELECT KS_CD,SYSTEM_MATTER_ID,DATA_NO
    FROM NWA7_WF_HZ_KGH_KSH_SS

    UNION ALL

    SELECT KS_CD,SYSTEM_MATTER_ID,DATA_NO
    FROM NWA7_WF_HZ_KGH_KSH_YT
    ) J3 ON J1.KS_CD = J3.KS_CD
    AND J1.DATA_NO = J3.DATA_NO
WHERE J1.KGH_KSH_YT_YK_KBN = '1'

UNION ALL

SELECT B1.data_no AS P_DATA_NO
    ,B1.data_kr_no
    ,B2.data_no AS R_DATA_NO
    ,B2.KI_WF_ST AS R_WF_ST
    ,B3.SYSTEM_MATTER_ID
FROM NWB2_T_SC_YT_HZ B1
LEFT JOIN (
    SELECT a.KS_CD
        ,a.DATA_KR_NO
        ,a.DATA_NO
        ,b.KI_WF_ST
    FROM NWB2_T_SC_RH_SS_HZ a
    JOIN NWA8_T_KS_SK_SCRH_SS b ON a.KS_CD = b.KS_CD
        AND a.DATA_NO = b.DATA_NO
    WHERE a.SC_RH_SS_YK_KBN = '1'
    ) B2 ON B1.DATA_KR_NO = B2.DATA_KR_NO
    AND B1.KS_CD = B2.KS_CD
LEFT JOIN (
    SELECT KS_CD,SYSTEM_MATTER_ID,DATA_NO
    FROM NWA7_WF_HZ_SCRH_SS

    UNION ALL

    SELECT KS_CD,SYSTEM_MATTER_ID,DATA_NO
    FROM NWA7_WF_HZ_SC_YT
    ) B3 ON B1.KS_CD = B3.KS_CD
    AND B1.DATA_NO = B3.DATA_NO
WHERE B1.SC_YT_YK_KBN = '1'

UNION ALL

SELECT C1.data_no AS P_DATA_NO
    ,C1.DATA_KR_NO
    ,C2.data_no AS R_DATA_NO
    ,C2.KI_WF_ST AS R_WF_ST
    ,C3.SYSTEM_MATTER_ID
FROM NWC2_T_SKH_YT_HZ C1
LEFT JOIN (
    SELECT a.KS_CD
        ,a.DATA_KR_NO
        ,a.DATA_NO
        ,b.KI_WF_ST
    FROM NWC2_T_SKH_SS_HZ a
    JOIN NWA8_T_KS_SK_SKH_SS b ON a.KS_CD = b.KS_CD
        AND a.DATA_NO = b.DATA_NO
    WHERE a.SKH_SS_YK_KBN = '1'
    ) C2 ON C1.DATA_KR_NO = C2.DATA_KR_NO
    AND C1.KS_CD = C2.KS_CD
LEFT JOIN (
    SELECT KS_CD,SYSTEM_MATTER_ID,DATA_NO
    FROM NWA7_WF_HZ_SKH_YT

    UNION ALL

    SELECT KS_CD,SYSTEM_MATTER_ID,DATA_NO
    FROM NWA7_WF_HZ_SKH_SS
    ) C3 ON C1.KS_CD = C3.KS_CD
    AND C1.DATA_NO = C3.DATA_NO
WHERE C1.SKH_YT_YK_KBN = '1';

据我所知,我无法从oracle的视图中添加索引,所以我猜想罪魁祸首是TMI_ISD_WV_LINK_REV4的此子查询:

(
  select system_matter_id from (select system_matter_id from TMI_ISD_ALL_WORKFLOW wfl WHERE wfl.DATA_NO IN (wvl.R_DATA_NO,wvl.P_DATA_NO) order by wfl.create_date_TIME desc )
  where rownum <= 1--fetch first 1 row only
)SYSTEM_MATTER_ID

1 个答案:

答案 0 :(得分:0)

实际上,已使用Materialized View调整了此性能,但是数据每3分钟更新一次,或如此。

谢谢大家。