Oracle exists子查询出现rownum引起性能问题的优化

Rationale
The SQL spent 100% of its database time on CPU, I/O and Cluster waits.
This part of database time may be improved by the SQL Tuning Advisor.
Rationale
Database time for this SQL was divided as follows: 100% for SQL
execution, 0% for parsing, 0% for PL/SQL execution and 0% for Java
execution.
Rationale
SQL statement with SQL_ID "7ptu1y0d95s2r" was executed 3209 times and
had an average elapsed time of 20 seconds.
原SQL:
update WWH
set WWH.STATUS = (case
when (select count(1)
from WWD
where

.........................

and exists (select 1
from WWD
where 1 = 1
and WWH.WH = WWD.WH
and WWH.WAVE = WWD.WAVE
and WWD.SO in ('2349212263452' )
and rownum = 1)


set line 1000
set pagesize 1000
set timing on
set autotrace traceonly
执行SQL.........

查看执行计划如下:

Execution Plan
----------------------------------------------------------
Plan hash value: 3044987130

--------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 1 | 45 | 191K (1)| 00:38:18 |
| 1 | UPDATE | WWH | | | | |
|* 2 | FILTER | | | | | |
|* 3 | TABLE ACCESS FULL | WWH | 98070 | 4309K| 449 (1)| 00:00:06 |
|* 4 | COUNT STOPKEY | | | | | |
|* 5 | INDEX UNIQUE SCAN | UNQ_WWD | 1 | 40 | 2 (0)| 00:00:01 |
| 6 | SORT AGGREGATE | | 1 | 24 | | |
|* 7 | COUNT STOPKEY | | | | | |
|* 8 | FILTER | | | | | |
|* 9 | INDEX RANGE SCAN | UNQ_WWD| 1 | 24 | 3 (0)| 00:00:01 |
| 10 | SORT AGGREGATE | | 1 | 27 | | |
|* 11 | TABLE ACCESS BY INDEX ROWID| WWD | 1 | 27 | 4 (0)| 00:00:01 |
|* 12 | INDEX RANGE SCAN | UNQ_WWD| 1 | | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - filter( EXISTS (SELECT 0 FROM "WWD" WHERE ROWNUM=1 AND
"WWD"."WH"=:B1 AND "WWD"."SO"='2349212263452' AND "WWD"."WAVE"=:B2))
-............................................

.............................................

Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
263080 consistent gets
0 physical reads
72 redo size
829 bytes sent via SQL*Net to client
3583 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
0 rows processed
上面显示表没走索引,逻辑读为263080,查看表的字段(WAVE,WH)存在索引,我们再建一个选择性更好的索引:
增加索引:
create index IND_MODIFY_TIME_IX on WWH(Modify_Time,WAVE,WH) tablespace wsx;

再查看执行计划依然没有走索引,逻辑读同样是263080大。

强制加入索引后,虽然执行计划中有显示走了全索引扫描,且驱动表由WWH表变成了WWD表,但逻辑读变得更大为299361,走索引前后性能都差的最主要原因是执行计划中看到有
"filter( EXISTS..........."这行,表示子查询没有展开。
SQL> update /*+index(WWH IND_MODIFY_TIME_IX) */ WWH ................

Elapsed: 00:00:01.11

Execution Plan
----------------------------------------------------------
Plan hash value: 389457693

----------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 1 | 45 | 196K (1)| 00:39:16 |
| 1 | UPDATE | WWH | | | | |
|* 2 | TABLE ACCESS BY INDEX ROWID | WWD | 4903 | 215K| 5302 (1)| 00:01:04 |
|* 3 | INDEX FULL SCAN | IND_MODIFY_TIME_IX | 4996 | | 602 (1)| 00:00:08 |
|* 4 | COUNT STOPKEY | | | | | |
|* 5 | INDEX UNIQUE SCAN | UNQ_WWD | 1 | 40 | 2 (0)| 00:00:01 |
| 6 | SORT AGGREGATE | | 1 | 24 | | |
|* 7 | COUNT STOPKEY | | | | | |
|* 8 | FILTER | | | | | |
|* 9 | INDEX RANGE SCAN | UNQ_WWD | 1 | 24 | 3 (0)| 00:00:01 |
| 10 | SORT AGGREGATE | | 1 | 27 | | |
|* 11 | TABLE ACCESS BY INDEX ROWID| WWD | 1 | 27 | 4 (0)| 00:00:01 |
|* 12 | INDEX RANGE SCAN | UNQ_WWD | 1 | | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - filter( EXISTS (SELECT 0 FROM "WWD" WHERE ROWNUM=1 AND
"WWD"."WH"=:B1 AND "WWD"."SO"='2349212263452' AND "WWD"."WAVE"=:B2))
-............................................

.............................................

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/a8342d664bee5954ff7931dbb46506ab.html