Oracle19c中SQL分开跑很快UNION ALL之后变慢分析

Posted robinson1988

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle19c中SQL分开跑很快UNION ALL之后变慢分析相关的知识,希望对你有一定的参考价值。

下面SQL要跑120秒

INSERT INTO CUST_GENERAL.NAP_VENDOR_WITH_LINE_TEMP
  (ORG_NAME,
   DEPT_NAME,
   VENDOR_NAME,
   CURRENCY_CODE,
   GL_DATE,
   CHECK_AMOUNT,
   TRX_AMOUNT,
   TRX_TYPE,
   INVOICE_NUM,
   CHECK_TYPE,
   CHECK_NUM,
   INVOICE_ID,
   PO_PLAN_NUM,
   DESCRIPTION,
   PROJECT_NUM,
   CNO,
   PAY_AMOUNT,
   DATA_TYPE,
   PARTY_ID,
   VENDOR_ID,
   ORG_ID,
   CHECK_ID)
  SELECT 
   ORG_NAME,
   DEPT_NAME,
   VENDOR_NAME,
   CURRENCY_CODE,
   GL_DATE,
   SUM(CHECK_AMOUNT) CHECK_AMOUNT,
   SUM(TRX_AMOUNT) TRX_AMOUNT,
   TRX_TYPE,
   INVOICE_NUM,
   CHECK_TYPE,
   CHECK_NUM,
   INVOICE_ID,
   PO_PLAN_NUM,
   DESCRIPTION,
   DECODE('N', 'Y', PROJECT_NUM, NULL) PROJECT_NUM,
   CNO,
   PAY_AMOUNT,
   DATA_TYPE,
   PARTY_ID,
   VENDOR_ID,
   ORG_ID,
   CHECK_ID
    FROM (SELECT HR.NAME ORG_NAME,
                 NPO_COMMON_PUBLIC_PKG.GET_FLEX_DESC('XY_COA_CC',
                                                     AP.ATTRIBUTE15) DEPT_NAME,
                 HP.PARTY_NAME VENDOR_NAME,
                 AP.INVOICE_CURRENCY_CODE CURRENCY_CODE,
                 AP.GL_DATE,
                 NULL CHECK_AMOUNT,
                 SUM(DECODE(AP.CANCELLED_DATE,
                            NULL,
                            TBL.AMOUNT + NVL(TBL.TAX_AMOUNT, 0),
                            TBL.CANCELLATION_AMOUNT + NVL(TBL.TAX_AMOUNT, 0))) TRX_AMOUNT,
                 AP_CODE.DISPLAYED_FIELD TRX_TYPE,
                 AP.INVOICE_NUM,
                 NULL CHECK_TYPE,
                 NULL CHECK_NUM,
                 AP.INVOICE_ID,
                 NULL PO_PLAN_NUM,
                 AP.DESCRIPTION,
                 TBL.PROJECT_NUM,
                 1 CNO,
                 NULL PAY_AMOUNT,
                 'INVOICE' DATA_TYPE,
                 AP.PARTY_ID,
                 AP.VENDOR_ID,
                 AP.ORG_ID,
                 NULL CHECK_ID
            FROM AP_INVOICES_ALL AP,
                 HR_OPERATING_UNITS HR,
                 AP_LOOKUP_CODES AP_CODE,
                 HZ_PARTIES HP,
                 (SELECT T.INVOICE_DISTRIBUTION_ID,
                         T.INVOICE_ID,
                         T.CANCELLED_FLAG,
                         T.AMOUNT,
                         DECODE(T.CANCELLATION_FLAG, 'Y', 0, T.AMOUNT) CANCELLATION_AMOUNT,
                         (SELECT SUM(T1.AMOUNT)
                            FROM AP.AP_INVOICE_DISTRIBUTIONS_ALL T1
                           WHERE 1 = 1
                             AND T1.CHARGE_APPLICABLE_TO_DIST_ID =
                                 T.INVOICE_DISTRIBUTION_ID
                             AND T1.ORG_ID = T.ORG_ID
                             AND (T1.CANCELLATION_FLAG = 'N' OR
                                 T1.CANCELLATION_FLAG IS NULL)) TAX_AMOUNT,
                         (SELECT PA.SEGMENT1
                            FROM PA_PROJECTS_ALL PA
                           WHERE PA.PROJECT_ID = TO_NUMBER(AIL.ATTRIBUTE5)) PROJECT_NUM
                    FROM AP.AP_INVOICE_DISTRIBUTIONS_ALL T,
                         AP_INVOICE_LINES_ALL            AIL
                   WHERE AIL.INVOICE_ID = T.INVOICE_ID
                     AND AIL.LINE_NUMBER = T.INVOICE_LINE_NUMBER
                     AND T.ORG_ID = AIL.ORG_ID
                     AND MO_GLOBAL.CHECK_ACCESS(AIL.ORG_ID) = 'Y'
                     AND T.LINE_TYPE_LOOKUP_CODE NOT IN
                         ('REC_TAX', 'NONREC_TAX', 'PREPAY')
                     AND EXISTS
                   (SELECT 1
                            FROM AP_INVOICES_ALL AIA
                           WHERE AIA.ORG_ID = NVL(99, AIA.ORG_ID)
                             AND AIA.INVOICE_TYPE_LOOKUP_CODE IN
                                 ('STANDARD', 'CREDIT')
                             AND AIA.INVOICE_CURRENCY_CODE =
                                 NVL(null, AIA.INVOICE_CURRENCY_CODE)
                             AND AIA.GL_DATE >= to_date('2022-01-01','yyyy-mm-dd')
                             AND AIA.GL_DATE <= to_date('2022-10-01','yyyy-mm-dd')
                             AND (AIA.VENDOR_ID = null OR null IS NULL)
                             AND AIA.INVOICE_ID = AIL.INVOICE_ID)) TBL
           WHERE AP.ORG_ID = HR.ORGANIZATION_ID
             AND AP.PARTY_ID = HP.PARTY_ID
             AND AP_CODE.LOOKUP_CODE = AP.INVOICE_TYPE_LOOKUP_CODE
             AND AP_CODE.LOOKUP_TYPE = 'INVOICE TYPE'
             AND AP.INVOICE_TYPE_LOOKUP_CODE IN ('STANDARD', 'CREDIT')
             AND AP.ORG_ID = NVL(99, AP.ORG_ID)
             AND EXISTS (SELECT 1
                    FROM NCM_COMMON_GT_TMP NCGT
                   WHERE NCGT.CHAR1 = AP.ATTRIBUTE15)
             AND (AP.ATTRIBUTE15 = '30034170' OR '30034170' IS NULL)
             AND AP.INVOICE_CURRENCY_CODE =
                 NVL(null, AP.INVOICE_CURRENCY_CODE)
             AND AP.GL_DATE >= to_date('2022-01-01','yyyy-mm-dd')
             AND AP.GL_DATE <= to_date('2022-10-01','yyyy-mm-dd')
             AND (AP.VENDOR_ID = null OR null IS NULL)
             AND MO_GLOBAL.CHECK_ACCESS(AP.ORG_ID) = 'Y'
             AND AP.INVOICE_ID = TBL.INVOICE_ID
             AND NOT EXISTS
           (SELECT AP.INVOICE_NUM
                    FROM AP_INVOICE_DISTRIBUTIONS_ALL APD,
                         AP_INVOICE_LINES_ALL         APL,
                         GL_CODE_COMBINATIONS         GCC
                   WHERE APL.INVOICE_ID = AP.INVOICE_ID
                     AND APL.INVOICE_ID = APD.INVOICE_ID
                     AND APL.LINE_NUMBER = APD.INVOICE_LINE_NUMBER
                     AND APD.DIST_CODE_COMBINATION_ID =
                         GCC.CODE_COMBINATION_ID
                     AND APD.ORG_ID = AP.ORG_ID
                     AND SUBSTR(GCC.SEGMENT3, 1, 1) = '9'
                     AND APD.AMOUNT <> 0
                     AND APD.LINE_TYPE_LOOKUP_CODE IN
                         ('ITEM', 'FREIGHT', 'ACCRUAL'))
             AND NAP_VENDOR_WITH_PKG.GET_CANCEL(AP.INVOICE_ID,
                                                AP.GL_DATE,
                                                AP.CANCELLED_DATE) = 'N'
           GROUP BY HR.NAME,
                    AP.ATTRIBUTE15,
                    HP.PARTY_NAME,
                    AP.INVOICE_CURRENCY_CODE,
                    AP.GL_DATE,
                    AP_CODE.DISPLAYED_FIELD,
                    AP.INVOICE_NUM,
                    AP.INVOICE_ID,
                    AP.DESCRIPTION,
                    TBL.PROJECT_NUM,
                    AP.PARTY_ID,
                    AP.VENDOR_ID,
                    AP.ORG_ID
          UNION ALL
          SELECT HR.NAME ORG_NAME,
                 NPO_COMMON_PUBLIC_PKG.GET_FLEX_DESC('XY_COA_CC',
                                                     AP.ATTRIBUTE15) DEPT_NAME,
                 HP.PARTY_NAME VENDOR_NAME,
                 AP.INVOICE_CURRENCY_CODE CURRENCY_CODE,
                 AP.GL_DATE,
                 NULL CHECK_AMOUNT,
                 SUM(DECODE(AP.CANCELLED_DATE,
                            NULL,
                            AIL.AMOUNT,
                            NAP_VENDOR_WITH_PKG.GET_INVOICE_LINE_AMOUNT(AP.INVOICE_ID))) TRX_AMOUNT,
                 AP_CODE.DISPLAYED_FIELD TRX_TYPE,
                 AP.INVOICE_NUM,
                 NULL CHECK_TYPE,
                 NULL CHECK_NUM,
                 AP.INVOICE_ID,
                 NULL PO_PLAN_NUM,
                 AP.DESCRIPTION,
                 (SELECT PA.SEGMENT1
                    FROM PA_PROJECTS_ALL PA
                   WHERE PA.PROJECT_ID = TO_NUMBER(AIL.ATTRIBUTE5)) PROJECT_NUM,
                 1 CNO,
                 NULL PAY_AMOUNT,
                 'INVOICE' DATA_TYPE,
                 AP.PARTY_ID,
                 AP.VENDOR_ID,
                 AP.ORG_ID,
                 NULL CHECK_ID
            FROM AP_INVOICES_ALL      AP,
                 HR_OPERATING_UNITS   HR,
                 AP_LOOKUP_CODES      AP_CODE,
                 HZ_PARTIES           HP,
                 AP_INVOICE_LINES_ALL AIL,
                 GL_CODE_COMBINATIONS GC
           WHERE AP.ORG_ID = HR.ORGANIZATION_ID
             AND AP.PARTY_ID = HP.PARTY_ID
             AND AP_CODE.LOOKUP_CODE = AP.INVOICE_TYPE_LOOKUP_CODE
             AND AP_CODE.LOOKUP_TYPE = 'INVOICE TYPE'
             AND AP.INVOICE_TYPE_LOOKUP_CODE IN ('STANDARD', 'CREDIT')
             AND AP.ORG_ID = NVL(99, AP.ORG_ID)
             AND EXISTS
           (SELECT 1
                    FROM NCM_COMMON_GT_TMP NCGT
                   WHERE NCGT.CHAR1 = AP.ATTRIBUTE15)
             AND (AP.ATTRIBUTE15 = '30034170' OR '30034170' IS NULL)
             AND AP.INVOICE_CURRENCY_CODE =
                 NVL(null, AP.INVOICE_CURRENCY_CODE)
             AND AP.GL_DATE >= to_date('2022-01-01','yyyy-mm-dd')
             AND AP.GL_DATE <= to_date('2022-10-01','yyyy-mm-dd')
             AND (AP.VENDOR_ID = null OR null IS NULL)
             AND MO_GLOBAL.CHECK_ACCESS(AP.ORG_ID) = 'Y'
             AND AIL.INVOICE_ID = AP.INVOICE_ID
             AND AIL.LINE_TYPE_LOOKUP_CODE IN ('ITEM', 'FREIGHT')
             AND DECODE(AP.CANCELLED_DATE,
                        NULL,
                        AIL.AMOUNT,
                        NAP_VENDOR_WITH_PKG.GET_INVOICE_LINE_AMOUNT(AP.INVOICE_ID)) <> 0
             AND AIL.DEFAULT_DIST_CCID = GC.CODE_COMBINATION_ID(+)
             AND SUBSTR(GC.SEGMENT3(+), 1, 1) <> '9'
             AND EXISTS
           (SELECT AP.INVOICE_NUM
                    FROM AP_INVOICE_DISTRIBUTIONS_ALL APD,
                         AP_INVOICE_LINES_ALL         APL,
                         GL_CODE_COMBINATIONS         GCC
                   WHERE APL.INVOICE_ID = AP.INVOICE_ID
                     AND APL.INVOICE_ID = APD.INVOICE_ID
                     AND APL.LINE_NUMBER = APD.INVOICE_LINE_NUMBER
                     AND APD.DIST_CODE_COMBINATION_ID =
                         GCC.CODE_COMBINATION_ID
                     AND APD.ORG_ID = AP.ORG_ID
                     AND APD.LINE_TYPE_LOOKUP_CODE IN
                         ('ITEM', 'FREIGHT', 'ACCRUAL')
                     AND SUBSTR(GCC.SEGMENT3, 1, 1) = '9'
                     AND APD.AMOUNT <> 0)
             AND EXISTS
           (SELECT AP.INVOICE_NUM
                    FROM AP_INVOICE_DISTRIBUTIONS_ALL APD,
                         AP_INVOICE_LINES_ALL         APL,
                         GL_CODE_COMBINATIONS         GCC
                   WHERE APL.INVOICE_ID = AP.INVOICE_ID
                     AND APL.INVOICE_ID = APD.INVOICE_ID
                     AND APL.LINE_NUMBER = APD.INVOICE_LINE_NUMBER
                     AND APD.DIST_CODE_COMBINATION_ID =
                         GCC.CODE_COMBINATION_ID
                     AND APD.ORG_ID = AP.ORG_ID
                     AND APD.LINE_TYPE_LOOKUP_CODE IN
                         ('ITEM', 'FREIGHT', 'ACCRUAL')
                     AND SUBSTR(GCC.SEGMENT3, 1, 1) <> '9'
                     AND APD.AMOUNT <> 0)
             AND NAP_VENDOR_WITH_PKG.GET_CANCEL(AP.INVOICE_ID,
                                                AP.GL_DATE,
                                                AP.CANCELLED_DATE) = 'N'
           GROUP BY HR.NAME,
                    AP.ATTRIBUTE15,
                    AP.INVOICE_NUM,
                    AP_CODE.DISPLAYED_FIELD,
                    AP.DESCRIPTION,
                    AP.GL_DATE,
                    AP.INVOICE_CURRENCY_CODE,
                    HP.PARTY_NAME,
                    AP.INVOICE_ID,
                    AP.CANCELLED_DATE,
                    AIL.ATTRIBUTE5,
                    AP.PARTY_ID,
                    AP.VENDOR_ID,
                    AP.ORG_ID
          UNION ALL
          SELECT HR.NAME ORG_NAME,
                 NPO_COMMON_PUBLIC_PKG.GET_FLEX_DESC('XY_COA_CC',
                                                     AP.ATTRIBUTE15) DEPT_NAME,
                 HP.PARTY_NAME VENDOR_NAME,
                 AP.INVOICE_CURRENCY_CODE CURRENCY_CODE,
                 NAP_VENDOR_WITH_PKG.GET_CANCEL_DATE(AP.INVOICE_ID,
                                                     AP.CANCELLED_DATE),
                 NULL CHECK_AMOUNT,
                 -SUM((TBL.AMOUNT + NVL(TBL.TAX_AMOUNT, 0))) TRX_AMOUNT,
                 AP_CODE.DISPLAYED_FIELD TRX_TYPE,
                 AP.INVOICE_NUM,
                 NULL CHECK_TYPE,
                 NULL CHECK_NUM,
                 AP.INVOICE_ID,
                 NULL PO_PLAN_NUM,
                 AP.DESCRIPTION,
                 TBL.PROJECT_NUM,
                 1 CNO,
                 NULL PAY_AMOUNT,
                 'INVOICE' DATA_TYPE,
                 AP.PARTY_ID,
                 AP.VENDOR_ID,
                 AP.ORG_ID,
                 NULL CHECK_ID
            FROM AP_INVOICES_ALL AP,
                 HR_OPERATING_UNITS HR,
                 AP_LOOKUP_CODES AP_CODE,
                 HZ_PARTIES HP,
                 (SELECT T.INVOICE_DISTRIBUTION_ID,
                         T.INVOICE_ID,
                         T.CANCELLED_FLAG,
                         SUM(DECODE(T.CANCELLATION_FLAG, 'Y', 0, T.AMOUNT)) AMOUNT,
                         (SELECT SUM(T1.AMOUNT)
                            FROM AP.AP_INVOICE_DISTRIBUTIONS_ALL T1
                           WHERE 1 = 1
                             AND T1.CHARGE_APPLICABLE_TO_DIST_ID =
                                 T.INVOICE_DISTRIBUTION_ID
                             AND (T1.CANCELLATION_FLAG = 'N' OR
                                 T1.CANCELLATION_FLAG IS NULL)) TAX_AMOUNT,
                         (SELECT PA.SEGMENT1
                            FROM PA_PROJECTS_ALL PA
                           WHERE PA.PROJECT_ID = TO_NUMBER(AIL.ATTRIBUTE5)) PROJECT_NUM
                    FROM AP.AP_INVOICE_DISTRIBUTIONS_ALL T,
                         AP_INVOICE_LINES_ALL            AIL
                   WHERE AIL.INVOICE_ID = T.INVOICE_ID
                     AND AIL.LINE_NUMBER = T.INVOICE_LINE_NUMBER
                     AND MO_GLOBAL.CHECK_ACCESS(AIL.ORG_ID) = 'Y'
                     AND T.LINE_TYPE_LOOKUP_CODE NOT IN
                         ('REC_TAX', 'NONREC_TAX', 'PREPAY')
                     AND EXISTS
                   (SELECT 1
                            FROM AP_INVOICES_ALL AIA
                           WHERE AIA.ORG_ID = NVL(99, AIA.ORG_ID)
                             AND AIA.INVOICE_TYPE_LOOKUP_CODE IN
                                 ('STANDARD', 'CREDIT')
                             AND AIA.INVOICE_CURRENCY_CODE =
                                 NVL(null, AIA.INVOICE_CURRENCY_CODE)
                             AND NAP_VENDOR_WITH_PKG.GET_CANCEL_DATE(AIA.INVOICE_ID,
                                                                     AIA.CANCELLED_DATE) >= to_date('2022-01-01','yyyy-mm-dd')
                             AND NAP_VENDOR_WITH_PKG.GET_CANCEL_DATE(AIA.INVOICE_ID,
                                                                     AIA.CANCELLED_DATE) <= to_date('2022-10-01','yyyy-mm-dd')
                             AND (AIA.VENDOR_ID = null OR null IS NULL)
                             AND AIA.INVOICE_ID = AIL.INVOICE_ID)
                   GROUP BY T.INVOICE_DISTRIBUTION_ID,
                            T.INVOICE_ID,
                            T.CANCELLED_FLAG,
                            AIL.ATTRIBUTE5) TBL
           WHERE AP.ORG_ID = HR.ORGANIZATION_ID
             AND AP.PARTY_ID = HP.PARTY_ID
             AND AP_CODE.LOOKUP_CODE = AP.INVOICE_TYPE_LOOKUP_CODE
             AND AP_CODE.LOOKUP_TYPE = 'INVOICE TYPE'
             AND AP.INVOICE_TYPE_LOOKUP_CODE IN ('STANDARD', 'CREDIT')
             AND AP.ORG_ID = NVL(99, AP.ORG_ID)
             AND EXISTS (SELECT 1
                    FROM NCM_COMMON_GT_TMP NCGT
                   WHERE NCGT.CHAR1 = AP.ATTRIBUTE15)
             AND (AP.ATTRIBUTE15 = '30034170' OR '30034170' IS NULL)
             AND AP.INVOICE_CURRENCY_CODE =
                 NVL(null, AP.INVOICE_CURRENCY_CODE)
             AND AP.CANCELLED_DATE IS NOT NULL
             AND NAP_VENDOR_WITH_PKG.GET_CANCEL_DATE(AP.INVOICE_ID,
                                                     AP.CANCELLED_DATE) >= to_date('2022-01-01','yyyy-mm-dd')
             AND NAP_VENDOR_WITH_PKG.GET_CANCEL_DATE(AP.INVOICE_ID,
                                                     AP.CANCELLED_DATE) <= to_date('2022-10-01','yyyy-mm-dd')
             AND (AP.VENDOR_ID = null OR null IS NULL)
             AND MO_GLOBAL.CHECK_ACCESS(AP.ORG_ID) = 'Y'
             AND AP.INVOICE_ID = TBL.INVOICE_ID
             AND NOT EXISTS
           (SELECT AP.INVOICE_NUM
                    FROM AP_INVOICE_DISTRIBUTIONS_ALL APD,
                         AP_INVOICE_LINES_ALL         APL,
                         GL_CODE_COMBINATIONS         GCC
                   WHERE APL.INVOICE_ID = AP.INVOICE_ID
                     AND APL.INVOICE_ID = APD.INVOICE_ID
                     AND APL.LINE_NUMBER = APD.INVOICE_LINE_NUMBER
                     AND APD.DIST_CODE_COMBINATION_ID =
                         GCC.CODE_COMBINATION_ID
                     AND APD.ORG_ID = AP.ORG_ID
                     AND SUBSTR(GCC.SEGMENT3, 1, 1) = '9'
                     AND APD.AMOUNT <> 0
                     AND APD.LINE_TYPE_LOOKUP_CODE IN
                         ('ITEM', 'FREIGHT', 'ACCRUAL'))
             AND NAP_VENDOR_WITH_PKG.GET_CANCEL(AP.INVOICE_ID,
                                                AP.GL_DATE,
                                                AP.CANCELLED_DATE) = 'N'
           GROUP BY HR.NAME,
                    AP.ATTRIBUTE15,
                    HP.PARTY_NAME,
                    AP.INVOICE_CURRENCY_CODE,
                    AP.INVOICE_ID,
                    AP.CANCELLED_DATE,
                    AP_CODE.DISPLAYED_FIELD,
                    AP.INVOICE_NUM,
                    AP.INVOICE_ID,
                    AP.DESCRIPTION,
                    TBL.PROJECT_NUM,
                    AP.PARTY_ID,
                    AP.VENDOR_ID,
                    AP.ORG_ID
          UNION ALL
          SELECT HR.NAME ORG_NAME,
                 NPO_COMMON_PUBLIC_PKG.GET_FLEX_DESC('XY_COA_CC',
                                                     AP.ATTRIBUTE15) DEPT_NAME,
                 HP.PARTY_NAME VENDOR_NAME,
                 AP.INVOICE_CURRENCY_CODE CURRENCY_CODE,
                 NAP_VENDOR_WITH_PKG.GET_CANCEL_DATE(AP.INVOICE_ID,
                                                     AP.CANCELLED_DATE),
                 NULL CHECK_AMOUNT,
                 SUM(-NAP_VENDOR_WITH_PKG.GET_INVOICE_LINE_AMOUNT(AP.INVOICE_ID)) TRX_AMOUNT,
                 AP_CODE.DISPLAYED_FIELD TRX_TYPE,
                 AP.INVOICE_NUM,
                 NULL CHECK_TYPE,
                 NULL CHECK_NUM,
                 AP.INVOICE_ID,
                 NULL PO_PLAN_NUM,
                 AP.DESCRIPTION,
                 (SELECT PA.SEGMENT1
                    FROM PA_PROJECTS_ALL PA
                   WHERE PA.PROJECT_ID = TO_NUMBER(AIL.ATTRIBUTE5)) PROJECT_NUM,
                 1 CNO,
                 NULL PAY_AMOUNT,
                 'INVOICE' DATA_TYPE,
                 AP.PARTY_ID,
                 AP.VENDOR_ID,
                 AP.ORG_ID,
                 NULL CHECK_ID
            FROM AP_INVOICES_ALL      AP,
                 HR_OPERATING_UNITS   HR,
                 AP_LOOKUP_CODES      AP_CODE,
                 HZ_PARTIES           HP,
                 AP_INVOICE_LINES_ALL AIL,
                 GL_CODE_COMBINATIONS GC
           WHERE AP.ORG_ID = HR.ORGANIZATION_ID
             AND AP.PARTY_ID = HP.PARTY_ID
             AND AP_CODE.LOOKUP_CODE = AP.INVOICE_TYPE_LOOKUP_CODE
             AND AP_CODE.LOOKUP_TYPE = 'INVOICE TYPE'
             AND AP.INVOICE_TYPE_LOOKUP_CODE IN ('STANDARD', 'CREDIT')
             AND AP.ORG_ID = NVL(99, AP.ORG_ID)
             AND EXISTS
           (SELECT 1
                    FROM NCM_COMMON_GT_TMP NCGT
                   WHERE NCGT.CHAR1 = AP.ATTRIBUTE15)
             AND (AP.ATTRIBUTE15 = '30034170' OR '30034170' IS NULL)
             AND AP.INVOICE_CURRENCY_CODE =
                 NVL(null, AP.INVOICE_CURRENCY_CODE)
             AND AP.CANCELLED_DATE IS NOT NULL
             AND NAP_VENDOR_WITH_PKG.GET_CANCEL_DATE(AP.INVOICE_ID,
                                                     AP.CANCELLED_DATE) >= to_date('2022-01-01','yyyy-mm-dd')
             AND NAP_VENDOR_WITH_PKG.GET_CANCEL_DATE(AP.INVOICE_ID,
                                                     AP.CANCELLED_DATE) <= to_date('2022-10-01','yyyy-mm-dd')
             AND (AP.VENDOR_ID = null OR null IS NULL)
             AND MO_GLOBAL.CHECK_ACCESS(AP.ORG_ID) = 'Y'
             AND AIL.INVOICE_ID = AP.INVOICE_ID
             AND AIL.LINE_TYPE_LOOKUP_CODE IN ('ITEM', 'FREIGHT')
             AND DECODE(AP.CANCELLED_DATE,
                        NULL,
                        AIL.AMOUNT,
                        NAP_VENDOR_WITH_PKG.GET_INVOICE_LINE_AMOUNT(AP.INVOICE_ID)) <> 0
             AND AIL.DEFAULT_DIST_CCID = GC.CODE_COMBINATION_ID(+)
             AND SUBSTR(GC.SEGMENT3(+), 1, 1) <> '9'
             AND EXISTS
           (SELECT AP.INVOICE_NUM
                    FROM AP_INVOICE_DISTRIBUTIONS_ALL APD,
                         AP_INVOICE_LINES_ALL         APL,
                         GL_CODE_COMBINATIONS         GCC
                   WHERE APL.INVOICE_ID = AP.INVOICE_ID
                     AND APL.INVOICE_ID = APD.INVOICE_ID
                     AND APL.LINE_NUMBER = APD.INVOICE_LINE_NUMBER
                     AND APD.DIST_CODE_COMBINATION_ID =
                         GCC.CODE_COMBINATION_ID
                     AND APD.ORG_ID = AP.ORG_ID
                     AND APD.LINE_TYPE_LOOKUP_CODE IN
                         ('ITEM', 'FREIGHT', 'ACCRUAL')
                     AND SUBSTR(GCC.SEGMENT3, 1, 1) = '9'
                     AND APD.AMOUNT <> 0)
             AND EXISTS
           (SELECT AP.INVOICE_NUM
                    FROM AP_INVOICE_DISTRIBUTIONS_ALL APD,
                         AP_INVOICE_LINES_ALL         APL,
                         GL_CODE_COMBINATIONS         GCC
                   WHERE APL.INVOICE_ID = AP.INVOICE_ID
                     AND APL.INVOICE_ID = APD.INVOICE_ID
                     AND APL.LINE_NUMBER = APD.INVOICE_LINE_NUMBER
                     AND APD.DIST_CODE_COMBINATION_ID =
                         GCC.CODE_COMBINATION_ID
                     AND APD.ORG_ID = AP.ORG_ID
                     AND APD.LINE_TYPE_LOOKUP_CODE IN
                         ('ITEM', 'FREIGHT', 'ACCRUAL')
                     AND SUBSTR(GCC.SEGMENT3, 1, 1) <> '9'
                     AND APD.AMOUNT <> 0)
             AND NAP_VENDOR_WITH_PKG.GET_CANCEL(AP.INVOICE_ID,
                                                AP.GL_DATE,
                                                AP.CANCELLED_DATE) = 'N'
           GROUP BY HR.NAME,
                    AP.ATTRIBUTE15,
                    AP.INVOICE_NUM,
                    AP_CODE.DISPLAYED_FIELD,
                    AP.DESCRIPTION,
                    AP.GL_DATE,
                    AP.INVOICE_CURRENCY_CODE,
                    HP.PARTY_NAME,
                    AP.INVOICE_ID,
                    AP.CANCELLED_DATE,
                    AIL.ATTRIBUTE5,
                    AP.PARTY_ID,
                    AP.VENDOR_ID,
                    AP.ORG_ID
          UNION ALL
          SELECT HR.NAME ORG_NAME,
                 NPO_COMMON_PUBLIC_PKG.GET_FLEX_DESC('XY_COA_CC',
                                                     AP.ATTRIBUTE15) DEPT_NAME,
                 HP.PARTY_NAME VENDOR_NAME,
                 AC.CURRENCY_CODE,
                 API.ACCOUNTING_DATE GL_DATE,
                 SUM(DECODE(AP.PAY_CURR_INVOICE_AMOUNT,
                            0,
                            0,
                            ROUND(NAP_VENDOR_WITH_PKG.GET_INVOICE_PROJECT_AMOUNT(AP.INVOICE_ID,
                                                                                 AIL.ATTRIBUTE5) /
                                  AP.PAY_CURR_INVOICE_AMOUNT * API.AMOUNT,
                                  2))) CHECK_AMOUNT,
                 NULL TRX_AMOUNT,
                 AP_CODE.DISPLAYED_FIELD TRX_TYPE,
                 AP.INVOICE_NUM,
                 IBY.PAYMENT_METHOD_NAME CHECK_TYEP,
                 AC.CHECK_NUMBER,
                 AP.INVOICE_ID,
                 AP.ATTRIBUTE2 PO_PLAN_NUM,
                 NAP_VENDOR_WITH_PKG.GET_DESC(API.ACCOUNTING_EVENT_ID) DESCRIPTION,
                 (SELECT PA.SEGMENT1
                    FROM PA_PROJECTS_ALL PA
                   WHERE PA.PROJECT_ID = TO_NUMBER(AIL.ATTRIBUTE5)) PROJECT_NUM,
                 ROW_NUMBER() OVER(PARTITION BY AC.CHECK_NUMBER, API.ACCOUNTING_EVENT_ID ORDER BY AC.CHECK_NUMBER) CNO,
                 API.AMOUNT PAY_AMOUNT,
                 'PAYMENT' DATA_TYPE,
                 AC.PARTY_ID,
                 AC.VENDOR_ID,
                 AC.ORG_ID,
                 AC.CHECK_ID
            FROM AP_CHECKS_ALL           AC,
                 HR_OPERATING_UNITS      HR,
                 HZ_PARTIES              HP,
                 AP_INVOICE_PAYMENTS_ALL API,
                 IBY_PAYMENT_METHODS_VL  IBY,
                 AP_INVOICES_ALL         AP,
                 AP_INVOICE_LINES_ALL    AIL,
                 AP_LOOKUP_CODES         AP_CODE
           WHERE AC.PARTY_ID = HP.PARTY_ID
             AND API.CHECK_ID = AC.CHECK_ID
             AND AC.PAYMENT_METHOD_CODE = IBY.PAYMENT_METHOD_CODE
             AND AC.ORG_ID = HR.ORGANIZATION_ID
             AND API.INVOICE_ID = AP.INVOICE_ID
             AND AP_CODE.LOOKUP_CODE = AP.INVOICE_TYPE_LOOKUP_CODE
             AND AP_CODE.LOOKUP_TYPE = 'INVOICE TYPE'
             AND AP.INVOICE_TYPE_LOOKUP_CODE IN ('STANDARD', 'CREDIT')
             AND (AC.ORG_ID = 99 OR 99 IS NULL)
             AND EXISTS
           (SELECT 1
                    FROM NCM_COMMON_GT_TMP NCGT
                   WHERE NCGT.CHAR1 = AP.ATTRIBUTE15)
             AND (AP.ATTRIBUTE15 = '30034170' OR '30034170' IS NULL)
             AND AC.CURRENCY_CODE = NVL(null, AC.CURRENCY_CODE)
             AND API.ACCOUNTING_DATE >= to_date('2022-01-01','yyyy-mm-dd')
             AND API.ACCOUNTING_DATE <= to_date('2022-10-01','yyyy-mm-dd')
             AND (AC.VENDOR_ID = null OR null IS NULL)
             AND MO_GLOBAL.CHECK_ACCESS(AC.ORG_ID) = 'Y'
             AND AIL.INVOICE_ID = AP.INVOICE_ID
             AND AIL.LINE_TYPE_LOOKUP_CODE IN ('ITEM', 'FREIGHT')
             AND NOT EXISTS
           (SELECT 'X'
                    FROM AP_CHECKS_ALL           AC1,
                         AP_INVOICE_PAYMENTS_ALL AIP1,
                         AP_INVOICE_PAYMENTS_ALL AIP2,
                         XLA.XLA_EVENTS          XE1,
                         XLA.XLA_EVENTS          XE2
                   WHERE AC1.CHECK_ID = AC.CHECK_ID
                     AND AC1.VOID_DATE IS NOT NULL
                     AND AC1.STATUS_LOOKUP_CODE = 'VOIDED'
                     AND AC1.CHECK_ID = AIP1.CHECK_ID
                     AND AC1.CHECK_ID = AIP2.CHECK_ID
                     AND AIP1.ACCOUNTING_EVENT_ID = XE1.EVENT_ID
                     AND AIP2.ACCOUNTING_EVENT_ID = XE2.EVENT_ID
                     AND XE1.EVENT_TYPE_CODE NOT LIKE '%CANCELLED'
                     AND XE2.EVENT_TYPE_CODE LIKE '%CANCELLED'
                     AND AIP1.ACCOUNTING_DATE = AIP2.ACCOUNTING_DATE)
             AND NOT EXISTS
           (SELECT 1
                    FROM CUST_SN.CSN_AP_IPT_PAY_APPLICATION CAIPA
                   WHERE CAIPA.SOURCE_INVOICE_NUM = AP.INVOICE_NUM
                     AND CAIPA.PAYMENT_METHOD = '客户退款')
           GROUP BY HR.NAME,
                    AP.ATTRIBUTE15,
                    HP.PARTY_NAME,
                    AC.CURRENCY_CODE,
                    API.ACCOUNTING_DATE,
                    AP_CODE.DISPLAYED_FIELD,
                    AP.INVOICE_NUM,
                    IBY.PAYMENT_METHOD_NAME,
                    AC.CHECK_NUMBER,
                    AP.INVOICE_ID,
                    AP.ATTRIBUTE2,
                    API.ACCOUNTING_EVENT_ID,
                    AIL.ATTRIBUTE5,
                    API.AMOUNT,
                    AC.PARTY_ID,
                    AC.VENDOR_ID,
                    AC.ORG_ID,
                    AC.CHECK_ID
          UNION ALL
          SELECT HR.NAME ORG_NAME,
                 NPO_COMMON_PUBLIC_PKG.GET_FLEX_DESC('XY_COA_CC',
                                                     AP.ATTRIBUTE15) DEPT_NAME,
                 HP.PARTY_NAME VENDOR_NAME,
                 AC.CURRENCY_CODE,
                 API.ACCOUNTING_DATE GL_DATE,
                 SUM(DECODE(AP.PAY_CURR_INVOICE_AMOUNT,
                            0,
                            0,
                            ROUND(NAP.LINE_AMOUNT /
                                  AP.PAY_CURR_INVOICE_AMOUNT * API.AMOUNT,
                                  2))) CHECK_AMOUNT,
                 NULL TRX_AMOUNT,
                 AP_CODE.DISPLAYED_FIELD TRX_TYPE,
                 AP.INVOICE_NUM,
                 IBY.PAYMENT_METHOD_NAME CHECK_TYEP,
                 AC.CHECK_NUMBER,
                 AP.INVOICE_ID,
                 AP.ATTRIBUTE2 PO_PLAN_NUM,
                 NAP_VENDOR_WITH_PKG.GET_DESC(API.ACCOUNTING_EVENT_ID) DESCRIPTION,
                 DECODE('N',
                        'Y',
                        NAP_INVOICES_PKG.GET_PO_PRJ_NUM_LISTS(NAP.PO_HEADER_ID),
                        NULL) PROJECT_NUM,
                 ROW_NUMBER() OVER(PARTITION BY AC.CHECK_NUMBER, API.ACCOUNTING_EVENT_ID ORDER BY AC.CHECK_NUMBER) CNO,
                 API.AMOUNT PAY_AMOUNT,
                 'PAYMENT' DATA_TYPE,
                 AC.PARTY_ID,
                 AC.VENDOR_ID,
                 AC.ORG_ID,
                 AC.CHECK_ID
            FROM AP_CHECKS_ALL           AC,
                 HR_OPERATING_UNITS      HR,
                 HZ_PARTIES              HP,
                 AP_INVOICE_PAYMENTS_ALL API,
                 IBY_PAYMENT_METHODS_VL  IBY,
                 AP_INVOICES_ALL         AP,
                 NAP_INVOICES_ALL        NAP,
                 AP_LOOKUP_CODES         AP_CODE
           WHERE AC.PARTY_ID = HP.PARTY_ID
             AND API.CHECK_ID = AC.CHECK_ID
             AND AP.INVOICE_ID = NAP.INVOICE_ID
             AND AC.PAYMENT_METHOD_CODE = IBY.PAYMENT_METHOD_CODE
             AND AC.ORG_ID = HR.ORGANIZATION_ID
             AND API.INVOICE_ID = AP.INVOICE_ID
             AND AP_CODE.LOOKUP_CODE = AP.INVOICE_TYPE_LOOKUP_CODE
             AND AP_CODE.LOOKUP_TYPE = 'INVOICE TYPE'
             AND AP.INVOICE_TYPE_LOOKUP_CODE IN ('PREPAYMENT')
             AND (AC.ORG_ID = 99 OR 99 IS NULL)
             AND EXISTS (SELECT 1
                    FROM NCM_COMMON_GT_TMP NCGT
                   WHERE NCGT.CHAR1 = AP.ATTRIBUTE15)
             AND (AP.ATTRIBUTE15 = '30034170' OR '30034170' IS NULL)
             AND AC.CURRENCY_CODE = NVL(null, AC.CURRENCY_CODE)
             AND API.ACCOUNTING_DATE >= to_date('2022-01-01','yyyy-mm-dd')
             AND API.ACCOUNTING_DATE <= to_date('2022-10-01','yyyy-mm-dd')
             AND (AC.VENDOR_ID = null OR null IS NULL)
             AND MO_GLOBAL.CHECK_ACCESS(AC.ORG_ID) = 'Y'
             AND NOT EXISTS
           (SELECT 'X'
                    FROM AP_CHECKS_ALL           AC1,
                         AP_INVOICE_PAYMENTS_ALL AIP1,
                         AP_INVOICE_PAYMENTS_ALL AIP2,
                         XLA.XLA_EVENTS          XE1,
                         XLA.XLA_EVENTS          XE2
                   WHERE AC1.CHECK_ID = AC.CHECK_ID
                     AND AC1.VOID_DATE IS NOT NULL
                     AND AC1.STATUS_LOOKUP_CODE = 'VOIDED'
                     AND AC1.CHECK_ID = AIP1.CHECK_ID
                     AND AC1.CHECK_ID = AIP2.CHECK_ID
                     AND AIP1.ACCOUNTING_EVENT_ID = XE1.EVENT_ID
                     AND AIP2.ACCOUNTING_EVENT_ID = XE2.EVENT_ID
                     AND XE1.EVENT_TYPE_CODE NOT LIKE '%CANCELLED'
                     AND XE2.EVENT_TYPE_CODE LIKE '%CANCELLED'
                     AND AIP1.ACCOUNTING_DATE = AIP2.ACCOUNTING_DATE)
             AND NOT EXISTS
           (SELECT 1
                    FROM CUST_SN.CSN_AP_IPT_PAY_APPLICATION CAIPA
                   WHERE CAIPA.SOURCE_INVOICE_NUM = AP.INVOICE_NUM
                     AND CAIPA.PAYMENT_METHOD = '客户退款')
           GROUP BY HR.NAME,
                    AP.ATTRIBUTE15,
                    HP.PARTY_NAME,
                    AC.CURRENCY_CODE,
                    API.ACCOUNTING_DATE,
                    AP_CODE.DISPLAYED_FIELD,
                    AP.INVOICE_NUM,
                    IBY.PAYMENT_METHOD_NAME,
                    AC.CHECK_NUMBER,
                    AP.INVOICE_ID,
                    AP.ATTRIBUTE2,
                    API.ACCOUNTING_EVENT_ID,
                    DECODE('N',
                           'Y',
                           NAP_INVOICES_PKG.GET_PO_PRJ_NUM_LISTS(NAP.PO_HEADER_ID),
                           NULL),
                    API.AMOUNT,
                    AC.PARTY_ID,
                    AC.VENDOR_ID,
                    AC.ORG_ID,
                    AC.CHECK_ID) ACORD
   GROUP BY ORG_NAME,
            DEPT_NAME,
            VENDOR_NAME,
            CURRENCY_CODE,
            GL_DATE,
            TRX_TYPE,
            INVOICE_NUM,
            CHECK_TYPE,
            CHECK_NUM,
            INVOICE_ID,
            PO_PLAN_NUM,
            DESCRIPTION,
            DECODE('N', 'Y', PROJECT_NUM, NULL),
            CNO,
            PAY_AMOUNT,
            DATA_TYPE,
            PARTY_ID,
            VENDOR_ID,
            ORG_ID,
            CHECK_ID;

