需要帮助在 12c 中优化 Oracle 查询

Posted

技术标签:

【中文标题】需要帮助在 12c 中优化 Oracle 查询【英文标题】:Need help to optimize Oracle query in 12c 【发布时间】:2016-03-07 20:09:50 【问题描述】:

在我们进入 11i 之前,这个特定的查询运行良好,但一旦我们切换到 12c,查询的表现就非常糟糕。 我试图删除提示,它似乎做得更好,但我们的 DBA 希望我们删除所有导致各种子步骤序列化的多个子查询。我不知道如何改变它。 关于我可以做什么或开始做什么的任何指示?

     WITH
       -- Get all active accounts that have not been suspended and the suspension rules
       -- apply for the account
       TT1 AS (SELECT 
                      pl.employer_id,
                      ma.employee_id,
                      ma.member_account_id,
                      pl.plan_id
                 FROM sa_plans pl,
                      sa_accounts ma
                WHERE TRUNC(SYSDATE) BETWEEN pl.start_date AND NVL(pl.end_date, TRUNC(SYSDATE))
                  AND pl.employer_id = 23
                  AND pl.plan_type_id NOT IN (4 
                                              ,1) 
                  AND pl.plan_id = ma.plan_id
                  AND sa_mc_info.fn_ee_susp_exempt(ma.employee_id, DECODE(pl.plan_type_id, 1, 6 ,5 )) = 0 
                  AND p_outstanding_threshold_perc > 0
                  AND NVL(ma.auto_suspension_flag, 0) = 0 
                  AND TRUNC(SYSDATE) BETWEEN ma.account_effective_date AND NVL(ma.account_end_date, TRUNC(SYSDATE))
                  AND ma.account_effective_date != NVL(ma.account_end_date, ma.account_effective_date + 1)
              ),
       -- Get all accounts that were active during the plan year for the current accounts
       -- and the outstanding transactions
       TT2 AS (SELECT /*+ MATERIALIZE */
                      ma1.member_account_id,
                      ma1.plan_id,
                      ma1.employee_id ,
                      wwsa.sa_mc_info.fn_acnt_unverified_txns(ma1.member_account_id) outstd_amount
                 FROM sa_accounts ma1,
                      TT1 ma
                WHERE ma1.account_effective_date != NVL(ma1.account_end_date, ma1.account_effective_date + 1)
                  AND ma1.plan_id = ma.plan_id
                  AND ma1.employee_id = ma.employee_id
                  AND ma1.member_account_id = TT1.member_account_id),
       -- Sum the outstanding transactions for the EE and plan 
       TT3 AS (SELECT /*+ MATERIALIZE ORDERED */
                      TT1.member_account_id,
                      SUM(TT2.outstd_amount) outstd_amount
                 FROM TT1,
                      TT2
                WHERE TT1.employee_id = TT2.employee_id
                  AND TT1.plan_id = TT2.plan_id
                GROUP BY TT1.member_account_id
                HAVING SUM(TT2.outstd_amount) > 0),               
       -- Get the current account balance for accounts with outstanding transactions
       TT4 AS (SELECT /*+ MATERIALIZE ORDERED */
                      TT1.*,
                      sa_bal_info.fn_account_balance(TT1.member_account_id) balance,
                      TT3.outstd_amount
                 FROM TT1,
                      TT3
                WHERE TT1.member_account_id = TT3.member_account_id),
       -- Get the list of accounts that need to be suspended
       TT5 as (SELECT /*+ MATERIALIZE */
                      member_account_id,
                      employee_id
                 FROM TT4
                WHERE outstd_amount > balance * p_outstanding_threshold_perc
                  AND outstd_amount > NVL(p_minimum_threshold_amount, 0)  )
SELECT *
  FROM TT5
ORDER BY employee_id;

这是解释计划

    -----------------------------------------------------------------------------------------------------------------------
