查询优化或调优

Posted

技术标签:

【中文标题】查询优化或调优【英文标题】:Query Optimize or Tuning 【发布时间】:2018-02-04 06:00:04 【问题描述】:

我对 SQL 相当陌生,并且对我一直在处理的查询有点不了解。我需要优化下面的 SQL 查询。谁能建议如何优化以下查询及其效率。

WITH MAL_BAL AS
  (SELECT
    /*+parallel(2) */
    DATE,
    BPD,
    A.MNO,
    A.BRT,
    A.BRTD,
    BDR,
    B.CDNO,
    B.CGNO,
    B.BT_DR_AMT,
    B.BT_CR_AMT,
    B.BT_DR_NUM,
    B.BT_CR_NUM
  FROM ABC.POST A
  LEFT OUTER JOIN ABC.BALL B
  ON ( A.BRTD = B.BRTD
  AND A.BRT      = B.BRT)
  WHERE DATE  = TO_DATE ('20170131', 'YYYYMMDD')
  AND B.BRTD BETWEEN TRUNC (ADD_MONTHS (TO_DATE ('20170131', 'YYYYMMDD'), - 1),'MM') AND TO_DATE ('20170131', 'YYYYMMDD')
  AND BAT_DPST_TYP_CDE NOT IN ('S', 'V')
  AND B.PRCS_CENTRE_CDE     = 'NA'
  ),
  A_LKUP AS
  (SELECT
    /*+parallel(2)*/
    AA.DATE,
    AA.MNO,
    AA.CDNO,
    AA.CGNO,
    AA.DAC,
    AA.PAC,
    CTD_DB_AMT,
    CTD_CR_NUM,
    CTD_DB_NUM,
    CTD_CR_AMT,
    B.BRT,
    B.BRTD,
    CC.M_DC,
    CASE
      WHEN CC.M_DC IN ('E','P','A','I')
      THEN NVL (CC.NPT_DACE,AA.DAC)
    END AS DAC_LKP,
    CASE
      WHEN CC.M_DC IN ('E','P','A','I')
      THEN NVL (CC.NPT_INTCHG_AMT_AT_CDE,DD.PRD_AT_INTCHG_AMT)
    END AS INTCHG_AMT_LKP,
    CASE
      WHEN CC.M_DC IN ('E','P','A','I')
      THEN NVL (CC.NPT_ASSMNT_AT_CDE,DD.PRD_ASSMNT_AT_COD)
    END AS ASSMNT_AT_LKP,
    CASE
      WHEN CC.M_PC IN ('E','P','A','I')
      THEN NVL (CC.NPT_PACE, AA.PAC)
    END AS PAC_LKP,
    CASE
      WHEN CC.M_PC IN ('E','P','A','I')
      THEN NVL (CC.NPT_ASSMNT_PITM_AT_CDE, DD.PRD_ASSMNT_PITM_AT)
    END AS PITM_ASSMNT_AT_LKP,
    CASE
      WHEN CC.M_DC IN ('E', 'P', 'A', 'I')
      THEN NVL (CC.NPT_INTCHG_AMT_AT_CDE, DD.PRD_AT_INTCHG_AMT)
      ELSE AA.PRD_AT_INTCHG_AMT
    END INTCHG_AMT_APL_TYP_CDE,
    CASE
      WHEN CC.M_PC IN ('E', 'P', 'A', 'I')
      THEN NVL (CC.NPT_INTCHG_PCT_AT_CDE, DD.PRD_AT_INTCHG_PCT)
      ELSE AA.PRD_AT_INTCHG_PCT
    END AS INTCHG_PCT_APL_TYP_CDE,
    CASE
      WHEN CC.M_DC IN ('E', 'P', 'A', 'I')
      THEN NVL (CC.NPT_ASSMNT_AT_CDE, DD.PRD_ASSMNT_AT_COD)
      ELSE AA.PRD_ASM_DISC_AT_CDE
    END PRD_ASSMNT_AT_CDE,
    CASE
      WHEN CC.M_PC IN ('E', 'P', 'A', 'I')
      THEN NVL (CC.NPT_ASSMNT_PITM_AT_CDE,DD.PRD_ASSMNT_PITM_AT)
      ELSE AA.PRD_ASM_PITM_AT_CDE
    END AS PRD_ASSMNT_PITM_AT_CDE
  FROM MP.MALL AA
  INNER JOIN MAL_BAL B
  ON AA.DATE  = B.DATE
  AND AA.MNO     = B.MNO
  AND AA.BILL_TYP = 'MD'
  AND AA.CDNO     = B.CDNO
  AND AA.CGNO     = B.CGNO
  LEFT OUTER JOIN MP.M_T1 BB
  ON AA.MNO = BB.MNO
  LEFT OUTER JOIN MP.N_T1CC
  ON AA.CLG_NUM      = CC.CLG_CF_NUM
  AND AA.CDNO     = CC.CDNO
  AND AA.CGNO     = CC.CGNO
  AND BB.PMF_NPP_NUM = CC.NPP_NUM
  LEFT OUTER JOIN MP.P_T1 DD
  ON AA.CLNO  = DD.CLG_CF_NUM
  AND AA.CDNO = DD.CDNO
  AND AA.CGNO = DD.CGNO
  ),
  APL_CD AS
  (SELECT
    /*+parallel(2)*/
    DATE,
    MNO,
    CDNO,
    CGNO,
    BRT,
    BRTD,
    CASE
      WHEN M_DC NOT IN ('E','P','A','I')
      THEN DAC
      WHEN DAC_LKP = 'D'
      THEN (
        CASE
          WHEN CTD_DB_AMT = 0
          THEN (
            CASE
              WHEN DAC_LKP = INTCHG_AMT_LKP
              THEN ASSMNT_AT_LKP
              WHEN DAC_LKP <> INTCHG_AMT_LKP
              THEN INTCHG_AMT_LKP
            END)
          WHEN CTD_DB_AMT <> 0
          THEN (
            CASE
              WHEN DAC_LKP = INTCHG_AMT_LKP
              THEN DAC_LKP
              WHEN DAC_LKP <> INTCHG_AMT_LKP
              THEN INTCHG_AMT_LKP
            END)
        END)
      WHEN DAC_LKP = 'C'
      THEN (
        CASE
          WHEN CTD_CR_AMT = 0
          THEN (
            CASE
              WHEN DAC_LKP = INTCHG_AMT_LKP
              THEN ASSMNT_AT_LKP
              WHEN DAC_LKP <> INTCHG_AMT_LKP
              THEN INTCHG_AMT_LKP
            END)
          WHEN CTD_CR_AMT <> 0
          THEN (
            CASE
              WHEN DAC_LKP = INTCHG_AMT_LKP
              THEN DAC_LKP
              WHEN DAC_LKP <> INTCHG_AMT_LKP
              THEN INTCHG_AMT_LKP
            END)
        END)
      WHEN DAC_LKP NOT IN ('D', 'C')
      THEN DAC_LKP
    END 
    DAC,
    -- PITM APPLY TYPE CODE
    CASE
      WHEN M_DC NOT IN ('E','P','A','I')
      THEN PAC
      WHEN PAC_LKP = 'D'
      THEN (
        CASE
          WHEN CTD_DB_NUM = 0
          THEN (
            CASE
              WHEN PAC_LKP = INTCHG_AMT_LKP
              THEN PITM_ASSMNT_AT_LKP
              WHEN PAC_LKP <> INTCHG_AMT_LKP
              THEN INTCHG_AMT_LKP
            END)
          WHEN CTD_DB_NUM <> 0
          THEN (
            CASE
              WHEN PAC_LKP = INTCHG_AMT_LKP
              THEN PITM_ASSMNT_AT_LKP
              WHEN PAC_LKP <> INTCHG_AMT_LKP
              THEN INTCHG_AMT_LKP
            END)
        END)
      WHEN PAC_LKP = 'C'
      THEN (
        CASE
          WHEN CTD_CR_NUM = 0
          THEN (
            CASE
              WHEN PAC_LKP = INTCHG_AMT_LKP
              THEN ASSMNT_AT_LKP
              WHEN PAC_LKP <> INTCHG_AMT_LKP
              THEN INTCHG_AMT_LKP
            END)
          WHEN CTD_CR_NUM <> 0
          THEN (
            CASE
              WHEN PAC_LKP = INTCHG_AMT_LKP
              THEN PAC_LKP
              WHEN PAC_LKP <> INTCHG_AMT_LKP
              THEN INTCHG_AMT_LKP
            END)
        END)
      WHEN PAC_LKP NOT IN ('D', 'C')
      THEN PAC_LKP
    END 
    PAC,
    INTCHG_AMT_APL_TYP_CDE,
    INTCHG_PCT_APL_TYP_CDE,
    PRD_ASSMNT_AT_CDE,
    PRD_ASSMNT_PITM_AT_CDE
  FROM A_LKUP
  ),
  MAL_BAL_MALL AS
  (SELECT
    /*+parallel(2) */
    A.DATE,
    A.CLNO,
    A.CDNO,
    A.CGNO,
    A.MNO,
    B.BRT,
    B.BRTD,
    B.BDR,
    A.CPT_ID,
    A.CTD_DB_AMT,
    A.CTD_CR_AMT,
    A.CTD_DB_NUM,
    A.CTD_CR_NUM,
    TRUNC (
    CASE
      WHEN A.CTD_FEE_DSCNT_AMT = 0
      THEN 0
      WHEN A.CTD_FEE_DSCNT_AMT <> 0
      THEN (
        CASE
          WHEN C.DAC               = 'N'
          AND (A.CTD_DB_AMT - A.CTD_CR_AMT) = 0
          THEN 0
          WHEN C.DAC                = 'N'
          AND (A.CTD_DB_AMT             - A.CTD_CR_AMT) <> 0
          THEN ( ( B.BT_DR_AMT - B.BT_CR_AMT) / (A.CTD_DB_AMT - A.CTD_CR_AMT)) * A.CTD_FEE_DSCNT_AMT
          WHEN C.DAC               = 'G'
          AND (A.CTD_DB_AMT + A.CTD_CR_AMT) = 0
          THEN 0
          WHEN C.DAC                = 'G'
          AND (A.CTD_DB_AMT             + A.CTD_CR_AMT) <> 0
          THEN ( ( B.BT_DR_AMT + B.BT_CR_AMT) / (A.CTD_DB_AMT + A.CTD_CR_AMT)) * A.CTD_FEE_DSCNT_AMT
          WHEN C.DAC = 'C'
          AND A.CTD_CR_AMT    = 0
          THEN 0
          WHEN C.DAC = 'C'
          AND A.CTD_CR_AMT   <> 0
          THEN (B.BT_CR_AMT / A.CTD_CR_AMT) * A.CTD_FEE_DSCNT_AMT
          WHEN C.DAC = 'D'
          AND A.CTD_DB_AMT    = 0
          THEN 0
          WHEN C.DAC = 'D'
          AND A.CTD_DB_AMT   <> 0
          THEN (B.BT_DR_AMT / A.CTD_DB_AMT) * A.CTD_FEE_DSCNT_AMT
          ELSE 0
        END)
    END, 6) BAT_PRD_FEE_DSCNT_AMT,
    TRUNC (
    CASE
      WHEN A.CTD_FEE_PITM_AMT = 0
      THEN 0
      WHEN A.CTD_FEE_PITM_AMT <> 0
      THEN (
        CASE
          WHEN C.PAC                = 'N'
          AND (A.CTD_DB_NUM - A.CTD_CR_NUM) = 0
          THEN 0
          WHEN C.PAC                 = 'N'
          AND (A.CTD_DB_NUM             - A.CTD_CR_NUM) <> 0
          THEN ( ( B.BT_DR_NUM - B.BT_CR_NUM) / (A.CTD_DB_NUM - A.CTD_CR_NUM)) * A.CTD_FEE_PITM_AMT
          WHEN C.PAC                = 'G'
          AND (A.CTD_DB_NUM + A.CTD_CR_NUM) = 0
          THEN 0
          WHEN C.PAC                 = 'G'
          AND (A.CTD_DB_NUM             + A.CTD_CR_NUM) <> 0
          THEN ( ( B.BT_DR_NUM + B.BT_CR_NUM) / (A.CTD_DB_NUM + A.CTD_CR_NUM)) * A.CTD_FEE_PITM_AMT
          WHEN C.PAC = 'C'
          AND A.CTD_CR_NUM   = 0
          THEN 0
          WHEN C.PAC = 'C'
          AND A.CTD_CR_NUM  <> 0
          THEN (B.BT_CR_NUM / A.CTD_CR_NUM) * A.CTD_FEE_PITM_AMT
          WHEN C.PAC = 'D'
          AND A.CTD_DB_NUM   = 0
          THEN 0
          WHEN C.PAC = 'D'
          AND A.CTD_DB_NUM  <> 0
          THEN (B.BT_DR_NUM / A.CTD_DB_NUM) * A.CTD_FEE_PITM_AMT
          ELSE 0
        END)
    END, 6) BAT_PRD_FEE_PITM_AMT,
    TRUNC (
    CASE
      WHEN A.CTD_INTCHG_AMT = 0
      THEN 0
      WHEN A.CTD_INTCHG_AMT <> 0
      THEN (
        CASE
          WHEN C.INTCHG_AMT_APL_TYP_CDE     = 'N'
          AND (A.CTD_DB_AMT - A.CTD_CR_AMT) = 0
          THEN 0
          WHEN C.INTCHG_AMT_APL_TYP_CDE      = 'N'
          AND (A.CTD_DB_AMT             - A.CTD_CR_AMT) <> 0
          THEN ( ( B.BT_DR_AMT - B.BT_CR_AMT) / (A.CTD_DB_AMT - A.CTD_CR_AMT)) * A.CTD_INTCHG_AMT
          WHEN C.INTCHG_AMT_APL_TYP_CDE     = 'G'
          AND (A.CTD_DB_AMT + A.CTD_CR_AMT) = 0
          THEN 0
          WHEN C.INTCHG_AMT_APL_TYP_CDE      = 'G'
          AND (A.CTD_DB_AMT             + A.CTD_CR_AMT) <> 0
          THEN ( ( B.BT_DR_AMT + B.BT_CR_AMT) / (A.CTD_DB_AMT + A.CTD_CR_AMT)) * A.CTD_INTCHG_AMT
          WHEN C.INTCHG_AMT_APL_TYP_CDE = 'C'
          AND A.CTD_CR_AMT              = 0
          THEN 0
          WHEN C.INTCHG_AMT_APL_TYP_CDE = 'C'
          AND A.CTD_CR_AMT             <> 0
          THEN (B.BT_CR_AMT / A.CTD_CR_AMT) * A.CTD_INTCHG_AMT
          WHEN C.INTCHG_AMT_APL_TYP_CDE = 'D'
          AND A.CTD_DB_AMT              = 0
          THEN 0
          WHEN C.INTCHG_AMT_APL_TYP_CDE = 'D'
          AND A.CTD_DB_AMT             <> 0
          THEN (B.BT_DR_AMT / A.CTD_DB_AMT) * A.CTD_INTCHG_AMT
          ELSE 0
        END)
    END, 6) BAT_PRD_INTCHG_AMT,
    B.BT_DR_AMT,
    B.BT_CR_AMT,
    B.BT_DR_NUM,
    B.BT_CR_NUM,
    CTD_FEE_DSCNT_AMT,
    CTD_FEE_PITM_AMT,
    CTD_INTCHG_AMT,
    CTD_FEE_AMT,
    SUM (b.BT_DR_AMT) OVER (PARTITION BY a.MNO, a.CDNO, a.CGNO, a.CTD_DB_AMT, a.CTD_CR_AMT) AS S_BT_DR_AMT,
    SUM (b.BT_CR_AMT) OVER (PARTITION BY a.MNO, a.CDNO, a.CGNO, a.CTD_DB_AMT, a.CTD_CR_AMT) AS S_BT_CR_AMT,
    'N'                                                                                                        AS RCLS_FLG,
    C.DAC,
    C.PAC,
    C.INTCHG_AMT_APL_TYP_CDE,
    C.INTCHG_PCT_APL_TYP_CDE,
    C.PRD_ASSMNT_AT_CDE,
    C.PRD_ASSMNT_PITM_AT_CDE
  FROM MP.MALL A,
    MAL_BAL B,
    APL_CD C
  WHERE A.DATE = B.DATE
  AND A.MNO       = B.MNO
  AND A.CDNO       = B.CDNO
  AND A.CGNO       = B.CGNO
  AND B.DATE   = C.DATE(+)
  AND B.MNO       = C.MNO(+)
  AND B.CDNO       = C.CDNO(+)
  AND B.CGNO       = C.CGNO(+)
  AND B.BRT       = C.BRT(+)
  AND B.BRTD   = C.BRTD(+)
  AND A.BILL_TYP   = 'MD'
  AND A.CPT_ID NOT LIKE '%CF%'
  AND A.DATE = TO_DATE ('20170131', 'YYYYMMDD')
  AND A.CPT_ID NOT LIKE 'BTCH'
  )
  --AND A.CLNO NOT IN (5, 6)),

