运行oracle sql查询加入大约10个表时出现ORA-01652错误

时间:2015-07-02 09:12:58

标签: sql oracle

我在oracle上有三个用户并且他们有相同的表空间,当我运行以下查询时,我在执行查询时遇到以下问题。并且在执行它时需要花费很多时间:(

ORA-01652: unable to extend temp segment by 128 in tablespace TEMP
01652. 00000 -  "unable to extend temp segment by %s in tablespace %s"
*Cause:    Failed to allocate an extent of the required number of blocks for
       a temporary segment in the tablespace indicated.
*Action:   Use ALTER TABLESPACE ADD DATAFILE statement to add one or more
       files to the tablespace indicated.

查询:

SELECT  A.*    
FROM LEA_AGREEMENT_DTL_RCL A,    
  nbfc_customer_m b,    
  db1.NBFC_PRODUCT_M c,    
  db1.LEA_INSTRUMENT_DTL e,    
  db1.LEA_LOANTYPE_M f,    
  db1.NBFC_BRANCH_M g,    
  ETRUPTI_CUST_AGRMNT_MAPPING h,    
  (SELECT A.caseid,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 82    
      AND A.ADVICETYPE    = 'R'    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS preemioverdue,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 9    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS installmentoverdue,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 37    
      AND A.ADVICETYPE    = 'P'    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS unadjustedamount,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 8    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS chequebounce,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 7    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS LPP,    
    SUM (    
    CASE    
      WHEN b.chargecodeid NOT IN (82,9)    
      AND A.ADVICETYPE         = 'R'    
      AND A.BPTYPE             = 'LS'    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END ) AS otheroverdue,    
    SUM (    
    CASE    
      WHEN A.ADVICETYPE = 'R'    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END )                 AS netreceivable,    
    MAX (c.balancetenure) AS balancetenure    
  FROM db1.NBFC_TXN_ADVICE_DTL A,    
    db1.NBFC_CHARGES_M b,    
    ETRUPTI_CUST_AGRMNT_MAPPING d,    
    (SELECT x.AGREEMENTID,    
      COUNT (1) AS balancetenure,    
      TO_CHAR (SUM(NVL(PRINCOMP,0)),'999,999,999,999,999,900.00')    
    FROM db1.LEA_REPAYSCH_DTL x,    
      ETRUPTI_CUST_AGRMNT_MAPPING y    
    WHERE x.billflage = 'N'    
    AND X.ADVFLAG     ='N'    
    AND x.agreementid = y.agreementid    
    AND y.user_id     = :user_id    
    GROUP BY x.AGREEMENTID    
    ) c    
  WHERE A.status      = 'A'    
  AND A.chargeid      = b.chargeid    
  AND A.CASEID        =C.AGREEMENTID    
  AND c.agreementid   = d.agreementid    
  AND d.user_id       = :user_id    
  AND A.PTXNADVICEID IS NULL    
  GROUP BY A.caseid    
  ) BALANCES,    
  (SELECT w.agreementid,    
    COUNT(1)                                                      AS baltenure,    
    TO_CHAR (SUM(NVL(w.PRINCOMP,0)),'999,999,999,999,999,900.00') AS principalcomp    
  FROM db1.LEA_REPAYSCH_DTL w,    
    ETRUPTI_CUST_AGRMNT_MAPPING z    
  WHERE w.BILLFLAGE = 'N'    
  AND w.agreementid = z.agreementid    
  AND z.user_id     = :user_id    
  GROUP BY w.agreementid    
  ) repay    
WHERE A.lesseeid      = b.customerid    
AND BALANCES.caseid(+)=A.proposalid    
AND A.productflag     = c.code(+)    
AND A.proposalid      = e.agreementid    
AND c.loantype        = f.loantype(+)    
AND A.agreementid     = repay.agreementid(+)    
AND A.agreementid     = h.agreementid    
AND g.branchid(+)     = a.service_branch    
AND h.USER_ID         = :user_id

如果可以以某种方式优化此查询,那么建议最受欢迎。

1 个答案:

答案 0 :(得分:1)

尝试使用CTE。除此之外,您应该使用SQL ANSI-Syntax,通常它更容易阅读。

您与表格db1.nbfc_parameter_m进行交叉联接。这是故意吗?

with c as
(SELECT x.AGREEMENTID,    
      COUNT (1) AS balancetenure,    
      TO_CHAR (SUM(NVL(PRINCOMP,0)),'999,999,999,999,999,900.00')    
    FROM db1.LEA_REPAYSCH_DTL x    
      JOIN ETRUPTI_CUST_AGRMNT_MAPPING y ON x.agreementid = y.agreementid 
    WHERE x.billflage = 'N'    
    AND X.ADVFLAG     ='N'    
    AND y.user_id     = :user_id    
    GROUP BY x.AGREEMENTID  
    ),

BALANCES AS
   (SELECT A.caseid,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 82    
      AND A.ADVICETYPE    = 'R'    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS preemioverdue,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 9    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS installmentoverdue,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 37    
      AND A.ADVICETYPE    = 'P'    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS unadjustedamount,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 8    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS chequebounce,    
    SUM (    
    CASE    
      WHEN b.chargecodeid = 7    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END) AS LPP,    
    SUM (    
    CASE    
      WHEN b.chargecodeid NOT IN (82,9)    
      AND A.ADVICETYPE         = 'R'    
      AND A.BPTYPE             = 'LS'    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END ) AS otheroverdue,    
    SUM (    
    CASE    
      WHEN A.ADVICETYPE = 'R'    
      THEN NVL (A.adviceamt, 0) - NVL (A.txnadjustedamt, 0) - NVL (A.amtinprocess, 0)    
      ELSE 0    
    END )                 AS netreceivable,    
    MAX (c.balancetenure) AS balancetenure    
  FROM db1.NBFC_TXN_ADVICE_DTL A    
    JOIN db1.NBFC_CHARGES_M b ON A.chargeid = b.chargeid    
    JOIN c ON A.CASEID = C.AGREEMENTID
    JOIN ETRUPTI_CUST_AGRMNT_MAPPING d ON c.agreementid = d.agreementid        
  WHERE A.status = 'A'    
  AND d.user_id       = :user_id    
  AND A.PTXNADVICEID IS NULL    
  GROUP BY A.caseid    
  ),

repay AS
  (SELECT w.agreementid,    
    COUNT(1)                                                      AS baltenure,    
    TO_CHAR (SUM(NVL(w.PRINCOMP,0)),'999,999,999,999,999,900.00') AS principalcomp    
  FROM db1.LEA_REPAYSCH_DTL w    
    JOIN ETRUPTI_CUST_AGRMNT_MAPPING z ON w.agreementid = z.agreementid    
  WHERE w.BILLFLAGE = 'N'    
  AND z.user_id     = :user_id    
  GROUP BY w.agreementid    
  )  

SELECT  A.*    
FROM LEA_AGREEMENT_DTL_RCL A    
  JOIN nbfc_customer_m b ON A.lesseeid      = b.customerid    
  RIGHT OUTER JOIN db1.NBFC_PRODUCT_M c ON A.productflag     = c.code
  CROSS JOIN db1.nbfc_parameter_m d -> There is no join condition! Is this by purpose a CROSS JOIN?  
  JOIN db1.LEA_INSTRUMENT_DTL e ON A.proposalid      = e.agreementid     
  RIGHT OUTER JOIN db1.LEA_LOANTYPE_M f ON c.loantype        = f.loantype    
  LEFT OUTER JOINdb1.NBFC_BRANCH_M g ON g.branchid     = a.service_branch      
  JOIN ETRUPTI_CUST_AGRMNT_MAPPING h ON A.agreementid     = h.agreementid      
  LEFT OUTER JOIN BALANCES ON BALANCES.caseid = A.proposalid     
  RIGHT OUTER JOIN  repay ON A.agreementid     = repay.agreementid
WHERE h.USER_ID         = :user_id

也许我混合RIGHT OUTER JOINLEFT OUTER JOIN,我不再使用旧的Oracle连接语法了。