调优过程中存在的执行即时语句

时间:2018-07-05 07:24:36

标签: oracle plsql sqlperformance

我想调整一个过程,该过程包含许多动态SQL语句,这些语句花费太多时间来执行。我需要帮助来调整这些或其他解决方案。

示例:

EXECUTE IMMEDIATE 'CREATE TABLE C1_LAN_GTT_1 NOLOGGING PARALLEL 4 AS 
SELECT /*+INDEX(C,CR_DECISION_M_CD1)*/ A.AGREEMENTID, 
       A.AGREEMENTNO, A.LESSEEID, A.PRODUCTFLAG, A.APP_FORMNO, A.AMTFIN
FROM   LEA_AGREEMENT_DTL A, CR_DECISION_M C
WHERE  A.AGREEMENTID = C.APPID
AND    A.STATUS = ''A''
AND    TRUNC(C.AUTHDATE) BETWEEN' || '''' || P_FROM_DATE || '''' ||
                       ' AND ' || '''' || P_TO_DATE || '''';

当我在PLSQL Developer中以调试模式检查过程时,发现这些动态语句花费了太多时间。

查询中没有数据仍然需要时间来执行。

3 个答案:

答案 0 :(得分:0)

没有更多详细信息,例如执行计划以及表,索引,数据量和分发的完整详细信息,我不知道您要面对什么性能问题。但是,我可能会从以下内容开始:

declare
    p_from_date date := date '2018-01-01';
    p_to_date   date := date '2018-02-01';

    myddl long := 'create table c1_lan_gtt_1 nologging parallel 4 as 
    select /*+index(c cr_decision_m_cd1)*/ a.agreementid, 
           a.agreementno, a.lesseeid, a.productflag, a.app_formno, a.amtfin
    from   lea_agreement_dtl a
           join cr_decision_m c on c.appid = a.agreementid
    where  a.status = ''A''
    and    c.authdate between date ''' || to_char(p_from_date,'YYYY-MM-DD') || '''' ||
                    ' and date '''     || to_char(p_to_date,'YYYY-MM-DD') || '''';
begin
    dbms_output.put_line(myddl);
end;

生成

create table c1_lan_gtt_1 nologging parallel 4 as 
    select /*+index(c cr_decision_m_cd1)*/ a.agreementid, 
           a.agreementno, a.lesseeid, a.productflag, a.app_formno, a.amtfin
    from   lea_agreement_dtl a
           join cr_decision_m c on c.appid = a.agreementid
    where  a.status = 'A'
    and    trunc(c.authdate) between date '2018-01-01' and date '2018-02-01'

编辑:由于索引CR_DECISION_M_CD1位于(APPID, TRUNC(AUTHDATE))上(来自注释),尽管逻辑上不需要,我已经恢复了trunc()函数。

答案 1 :(得分:-1)

检查P_FROM_DATE和P_TO_DATE是绑定变量enter link description here

答案 2 :(得分:-1)

使用存在而不是加入的新版本的立即执行语句

EXECUTE IMMEDIATE 'CREATE TABLE C1_LAN_GTT_1 NOLOGGING PARALLEL 4 AS 
SELECT A.AGREEMENTID, A.AGREEMENTNO, A.LESSEEID, A.PRODUCTFLAG, A.APP_FORMNO, A.AMTFIN
FROM LEA_AGREEMENT_DTL A
WHERE A.STATUS = ''A''
AND EXISTS (
  SELECT /*+INDEX(C CR_DECISION_M_CD1)*/ NULL 
  FROM CR_DECISION_M C
  WHERE C.APPID = A.AGREEMENTID
  AND TRUNC(C.AUTHDATE) BETWEEN '''||P_FROM_DATE||''' AND '''||P_TO_DATE||'''
)';