SELECT A.DATE,
  A.CDNO,
  A.CGNO,
  A.MNO,
  A.BRT,
  A.BRTD,
  A.BDR,
  A.CLNO,
  A.CPT_ID,
  A.DAC DACE,
  A.PAC DSCNT_PACE,
  A.INTCHG_PCT_APL_TYP_CDE,
  A.INTCHG_AMT_APL_TYP_CDE,
  A.PRD_ASSMNT_AT_CDE,
  A.PRD_ASSMNT_PITM_AT_CDE,
  A.BT_DR_AMT BP_DB_AMT,
  A.BT_CR_AMT BP_CR_AMT,
  A.BT_DR_NUM BP_DB_NUM,
  A.BT_CR_NUM BP_CR_NUM,
  A.BP_FEE_DSCNT_AMT,
  A.BP_FEE_PITM_AMT,
  A.BP_INTCHG_AMT,
  NVL (B.BP_FEE_ASSMNT_AMT, 0) BP_FEE_ASSMNT_AMT,
  NVL (B.BP_ASSMNT_PITM_AMT, 0) BP_ASSMNT_PITM_AMT,
  NVL (B.BP_ASSMNT_AMT, 0) BP_ASSMNT_AMT,
  NVL (B.CONVNCE_FEE, 0) CONVNCE_FEE,
  NVL (B.SURCHG_AMT, 0) SURCHG_AMT,
  NVL (B.PHRESIA_AMT, 0) PHRESIA_AMT,
  NVL (B.DCC_RBT_AMT, 0) DCC_RBT_AMT,
  A.RCLS_FLG RLCS_FLG
