0

我有一个查询(下)。解释计划显示 CPU 使用率很高,这也导致了我们实验室的停机。所以可以进一步管这个查询吗?我应该怎么做才能调整它?

仅供参考,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' )

谢谢和问候,钱德拉

4

2 回答 2

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
       )

这应该摆脱全表扫描。

于 2013-01-16T10:40:26.533 回答
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如果版本不能更快地运行,这只是一个建议。

于 2013-01-16T11:34:34.960 回答