SQL虽然很长,但是并不复杂,SQL长的原因是里面有多个UNION ALL
遇到一长串UNION ALL的SQL一般都是单独跑每段SQL,看谁慢就优化谁
单独跑每段SQL都能秒杀,但是SQL组合在一起就变慢了
UNION ALL之后变慢肯定是UNION ALL之后的执行计划与单独分段跑的执行计划不一致导致
将优化器版本降为11.2.0.4,SQL可以在5秒左右跑完
如果只是为了优化SQL,可以直接加个HINT /*+ OPTIMIZER_FEATURES_ENABLE(‘11.2.0.4’) */
如果想要刨根问底,就需要找到是哪个隐含参数引起
可以在虚拟机环境中部署一套11.2.0.4环境,一套19c环境
在两个环境中分表创建a,b表(以sysdba执行)
create table scott.a as select * from sys.x$ksppi;
create table scott.b as select * from sys.x$ksppcv;
然后在11g中创建指向19c的DBLINK,执行下面脚本查询出19c中新增的优化器参数

select name from (
select a.ksppinm name,
       b.ksppstvl value,
       b.ksppstdf deflt,
       a.ksppdesc describ 
  from a@db19c, b@db19c
 where a.indx = b.indx
   and a.ksppinm like '_optimi%'
minus
select a.ksppinm name,
       b.ksppstvl value,
       b.ksppstdf deflt,
       a.ksppdesc describ
  from a, b
 where a.indx = b.indx
   and a.ksppinm like '_optimi%'
) where value='TRUE';

