如何看懂執行計劃!

warehouse發表於2009-06-19

The execution order in EXPLAIN PLAN output begins with the line that is the furthest indented to the right. The next step is the parent of that line. If two lines are indented equally, then the top line is normally executed first.

在doc上看到了,記錄一下

[@more@]

SQL> select count(*) from dba_objects;

COUNT(*)
----------
11345


Execution Plan
----------------------------------------------------------
Plan hash value: 2598313856

--------------------------------------------------------------------------------

---------------

| Id | Operation | Name | Rows | Bytes | Cost (%C

PU)| Time |

--------------------------------------------------------------------------------

---------------

| 0 | SELECT STATEMENT | | 1 | | 38
(6)| 00:00:01 |

| 1 | SORT AGGREGATE | | 1 | |
| |

| 2 | VIEW | DBA_OBJECTS | 10010 | | 38
(6)| 00:00:01 |

| 3 | UNION-ALL | | | |
| |

|* 4 | FILTER | | | |
| |

|* 5 | HASH JOIN | | 11645 | 398K| 35
(6)| 00:00:01 |

| 6 | TABLE ACCESS FULL | USER$ | 32 | 96 | 2
(0)| 00:00:01 |

|* 7 | TABLE ACCESS FULL | OBJ$ | 11645 | 363K| 32
(4)| 00:00:01 |

|* 8 | TABLE ACCESS BY INDEX ROWID| IND$ | 1 | 8 | 2
(0)| 00:00:01 |

|* 9 | INDEX UNIQUE SCAN | I_IND1 | 1 | | 1
(0)| 00:00:01 |

| 10 | NESTED LOOPS | | 3 | 18 | 3
(0)| 00:00:01 |

| 11 | INDEX FULL SCAN | I_LINK1 | 3 | 9 | 1
(0)| 00:00:01 |

| 12 | TABLE ACCESS CLUSTER | USER$ | 1 | 3 | 1
(0)| 00:00:01 |

|* 13 | INDEX UNIQUE SCAN | I_USER# | 1 | | 0
(0)| 00:00:01 |

doc的前半句話不能很好的理解,不知道它要表述什麼意思?
反正我們只需要看第三列"|"和語句對比,看哪個語句動作縮的
越靠右側就想執行它,如果存在相同的則誰在前面就執行誰,根據這個
指導原則,計劃的執行順序如下:
6,7,5,9,8,4,11,13,12,10,3,2,1,0

--===============================
SQL> select sql_id,sql_text from v$sql where sql_text like '%select count(*) fro
m dba_objects%';

SQL_ID
-------------
SQL_TEXT
--------------------------------------------------------------------------------

gxk8zvq0j02z8
select sql_id,sql_text from v$sql where sql_text like '%select count(*) from dba

_objects%'

1jhx4zbub8uw3
select * from v$sql where sql_text like '%select count(*) from dba_objects%'

8vcrngun00v6g
select sql_id,sql_text from v$sql where sql_text like '%select count(*) from dba


SQL_ID
-------------
SQL_TEXT
--------------------------------------------------------------------------------

_objects%'

g4pkmrqrgxg3b
select count(*) from dba_objects

08bm3s8hxudu7
EXPLAIN PLAN SET STATEMENT_ID='PLUS886' FOR select count(*) from dba_objects


SQL>
--===============================
SQL> select id,parent_id from v$sql_plan where sql_id='g4pkmrqrgxg3b';

ID PARENT_ID
---------- ----------
0
1 0
2 1
3 2
4 3
5 4
6 5
7 5
8 4
9 8
10 3

ID PARENT_ID
---------- ----------
11 10
12 10
13 12

14 rows selected.

SQL>
9i的執行計劃顯示出來的是2列數字:
看起來就更容易了:
先從最上面看id,之後趙它的parent_id,如此下去直到找不到
parent_id,那麼就先執行這個id,遇到具有相同parent_id的則誰
在前面執行誰,按照這種方法排列的順序如下
6,7,5,9,8,4,11,13,12,10,3,2,1,0
其實我們可以根據id和parent_id畫一顆樹,然後按照前序(應該是前序吧)
遍歷這顆樹就可以了:

如圖:http://www.itpub.net/viewthread.php?tid=1411679&extra=page%3D1&frombbs=1

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/19602/viewspace-1023289/,如需轉載,請註明出處,否則將追究法律責任。

相關文章