在IF ELSE的情况下,Oracle存储过程执行计划

时间:2016-05-30 11:49:55

标签: oracle performance stored-procedures jdbc

我有一个问题,如果存储过程的执行计划具有IF ELSE语句,如何根据IN参数的值执行IF或ELSE块并且块中的查询获取相同的值,那么如何创建存储过程的执行计划?来自相同的表但在where子句中有不同的列。

据我所知,每个存储过程只有一个执行计划。如果Java应用程序使用普通JDBC调用存储过程,则首次调用过程时会创建执行计划,并且执行块(IF)将用于创建执行计划。

下次执行ELSE块时,它使用相同的执行计划,因此执行速度很慢。

我问的是这个问题,因为我调用类似的存储过程,执行时间根据我传递的值和执行的块而有很大不同。

如果是这种情况,可以采取哪些措施来提高性能。是否应该编写单独的程序而不是使用IF ELSE。

PFB存储过程代码:

PACKAGE         COP00134_006_1 AS
    TYPE t_cur_type IS REF CURSOR;
    PROCEDURE GetConsignmentDetails(
      sPInputList     IN VARCHAR2,
      sPInputType     IN VARCHAR2,
      cPCon           OUT t_cur_type,
      cPStatus        OUT t_cur_type,
      sPError         OUT VARCHAR2);
END COP00134_006_1;

实现:

PACKAGE BODY         COP00134_006_1 AS
    PROCEDURE GetConsignmentDetails(
      sPInputList     IN VARCHAR2,
      sPInputType     IN VARCHAR2,
      cPCon           OUT t_cur_type,
      cPStatus        OUT t_cur_type,
      sPError         OUT VARCHAR2)
    IS
    BEGIN
        sPError := '0';
        IF sPInputType = 'CON' THEN
         OPEN cPCon FOR
           SELECT 
                    con_id
                   ,con_legacy_id
                   ,con_create_td
                   ,con_pickup_lt
                   ,con_deliv_due_lt
                   ,CON_DELIV_END_LT
                   ,SRP_ID_AREA_DEST
                   ,CON_CLNT_REF_TX
                   ,BUL_CSYS_ID_ORIG
                   ,BUL_ID_ORIG
                   ,BUL_NM_ORIG
                   ,BUL_NM_DEST
                   ,cpn_oa_town_nm_r
                   ,cpn_oa_town_nm_d
                   ,cou_iso_id_orig
                   ,Cou_Nm_Orig
                   ,cou_iso_id_dest
                   ,Cou_Nm_Dest
                   ,PCE_QT
                   ,DUPLICATES
                   ,COS_SIGN_NM FROM (
             SELECT  /*+ cardinality (b 3) */ 
                    co.con_id
                   ,co.con_legacy_id
                   ,co.con_create_td
                   ,co.con_pickup_lt
                   ,co.con_deliv_due_lt
                   ,co.CON_DELIV_END_LT
                   ,co.SRP_ID_AREA_DEST
                   ,co.CON_CLNT_REF_TX
                   ,co.BUL_CSYS_ID_ORIG
                   ,blOrig.BUL_ID as BUL_ID_ORIG
                   ,blOrig.BUL_NM as BUL_NM_ORIG
                   ,blDest.BUL_NM as BUL_NM_DEST
                   ,cnr.cpn_oa_town_nm as cpn_oa_town_nm_r
                   ,cnd.cpn_oa_town_nm as cpn_oa_town_nm_d
                   ,co.cou_iso_id_orig
                   ,cuOrig.Cou_nm as Cou_Nm_Orig
                   ,co.cou_iso_id_dest
                   ,cuDest.Cou_nm as Cou_Nm_Dest
                   ,(select count(*) from CORPCV01 PC WHERE PC.CON_ID = CO.CON_ID) PCE_QT
                   ,CASE WHEN COUNT(*) OVER(PARTITION BY CON_LEGACY_ID) > 1 THEN 'TRUE' ELSE 'FALSE' END AS DUPLICATES
                   ,cs.COS_SIGN_NM, ROW_NUMBER() OVER(PARTITION BY CS.CON_ID ORDER BY COS_EVENT_TD) SEQ_NR
               FROM corcov01 co
              INNER JOIN corcnv01 cnr
                 ON cnr.con_id = co.con_id
                AND cnr.cpn_type_cd = 'R'
               LEFT OUTER JOIN corcnv01 cnd
                 ON cnd.con_id = co.con_id
                AND cnd.cpn_type_cd = 'D'
              INNER JOIN ncrcuv01 cuOrig
                 ON cuOrig.COU_ISO_ID = co.COU_ISO_ID_ORIG
                AND TRUNC(CURRENT_DATE) BETWEEN cuOrig.COU_EFFECT_DT AND cuOrig.COU_EFFECT_TO_DT
              INNER JOIN ncrcuv01 cuDest
                 ON cuDest.COU_ISO_ID = co.COU_ISO_ID_DEST
                AND TRUNC(CURRENT_DATE) BETWEEN cuDest.COU_EFFECT_DT AND cuDest.COU_EFFECT_TO_DT
              LEFT OUTER JOIN ncrblv01 blOrig
                 ON blOrig.BUL_CSYS_ID = co.BUL_CSYS_ID_ORIG
                AND TRUNC(CURRENT_DATE) BETWEEN blOrig.BUL_EFFECT_DT AND blOrig.BUL_EFFECT_TO_DT
              LEFT OUTER JOIN ncrblv01 blDest
                 ON blDest.BUL_CSYS_ID = co.BUL_CSYS_ID_DEST
                AND TRUNC(CURRENT_DATE) BETWEEN blDest.BUL_EFFECT_DT AND blDest.BUL_EFFECT_TO_DT
              LEFT OUTER JOIN corcsv01 cs
                 ON co.con_id = cs.con_id 
                AND cs.cos_sign_nm is not null
                and cs.cos_delete_in = 'N'
               WHERE co.con_Legacy_Id IN ( select dataItem from TABLE(ZYADMIN.ZYP90008.GetListFromCSV(sPInputList)) b )
               )WHERE SEQ_NR = 1;
          OPEN cPStatus FOR
              SELECT csv.CON_ID AS CON_ID_COS
                    ,csv.XSF_ID
                    ,csv.XSS_ID
                    ,csv.XSG_ID
                    ,csv.XSD_ID
                    ,ndv.XSX_ID
                    ,ndv.xsd_customer_ds
                    ,BUL_CSYS_ID_OCC
                    ,nlv.BUL_NM as BUL_NM_OCC
                    ,csv.COS_EVENT_LT
                    ,ndv.XSD_SEVERITY_CD
                    ,qb.QLA_DS
                FROM corcsv01 csv 
               INNER JOIN corcov01 co
                  ON csv.con_id = co.con_id
               INNER JOIN ncrsdv01 ndv 
                  ON ndv.XSF_ID=csv.XSF_ID 
                 AND ndv.XSS_ID=csv.XSS_ID 
                 AND ndv.XSG_ID=csv.XSG_ID 
                 AND ndv.XSD_ID=csv.XSD_ID 
                 AND TRUNC(CURRENT_DATE) BETWEEN ndv.XSD_EFFECT_DT AND ndv.XSD_EFFECT_TO_DT 
               INNER JOIN ncrksv01 ks 
                  ON ndv.XSX_ID= ks.XSX_ID 
                 AND TRUNC(CURRENT_DATE) BETWEEN ks.SKT_EFFECT_DT AND ks.SKT_EFFECT_TO_DT 
                 AND ks.SCA_ID = 'WEB'
               LEFT OUTER JOIN ncrqav01 qa
                 ON qa.XSX_ID = ndv.XSX_ID
                AND qa.APP_ID = 'EXCO'
                AND TRUNC(CURRENT_DATE) BETWEEN QAS_EFFECT_DT AND QAS_EFFECT_TO_DT
               LEFT OUTER JOIN ncrqbv01 qb
                 ON qa.QLA_ID = qb.QLA_ID
                AND qa.APP_ID = qb.APP_ID
                AND TRUNC(CURRENT_DATE) BETWEEN QLA_EFFECT_DT AND QLA_EFFECT_TO_DT
               INNER JOIN ncrblv01 nlv 
                  ON nlv.BUL_CSYS_ID=csv.BUL_CSYS_ID_OCC 
                 AND TRUNC(CURRENT_DATE)BETWEEN nlv.BUL_EFFECT_DT AND nlv.BUL_EFFECT_TO_DT 
               WHERE co.con_Legacy_Id IN ( select dataItem from TABLE(ZYADMIN.ZYP90008.GetListFromCSV(sPInputList)) b );
        ELSE
         OPEN cPCon FOR
                    SELECT 
                    con_id
                   ,con_legacy_id
                   ,con_create_td
                   ,con_pickup_lt
                   ,con_deliv_due_lt
                   ,CON_DELIV_END_LT
                   ,SRP_ID_AREA_DEST
                   ,CON_CLNT_REF_TX
                   ,BUL_CSYS_ID_ORIG
                   ,BUL_ID_ORIG
                   ,BUL_NM_ORIG
                   ,BUL_NM_DEST
                   ,cpn_oa_town_nm_r
                   ,cpn_oa_town_nm_d
                   ,cou_iso_id_orig
                   ,Cou_Nm_Orig
                   ,cou_iso_id_dest
                   ,Cou_Nm_Dest
                   ,PCE_QT
                   ,DUPLICATES
                   ,COS_SIGN_NM FROM (
             SELECT  /*+ cardinality (b 3) */ 
                    co.con_id
                   ,co.con_legacy_id
                   ,co.con_create_td
                   ,co.con_pickup_lt
                   ,co.con_deliv_due_lt
                   ,co.CON_DELIV_END_LT
                   ,co.SRP_ID_AREA_DEST
                   ,co.CON_CLNT_REF_TX
                   ,co.BUL_CSYS_ID_ORIG
                   ,blOrig.BUL_ID as BUL_ID_ORIG
                   ,blOrig.BUL_NM as BUL_NM_ORIG
                   ,blDest.BUL_NM as BUL_NM_DEST
                   ,cnr.cpn_oa_town_nm as cpn_oa_town_nm_r
                   ,cnd.cpn_oa_town_nm as cpn_oa_town_nm_d
                   ,co.cou_iso_id_orig
                   ,cuOrig.Cou_nm as Cou_Nm_Orig
                   ,co.cou_iso_id_dest
                   ,cuDest.Cou_nm as Cou_Nm_Dest
                   ,(select count(*) from CORPCV01 PC WHERE PC.CON_ID = CO.CON_ID) PCE_QT
                   ,'FALSE' AS DUPLICATES
                   ,cs.COS_SIGN_NM, ROW_NUMBER() OVER(PARTITION BY CS.CON_ID ORDER BY COS_EVENT_TD) SEQ_NR
               FROM corcov01 co
              INNER JOIN corcnv01 cnr
                 ON cnr.con_id = co.con_id
                AND cnr.cpn_type_cd = 'R'
               LEFT OUTER JOIN corcnv01 cnd
                 ON cnd.con_id = co.con_id
                AND cnd.cpn_type_cd = 'D'
              INNER JOIN ncrcuv01 cuOrig
                 ON cuOrig.COU_ISO_ID = co.COU_ISO_ID_ORIG
                AND TRUNC(CURRENT_DATE) BETWEEN cuOrig.COU_EFFECT_DT AND cuOrig.COU_EFFECT_TO_DT
              INNER JOIN ncrcuv01 cuDest
                 ON cuDest.COU_ISO_ID = co.COU_ISO_ID_DEST
                AND TRUNC(CURRENT_DATE) BETWEEN cuDest.COU_EFFECT_DT AND cuDest.COU_EFFECT_TO_DT
              LEFT OUTER JOIN ncrblv01 blOrig
                 ON blOrig.BUL_CSYS_ID = co.BUL_CSYS_ID_ORIG
                AND TRUNC(CURRENT_DATE) BETWEEN blOrig.BUL_EFFECT_DT AND blOrig.BUL_EFFECT_TO_DT
              LEFT OUTER JOIN ncrblv01 blDest
                 ON blDest.BUL_CSYS_ID = co.BUL_CSYS_ID_DEST
                AND TRUNC(CURRENT_DATE) BETWEEN blDest.BUL_EFFECT_DT AND blDest.BUL_EFFECT_TO_DT
              LEFT OUTER JOIN corcsv01 cs
                 ON co.con_id = cs.con_id 
                AND cs.cos_sign_nm is not null
                and cs.cos_delete_in = 'N'
               WHERE co.CON_CLNT_REF_CR IN ( select dataItem from TABLE(ZYADMIN.ZYP90008.GetListFromCSV(sPInputList)) b )
               )WHERE SEQ_NR = 1;
          OPEN cPStatus FOR
              SELECT csv.CON_ID AS CON_ID_COS
                    ,csv.XSF_ID
                    ,csv.XSS_ID
                    ,csv.XSG_ID
                    ,csv.XSD_ID
                    ,ndv.XSX_ID
                    ,ndv.xsd_customer_ds
                    ,BUL_CSYS_ID_OCC
                    ,nlv.BUL_NM as BUL_NM_OCC
                    ,csv.COS_EVENT_LT
                    ,ndv.XSD_SEVERITY_CD
                    ,qb.QLA_DS
                FROM corcsv01 csv 
               INNER JOIN corcov01 co
                  ON csv.con_id = co.con_id
               INNER JOIN ncrsdv01 ndv 
                  ON ndv.XSF_ID=csv.XSF_ID 
                 AND ndv.XSS_ID=csv.XSS_ID 
                 AND ndv.XSG_ID=csv.XSG_ID 
                 AND ndv.XSD_ID=csv.XSD_ID 
                 AND TRUNC(CURRENT_DATE) BETWEEN ndv.XSD_EFFECT_DT AND ndv.XSD_EFFECT_TO_DT 
               INNER JOIN ncrksv01 ks 
                  ON ndv.XSX_ID= ks.XSX_ID 
                 AND TRUNC(CURRENT_DATE) BETWEEN ks.SKT_EFFECT_DT AND ks.SKT_EFFECT_TO_DT 
                 AND ks.SCA_ID = 'WEB'
               LEFT OUTER JOIN ncrqav01 qa
                 ON qa.XSX_ID = ndv.XSX_ID
                AND qa.APP_ID = 'EXCO'
                AND TRUNC(CURRENT_DATE) BETWEEN QAS_EFFECT_DT AND QAS_EFFECT_TO_DT
               LEFT OUTER JOIN ncrqbv01 qb
                 ON qa.QLA_ID = qb.QLA_ID
                AND qa.APP_ID = qb.APP_ID
                AND TRUNC(CURRENT_DATE) BETWEEN QLA_EFFECT_DT AND QLA_EFFECT_TO_DT
               INNER JOIN ncrblv01 nlv 
                  ON nlv.BUL_CSYS_ID=csv.BUL_CSYS_ID_OCC 
                 AND TRUNC(CURRENT_DATE)BETWEEN nlv.BUL_EFFECT_DT AND nlv.BUL_EFFECT_TO_DT 
               WHERE co.CON_CLNT_REF_CR IN ( select dataItem from TABLE(ZYADMIN.ZYP90008.GetListFromCSV(sPInputList)) b );
        END IF;
    EXCEPTION
        WHEN OTHERS THEN
          sPError := '1' ||' '||SQLCODE||' '||SUBSTR(SQLERRM, 1, 200);
    END;