| Id  | Operation                               | Name                        | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |                             |     1 |    26 |  8622   (1)| 00:00:01 |
|   1 |  TEMP TABLE TRANSFORMATION              |                             |       |       |         |             |
|   2 |   LOAD AS SELECT                        | SYS_TEMP_0FD9D666F_DCBC1560 |       |       |         |             |
|   3 |    NESTED LOOPS                         |                             |     4 |   252 |  8600   (1)| 00:00:01 |
|   4 |     NESTED LOOPS                        |                             |  1707 |   252 |  8600   (1)| 00:00:01 |
|*  5 |      TABLE ACCESS BY INDEX ROWID BATCHED| SA_PLANS                    |     3 |    84 |  8452   (1)| 00:00:01 |
|   6 |       INDEX FULL SCAN                   | SA_PLANS_PK                 | 19218 |       |    75   (2)| 00:00:01 |
|*  7 |      INDEX RANGE SCAN                   | SA_ACCOUNTS_N03             |   569 |       |     2   (0)| 00:00:01 |
|*  8 |     TABLE ACCESS BY INDEX ROWID         | SA_ACCOUNTS                 |     1 |    35 |    67   (0)| 00:00:01 |
|   9 |   LOAD AS SELECT                        | SYS_TEMP_0FD9D6670_DCBC1560 |       |       |         |             |
|  10 |    NESTED LOOPS                         |                             |     3 |   177 |     8   (0)| 00:00:01 |
|  11 |     NESTED LOOPS                        |                             |     3 |   177 |     8   (0)| 00:00:01 |
|  12 |      VIEW                               |                             |     1 |    26 |     2   (0)| 00:00:01 |
|  13 |       TABLE ACCESS FULL                 | SYS_TEMP_0FD9D666F_DCBC1560 |     1 |    22 |     2   (0)| 00:00:01 |
|* 14 |      INDEX RANGE SCAN                   | SA_ACCOUNTS_N01             |     3 |       |     2   (0)| 00:00:01 |
|* 15 |     TABLE ACCESS BY INDEX ROWID         | SA_ACCOUNTS                 |     3 |    99 |     6   (0)| 00:00:01 |
|  16 |   LOAD AS SELECT                        | SYS_TEMP_0FD9D6671_DCBC1560 |       |       |         |             |
|* 17 |    FILTER                               |                             |       |       |         |             |
|  18 |     HASH GROUP BY                       |                             |     1 |    41 |     5  (20)| 00:00:01 |
|* 19 |      HASH JOIN                          |                             |     1 |    41 |     4   (0)| 00:00:01 |
|  20 |       VIEW                              |                             |     1 |    17 |     2   (0)| 00:00:01 |
|  21 |        TABLE ACCESS FULL                | SYS_TEMP_0FD9D666F_DCBC1560 |     1 |    22 |     2   (0)| 00:00:01 |
|  22 |       VIEW                              |                             |     1 |    24 |     2   (0)| 00:00:01 |
|  23 |        TABLE ACCESS FULL                | SYS_TEMP_0FD9D6670_DCBC1560 |     1 |    30 |     2   (0)| 00:00:01 |
|  24 |   LOAD AS SELECT                        | SYS_TEMP_0FD9D6672_DCBC1560 |       |       |         |             |
|* 25 |    HASH JOIN                            |                             |     1 |    78 |     4   (0)| 00:00:01 |
|  26 |     VIEW                                |                             |     1 |    52 |     2   (0)| 00:00:01 |
|  27 |      TABLE ACCESS FULL                  | SYS_TEMP_0FD9D666F_DCBC1560 |     1 |    22 |     2   (0)| 00:00:01 |
|  28 |     VIEW                                |                             |     1 |    26 |     2   (0)| 00:00:01 |
|  29 |      TABLE ACCESS FULL                  | SYS_TEMP_0FD9D6671_DCBC1560 |     1 |    19 |     2   (0)| 00:00:01 |
|  30 |   LOAD AS SELECT                        | SYS_TEMP_0FD9D6673_DCBC1560 |       |       |         |             |
|* 31 |    VIEW                                 |                             |     1 |    52 |     2   (0)| 00:00:01 |
|  32 |     TABLE ACCESS FULL                   | SYS_TEMP_0FD9D6672_DCBC1560 |     1 |    48 |     2   (0)| 00:00:01 |
|  33 |   SORT ORDER BY                         |                             |     1 |    26 |     3  (34)| 00:00:01 |
|  34 |    VIEW                                 |                             |     1 |    26 |     2   (0)| 00:00:01 |
|  35 |     TABLE ACCESS FULL                   | SYS_TEMP_0FD9D6673_DCBC1560 |     1 |    12 |     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------------------------------

【问题讨论】:

之前版本的计划是什么(11i 是 Oracle 应用程序的一个版本,我假设您的意图是说它在之前版本的 Oracle 数据库(如 11g)中运行良好)?您发布的查询仍然有一堆提示,但在您的问题文本中说您删除了令人困惑的“提示”。您发布的查询计划是否来自带有所有提示的查询版本?还是去掉了提示的那个? 缺少“谓词信息” - 它打印在计划表下,您可以用它补充计划吗? 我会去掉 MATERIALIZE 和 ORDERED 提示(Oracle 将决定他应该使用什么),我会尝试使用 PARALLEL(8) 提示。优化它的“瞄准射击瞄准射击......”,你应该尝试一些方法,因为它取决于很多事情 该函数是 sa_mc_info.fn_ee_susp_exempt() DETERMINISTIC 吗?如果没有,则将其更改为 您在迁移到 12c 后重新创建了优化器统计信息? 【参考方案1】:

尝试执行 TT1 并评估执行时间。如果没问题,那么继续 TT1+TT2 以此类推。在真正解决问题之前,您需要了解问题出在哪里。

【讨论】:

【参考方案2】:

我认为您的问题与一个糟糕的计划有关。

所以,正如你所说,它在 11g 数据库上表现得很好,所以你可以做两件事来尝试在短期内解决你的问题:

    检查 12c 版本的 optimizer_features_enable 参数。如果设置为 12.* 或更高版本,请尝试将运行查询的会话降级为旧数据库的 11.* 版本。

    假设您在生产环境中启用了 Tuning Pack(许可问题 - 注意),您可以使用 SQL Profiles。如何?您可以手动从 11g 复制计划并将其导入 12c 数据库。看看 dbms_sqltune.import_sql_profile。 https://oracle-randolf.blogspot.com.br/2009/03/plan-stability-in-10g-using-existing.html

正如我之前所说,这些都是短期行动。最好评估一下你的 SQL。

【讨论】:

以上是关于需要帮助在 12c 中优化 Oracle 查询的主要内容,如果未能解决你的问题,请参考以下文章

需要帮助来优化 ORACLE SQL 查询 [关闭]

如何在 Oracle 12c 中跟踪 sql 查询?

oracle表单12c中的Json_table函数

Oracle 查询优化帮助 - 多遍

Oracle12c中SQL性能优化(SQL TUNING)新特性之自动重优化(automatic reoptimization)

Oracle12c 性能优化攻略:攻略1-3: 匹配表类型与业务需求