2016-03-07 206 views
2

这种特殊的查询工作正常,直到我们在11i的,但只要我们切换到12C查询一直在做非常糟糕的Oracle查询。 我试图删除提示,它似乎做得好一点,但我们的DBA希望我们删除所有导致各个子步骤序列化的多个子查询。我对如何改变它感到不知所措。 关于我能做什么或开始的任何指针?需要帮助优化12C

 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 | 
----------------------------------------------------------------------------------------------------------------------- 
+0

什么是在以前的版本计划(11i的是Oracle应用的版本,我相信你的意图是在这里说,这在以前的版本一样11g中的Oracle数据库的运行良好)?您发布的查询仍然有一些提示,但在问题的文本中说,您删除了令人困惑的“提示”。您发布的查询计划是否来自包含所有提示的查询版本?或提示删除的那个? –

+1

缺少“谓词信息” - 它打印在计划表下,你可以用它补充计划吗? – krokodilko

+0

我会拿出具体化,并下令提示,(甲骨文将决定他应该怎么使用),我会尝试并行(8)提示来代替。优化其关于“瞄准射击 - 瞄准 - 射击......”,因为它取决于事情 – Thomas

回答

0

尝试执行TT1和评估的执行时间。如果没有问题,则继续使用TT1 + TT2等。你需要了解问题出在解决之前。

0

我会假设你的问题是直接关系到一个不错的计划。

所以,如你所说,它表现得很好的11g数据库所以有基本的两件事情你可以做来尝试解决一个短期的方式你的问题:

  1. 检查OPTIMIZER_FEATURES_ENABLE参数在12c版本。如果它设置为12. *或较高版本,请尝试将正在运行查询的会话降级到旧数据库的11. *版本。

  2. 假设你有优化包(许可问题 - 照顾)使能为您的生产环境中,你可以使用SQL配置文件。怎么样?您可以手动将您的计划从11g复制并导入到12c数据库。看看dbms_sqltune.import_sql_profile。 https://oracle-randolf.blogspot.com.br/2009/03/plan-stability-in-10g-using-existing.html

正如我前面所说,这些都是短期行为。评估你的SQL会更好。