查询(具有聚合函数)调优

时间:2013-01-16 10:36:26

标签: sql

我有一个查询(如下)。解释计划显示高CPU利用率,这也导致我们实验室的停机时间。那么有可能进一步管理这个查询吗?我该怎么做才能调整它?

FYI,mtr_main_a,mtr_main_b,mtr_hist包含巨大的记录,可能是1000万或更多。

SELECT to_char(MAX(mdt), 'MM-DD-RRRR HH24:MI:SS')
FROM   (
           SELECT MAX(mod_date - 2 / 86400) mdt
           FROM   mtr_main_a
           UNION
           SELECT MAX(mod_date - 2 / 86400) mdt
           FROM   mtr_main_b
           UNION
           SELECT MAX(mod_date - 2 / 86400) mdt
           FROM   mtr_hist@batch_hist
       )
/

解释计划如下

Execution Plan
----------------------------------------------------------
Plan hash value: 1573811822

-------------------------------------------------------------------------------------------------------------
| Id  | Operation              | Name       | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     | Inst   |IN-OUT|
-------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |            |     1 |     9 |       | 79803   (1)| 00:18:38 |        |      |
|   1 |  SORT AGGREGATE        |            |     1 |     9 |       |            |          |        |      |
|   2 |   VIEW                 |            |     2 |    18 |       | 79803   (1)| 00:18:38 |        |      |
|   3 |    SORT UNIQUE         |            |     2 |    17 |    77M| 79803   (2)| 00:18:38 |        |      |
|   4 |     UNION-ALL          |            |       |       |       |            |          |        |      |
|   5 |      SORT AGGREGATE    |            |     1 |     8 |       | 79459   (1)| 00:18:33 |        |      |
|   6 |       TABLE ACCESS FULL| MTR_MAIN_A |  5058K|    38M|       | 67735   (1)| 00:15:49 |        |      |
|   7 |      SORT AGGREGATE    |            |     1 |     9 |       |   344   (1)| 00:00:05 |        |      |
|   8 |       TABLE ACCESS FULL| MTR_MAIN_B |     1 |     9 |       |   343   (1)| 00:00:05 |        |      |
|   9 |      REMOTE            |            |       |       |       |            |          |  HISTB | R->S |
-------------------------------------------------------------------------------------------------------------

Remote SQL Information (identified by operation id):
----------------------------------------------------

   9 - EXPLAIN PLAN SET STATEMENT_ID='PLUS10294704' INTO PLAN_TABLE@! FOR SELECT
       MAX("A1"."MOD_DATE"-.00002314814814814814814814814814814814814815) FROM "MTR_HIST" "A1" (accessing
       'HISTB' )

谢谢和问候, 钱德拉

2 个答案:

答案 0 :(得分:1)

您应该能够通过在mod_date列上添加索引并在确定最大日期后以最后减法的方式更改查询来大大提高性能:

SELECT to_char(MAX(mdt) - 2 / 86400, 'MM-DD-RRRR HH24:MI:SS')
FROM   (
           SELECT MAX(mod_date) mdt
           FROM   mtr_main_a
           UNION
           SELECT MAX(mod_date) mdt
           FROM   mtr_main_b
           UNION
           SELECT MAX(mod_date) mdt
           FROM   mtr_hist@batch_hist
       )

这应该摆脱全表扫描。

答案 1 :(得分:1)

如果列上有索引,此版本的工作原理如何:

SELECT to_char((case when a.mdt > b.mdt and a.mdt > c.mdt then a.mdt
                     when b.mdt > c.mdt then b.mdt
                     else c.mdt
                end) - 2 / 86400, 'MM-DD-RRRR HH24:MI:SS')
FROM (SELECT MAX(mod_date) mdt
      FROM   mtr_main_a
     ) a cross join
     (SELECT MAX(mod_date) mdt
      FROM   mtr_main_b
     ) b cross join
     (SELECT MAX(mod_date) mdt
      FROM   mtr_hist@batch_hist
     ) c

如果union版本无法更快地运行,这只是一个建议。