END COP00134_006_1;

问题在于ELSE部分中执行的查询。 正如评论中所提到的,我单独运行相同的查询,并且它们在不到一秒的时间内执行,而当相同的数据传递给过程时,它需要超过一分钟。我无法运行分析器,因为我没有数据库访问权限,而且询问DBA的过程将花费一周以上的时间,我需要尽快解决这个问题

1 个答案:

答案 0 :(得分:0)

Oracle为查询而非存储过程构建执行计划。因此,存储过程中的每个查询都有自己的执行计划。你这么说关于你的查询:

  

“在where子句中使用不同的列”

不同的列意味着不同的访问路径:这可能转化为索引的不同使用,不同的磁盘读取活动,可能不同的数据量。因此,根据IF ... ELSE的哪个分支来体验不同的性能并不奇怪。具有单独的过程不会改变基础查询的性能。

要检查差异,请为每个单独的查询运行EXPLAIN PLAN。 Find out more。您需要将语句剪切成SQL工作表(或用于处理SQL的任何工具)。

如果您认为问题出在存储过程中,那么您应该调查PL / SQL分析器的时间。 Find out more。如果它尚未安装,您可能需要DBA来安装它。

  

“当在存储过程中执行相同的查询时,相同的值和相同的数据需要60秒以上”

你一直在谈论经过的时间。你是如何衡量这些的?你在测量什么?时间到了哪里?当真正的罪魁祸首是一个糟糕的JDBC配置的错误网络连接时,这不是第一次因为性能不佳而被归咎于数据库。