查询出来的参数如下:
_optimizer_ads_use_partial_results
_optimizer_ads_use_spd_cache
_optimizer_aggr_groupby_elim
_optimizer_allow_all_access_paths
_optimizer_ansi_join_lateral_enhance
_optimizer_ansi_rearchitecture
_optimizer_band_join_aware
_optimizer_batch_table_access_by_rowid
_optimizer_cluster_by_rowid
_optimizer_cluster_by_rowid_batched
_optimizer_cube_join_enabled
_optimizer_eliminate_subquery
_optimizer_enable_plsql_stats
_optimizer_enhanced_join_elimination
_optimizer_gather_feedback
_optimizer_gather_stats_on_conventional_dml
_optimizer_gather_stats_on_load
_optimizer_gather_stats_on_load_index
_optimizer_generate_ptf_implied_preds
_optimizer_generate_transitive_pred
_optimizer_hybrid_fpwj_enabled
_optimizer_inmemory_access_path
_optimizer_inmemory_autodop
_optimizer_inmemory_bloom_filter
_optimizer_inmemory_capture_stored_stats
_optimizer_inmemory_cluster_aware_dop
_optimizer_inmemory_gen_pushable_preds
_optimizer_inmemory_minmax_pruning
_optimizer_inmemory_table_expansion
_optimizer_interleave_or_expansion
_optimizer_key_vector_payload
_optimizer_key_vector_pruning_enabled
_optimizer_multi_table_outerjoin
_optimizer_multicol_join_elimination
_optimizer_nlj_hj_adaptive_join
_optimizer_non_blocking_hard_parse
_optimizer_null_accepting_semijoin
_optimizer_partial_join_eval
_optimizer_quarantine_sql
_optimizer_reduce_groupby_key
_optimizer_strans_adaptive_pruning
_optimizer_track_hint_usage
_optimizer_union_all_gsets
_optimizer_unnest_scalar_sq
_optimizer_use_gtt_session_stats
_optimizer_use_histograms
_optimizer_use_stats_on_conventional_dml
_optimizer_use_xt_rowid
_optimizer_vector_transformation
根据参数名称大概就能猜测到参数是什么作用
与inmemory相关的参数一般来说不会引起本次问题
与gather相关的参数一般来说也不会引起本次问题
剩余的参数就不多了,多试几次就能找到引起SQL变慢的参数
最终经过排查,发现本次引起SQL变慢的参数是_optimizer_unnest_scalar_sq 

以上是关于Oracle19c中SQL分开跑很快UNION ALL之后变慢分析的主要内容,如果未能解决你的问题,请参考以下文章

Oracle19c中SQL分开跑很快UNION ALL之后变慢分析

Oracle19c中SQL分开跑很快UNION ALL之后变慢分析

union all 效率问题

小知识:Oracle中的层次查询

oracle 19c虚拟要下自动索引测试

我正在 oracle 19c 中创建一个 PL SQL 函数来查找两个数字中的最大值