FROM MAL_BAL_MALL A
LEFT OUTER JOIN ABC.BBA B
ON A.DATE  = B.DATE
AND A.MNO     = B.MNO
AND A.CDNO     = B.CDNO
AND A.CGNO     = B.CGNO
AND A.BRT     = B.BRT
AND A.BRTD = B.BRTD
WHERE CTD_DB_AMT = S_BT_DR_AMT
AND CTD_CR_AMT   = S_BT_CR_AMT;

注意:BBA 表有完整的访问计划(我正在研究它的索引),除此之外,如果需要任何其他更改,请告诉我。

执行计划

Plan hash value: 489280008

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                           | Name                        | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |    TQ  |IN-OUT| PQ Distrib |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                                    |                             |     1 |   437 |   643   (1)| 00:00:02 |       |       |        |      |            |
|   1 |  TEMP TABLE TRANSFORMATION                          |                             |       |       |            |          |       |       |        |      |            |
|   2 |   PX COORDINATOR                                    |                             |       |       |            |          |       |       |        |      |            |
|   3 |    PX SEND QC (RANDOM)                              | :TQ10000                    |       |       |            |          |       |       |  Q1,00 | P->S | QC (RAND)  |
|   4 |     LOAD AS SELECT                                  | SYS_TEMP_0FD9D9682_9E3FE5C2 |       |       |            |          |       |       |  Q1,00 | PCWP |            |
|   5 |      NESTED LOOPS                                   |                             |       |       |            |          |       |       |  Q1,00 | PCWP |            |
|   6 |       NESTED LOOPS                                  |                             |     1 |   111 |   633   (1)| 00:00:02 |       |       |  Q1,00 | PCWP |            |
|   7 |        PX BLOCK ITERATOR                            |                             |     1 |    65 |   631   (1)| 00:00:02 |     1 |     4 |  Q1,00 | PCWC |            |
|*  8 |         TABLE ACCESS STORAGE FULL                   | POST                        |     1 |    65 |   631   (1)| 00:00:02 |     1 |1048575|  Q1,00 | PCWP |            |
|   9 |        PARTITION RANGE AND                          |                             |     1 |       |     1   (0)| 00:00:01 |KEY(AP)|KEY(AP)|  Q1,00 | PCWP |            |
|* 10 |         INDEX RANGE SCAN                            | BALL_PK                     |     1 |       |     1   (0)| 00:00:01 |KEY(AP)|KEY(AP)|  Q1,00 | PCWP |            |
|  11 |       TABLE ACCESS BY LOCAL INDEX ROWID             | BALL                        |     1 |    46 |     2   (0)| 00:00:01 |     1 |     1 |  Q1,00 | PCWP |            |
|  12 |   PX COORDINATOR                                    |                             |       |       |            |          |       |       |        |      |            |
|  13 |    PX SEND QC (RANDOM)                              | :TQ20004                    |     1 |   437 |    11  (19)| 00:00:01 |       |       |  Q2,04 | P->S | QC (RAND)  |
|* 14 |     HASH JOIN OUTER                                 |                             |     1 |   437 |    11  (19)| 00:00:01 |       |       |  Q2,04 | PCWP |            |
|* 15 |      VIEW                                           |                             |     1 |   276 |     8  (13)| 00:00:01 |       |       |  Q2,04 | PCWP |            |
|  16 |       WINDOW SORT                                   |                             |     1 |   397 |     8  (13)| 00:00:01 |       |       |  Q2,04 | PCWP |            |
|  17 |        PX RECEIVE                                   |                             |     1 |   397 |     7   (0)| 00:00:01 |       |       |  Q2,04 | PCWP |            |
|  18 |         PX SEND HASH                                | :TQ20002                    |     1 |   397 |     7   (0)| 00:00:01 |       |       |  Q2,02 | P->P | HASH       |
|* 19 |          HASH JOIN OUTER BUFFERED                   |                             |     1 |   397 |     7   (0)| 00:00:01 |       |       |  Q2,02 | PCWP |            |
|  20 |           PX RECEIVE                                |                             |       |       |            |          |       |       |  Q2,02 | PCWP |            |
|  21 |            PX SEND HASH                             | :TQ20000                    |       |       |            |          |       |       |  Q2,00 | P->P | HASH       |
|  22 |             NESTED LOOPS                            |                             |       |       |            |          |       |       |  Q2,00 | PCWP |            |
|  23 |              NESTED LOOPS                           |                             |     1 |   303 |     3   (0)| 00:00:01 |       |       |  Q2,00 | PCWP |            |
|* 24 |               VIEW                                  |                             |     1 |   135 |     2   (0)| 00:00:01 |       |       |  Q2,00 | PCWP |            |
|  25 |                PX BLOCK ITERATOR                    |                             |     1 |    89 |     2   (0)| 00:00:01 |       |       |  Q2,00 | PCWC |            |
|  26 |                 TABLE ACCESS STORAGE FULL           | SYS_TEMP_0FD9D9682_9E3FE5C2 |     1 |    89 |     2   (0)| 00:00:01 |       |       |  Q2,00 | PCWP |            |
|  27 |               PARTITION RANGE SINGLE                |                             |     1 |       |     1   (0)| 00:00:01 |  2774 |  2774 |  Q2,00 | PCWP |            |
|* 28 |                INDEX UNIQUE SCAN                    | MALL_PK                     |     1 |       |     1   (0)| 00:00:01 |  2774 |  2774 |  Q2,00 | PCWP |            |
|* 29 |              TABLE ACCESS BY LOCAL INDEX ROWID      | MALL                        |     1 |   168 |     1   (0)| 00:00:01 |  2774 |  2774 |  Q2,00 | PCWP |            |
|  30 |           PX RECEIVE                                |                             |     1 |    94 |     4   (0)| 00:00:01 |       |       |  Q2,02 | PCWP |            |
|  31 |            PX SEND HASH                             | :TQ20001                    |     1 |    94 |     4   (0)| 00:00:01 |       |       |  Q2,01 | P->P | HASH       |
|  32 |             VIEW                                    |                             |     1 |    94 |     4   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  33 |              NESTED LOOPS OUTER                     |                             |     1 |   228 |     4   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  34 |               NESTED LOOPS OUTER                    |                             |     1 |   183 |     3   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  35 |                VIEW                                 |                             |     1 |   164 |     3   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  36 |                 NESTED LOOPS OUTER                  |                             |     1 |   217 |     3   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  37 |                  NESTED LOOPS                       |                             |     1 |   205 |     3   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|* 38 |                   VIEW                              |                             |     1 |    70 |     2   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  39 |                    PX BLOCK ITERATOR                |                             |     1 |    89 |     2   (0)| 00:00:01 |       |       |  Q2,01 | PCWC |            |
|  40 |                     TABLE ACCESS STORAGE FULL       | SYS_TEMP_0FD9D9682_9E3FE5C2 |     1 |    89 |     2   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  41 |                   PARTITION RANGE SINGLE            |                             |     1 |   135 |     1   (0)| 00:00:01 |  2774 |  2774 |  Q2,01 | PCWP |            |
|  42 |                    TABLE ACCESS BY LOCAL INDEX ROWID| MALL                        |     1 |   135 |     1   (0)| 00:00:01 |  2774 |  2774 |  Q2,01 | PCWP |            |
|* 43 |                     INDEX UNIQUE SCAN               | MALL_PK                     |     1 |       |     1   (0)| 00:00:01 |  2774 |  2774 |  Q2,01 | PCWP |            |
|  44 |                  TABLE ACCESS BY INDEX ROWID        | M_T1                        |     1 |    12 |     1   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|* 45 |                   INDEX UNIQUE SCAN                 | M_T1_PK                     |     1 |       |     1   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  46 |                TABLE ACCESS BY INDEX ROWID          | P_T1                        |     1 |    19 |     0   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|* 47 |                 INDEX UNIQUE SCAN                   | P_T1_PK                     |     1 |       |     0   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  48 |               TABLE ACCESS BY INDEX ROWID           | N_T1                        |     1 |    45 |     1   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|* 49 |                INDEX UNIQUE SCAN                    | N_PK                        |     1 |       |     1   (0)| 00:00:01 |       |       |  Q2,01 | PCWP |            |
|  50 |      BUFFER SORT                                    |                             |       |       |            |          |       |       |  Q2,04 | PCWC |            |
|  51 |       PX RECEIVE                                    |                             |     1 |   161 |     2   (0)| 00:00:01 |       |       |  Q2,04 | PCWP |            |
|  52 |        PX SEND BROADCAST                            | :TQ20003                    |     1 |   161 |     2   (0)| 00:00:01 |       |       |  Q2,03 | P->P | BROADCAST  |
|  53 |         PX BLOCK ITERATOR                           |                             |     1 |   161 |     2   (0)| 00:00:01 |       |       |  Q2,03 | PCWC |            |
|  54 |          TABLE ACCESS STORAGE FULL                  | BBA                         |     1 |   161 |     2   (0)| 00:00:01 |       |       |  Q2,03 | PCWP |            |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   8 - filter("A"."DATE"=TO_DATE(' 2017-01-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "A"."BRTD"<=TO_DATE(' 2017-01-31 00:00:00', 'syyyy-mm-dd 
              hh24:mi:ss') AND "A"."BAT_DPST_TYP_CDE"<>'S' AND "A"."BAT_DPST_TYP_CDE"<>'V' AND "A"."BRTD">=TO_DATE(' 2016-12-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
  10 - access("B"."PRCS_CENTRE_CDE"='NA' AND "A"."BRT"="B"."BRT" AND "A"."BRTD"="B"."BRTD")
       filter("B"."BRTD"<=TO_DATE(' 2017-01-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "B"."BRTD">=TO_DATE(' 2016-12-01 00:00:00', 'syyyy-mm-dd 
              hh24:mi:ss') AND "A"."BRTD"="B"."BRTD")
  14 - access("J"."BRTD"="B"."BRTD"(+) AND "J"."BRT"="B"."BRT"(+) AND "J"."CGNO"="B"."CGNO"(+) AND "J"."CDNO"="B"."CDNO"(+) AND 
              "J"."MRNO"="B"."MRNO"(+) AND "J"."DATE"="B"."DATE"(+))
  15 - filter("CTD_DB_AMT"="S_BT_DR_AMT" AND "CTD_CR_AMT"="S_BT_CR_AMT")
  19 - access("B"."DATE"="C"."DATE"(+) AND "B"."MRNO"="C"."MRNO"(+) AND "B"."CDNO"="C"."CDNO"(+) AND "B"."CGNO"="C"."CGNO"(+) AND 
              "B"."BRT"="C"."BRT"(+) AND "B"."BRTD"="C"."BRTD"(+))
  24 - filter("B"."DATE"=TO_DATE(' 2017-01-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
  28 - access("A"."DATE"=TO_DATE(' 2017-01-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "A"."BILL_TYP"='MD' AND "A"."CDNO"="B"."CDNO" AND 
              "A"."CGNO"="B"."CGNO" AND "A"."MRNO"="B"."MRNO")
  29 - filter("A"."CPT_ID" NOT LIKE '%CF%' AND "A"."CPT_ID"<>'BTCH')
  38 - filter("B"."DATE"=TO_DATE(' 2017-01-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
  43 - access("AA"."DATE"=TO_DATE(' 2017-01-31 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "AA"."BILL_TYP"='MD' AND "AA"."CDNO"="B"."CDNO" AND 
              "AA"."CGNO"="B"."CGNO" AND "AA"."MRNO"="B"."MRNO")
  45 - access("AA"."MRNO"="BB"."MRNO"(+))
  47 - access("from$_subquery$_008"."CLG_NUM"="DD"."CLG_CF_NUM"(+) AND "from$_subquery$_008"."QCSJ_C000000000600004"="DD"."CDNO"(+) AND 
              "from$_subquery$_008"."QCSJ_C000000000600006"="DD"."CGNO"(+))
  49 - access("AA"."CLNO"="CC"."CLG_CF_NUM"(+) AND "BB"."PMF_NPP_NUM"="CC"."NPP_NUM"(+) AND "AA"."CDNO"="CC"."CDNO"(+) AND "AA"."CGNO"="CC"."CGNO"(+))

Note
-----
   - dynamic sampling used for this statement (level=5)
   - Degree of Parallelism is 2 because of hint

【问题讨论】:

你在说什么“低于 SQL 查询”? 您使用的是哪个 dbms? 为什么需要优化它?它是否运行缓慢,如果是,需要多长时间以及表中有多少数据?您是否收集了有关表格的统计信息?解释计划中有很多Rows = 1,这通常是统计数据过时或缺失的标志。 【参考方案1】:

解决 SQL 查询问题的第一步是简单地删除所有 优化器提示(在您的情况下为PARALLEL)并重新运行查询以查看提示是否没有导致 性能问题。

基本上,在不了解基本机制和含义的情况下对单个查询进行故障排除的可能性非常有限 执行计划的操作。

幸运的是,网络上有大量可用资源。

我建议使用DBMS_SQLTUNE.REPORT_SQL_MONITOR 观察查询的执行情况

您将了解在哪里,即在哪个操作中消耗了数据库时间以及优化如何准确估计肉体 (通过实际行数估计行数的差异)。

为了解决问题,您还可以尝试简化查询(检查其他人在 SO 上发布的查询有多少行),以便提高性能 问题依然存在。

【讨论】:

以上是关于查询优化或调优的主要内容,如果未能解决你的问题,请参考以下文章

TiDB 查询优化及调优系列查询执行计划的调整及优化原理

MySQL调优查询优化

TiDB 查询优化及调优系列TiDB 优化器简介

MYSQL性能调优06_分页查询优化JOIN关联查询优化in和exsits优化count(*)查询优化

MySQL调优--05---多表查询优化子查询优化 ORDER BY优化GROUP BY优化分页查询优化

TiDB 查询优化及调优系列TiDB 查询计划简介