轉:Oracle中檢視已執行sql的執行計劃---dbms_xplan.display_cursor
有時候我們可能會希望檢視一條已經執行過的的執行計劃,常用的方式有兩種:a,set autotrace後再重新執行一遍,不過重新執行可能會浪費時間,而且有些語句也不允許(例如修改操作的語句),或者查詢v$sql_plan檢視,但v$檢視的可讀性又不是那麼好,這裡提供一個新方式,透過dbms_xplan.display_cursor來獲取執行過的sql的執行計劃。
[@more@]首先看看該函式的語法:
DBMS_XPLAN.DISPLAY_CURSOR(
sql_id IN VARCHAR2 DEFAULT NULL,
child_number IN NUMBER DEFAULT NULL,
format IN VARCHAR2 DEFAULT 'TYPICAL');
由上可知,我們至少需要找到執行過sql的sql_id,該引數可以從v$sql檢視中找到。
下面,舉個例子吧,執行一個簡單查詢:
SQL> select count(0) from cat_product cp,cat_drug cd where cp.medical_id=cd.id;
COUNT(0)
----------
118908
如果我們想獲取該語句的實際執行計劃,透過下列步驟:
1、查詢v$sql檢視,找到該語句的sql_id(注意喲,必須要確保你要查詢的sql語句還在shared pool):
SQL> select sql_id from v$sql where sql_text=
2 'select count(0) from cat_product cp,cat_drug cd where cp.medical_id=cd.id';
SQL_ID
-------------
c9cxqvr3q4tjd
2、呼叫dbms_xplan包,檢視該語句執行時的實現執行計劃:
SQL> select * from table(dbms_xplan.display_cursor('c9cxqvr3q4tjd'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID c9cxqvr3q4tjd, child number 0
-------------------------------------
select count(0) from cat_product cp,cat_drug cd where cp.medical_id=cd.id
Plan hash value: 2559475106
-----------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | | 750 (100)| |
| 1 | SORT AGGREGATE | | 1 | 50 | | | |
|* 2 | HASH JOIN | | 118K| 5804K| 4096K| 750 (1)| 00:00:11 |
| 3 | INDEX FAST FULL SCAN| PK_CAT_DRUG | 112K| 2758K| | 186 (1)| 00:00:03 |
| 4 | INDEX FAST FULL SCAN| TU_CAT_PRODUCT_MED_CHECK | 118K| 2902K| | 212 (1)| 00:00:03 |
-----------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("CP"."MEDICAL_ID"="CD"."ID")
事實上dbms_xplan.display_cursor也非常靈活,如果執行的統計資訊也被收集的話,還可以顯示出每一步實際的花費時間等資訊,例如:
SQL> select /*+gather_plan_statistics*/ count(0) from cat_product cp,cat_drug cd where cp.medical_id=cd.id;
COUNT(0)
----------
118908
SQL> select sql_id from v$sql where sql_text=
2 'select /*+gather_plan_statistics*/ count(0) from cat_product cp,cat_drug cd where cp.medical_id=cd.id';
SQL_ID
-------------
91w1ug6vc9pxh
SQL> select * from table(dbms_xplan.display_cursor('91w1ug6vc9pxh',null,'all iostats last'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 91w1ug6vc9pxh, child number 0
-------------------------------------
select /*+gather_plan_statistics*/ count(0) from cat_product cp,cat_drug cd where cp.medical_id=cd.id
Plan hash value: 2559475106
-----------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes|E-Temp | Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
-----------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | 50 | | | | 1 |00:00:00.18 | 595 |
|* 2 | HASH JOIN | | 1 | 118K| 5804K| 4096K| 750 (1)| 00:00:11 | 118K|00:00:00.33 | 595 |
| 3 | INDEX FAST FULL SCAN| PK_CAT_DRUG | 1 | 112K| 2758K| | 186 (1)| 00:00:03 | 112K|00:00:00.01 | 278 |
| 4 | INDEX FAST FULL SCAN| TU_CAT_PRODUCT_MED_CHECK | 1 | 118K| 2902K| | 212 (1)| 00:00:03 | 118K|00:00:00.01 | 317 |
-----------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1
3 - SEL$1 /
4 - SEL$1 /
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("CP"."MEDICAL_ID"="CD"."ID")
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - (#keys=0) COUNT(*)[22]
2 - (#keys=1)
3 - "CD"."ID"[CHARACTER,24]
4 - "CP"."MEDICAL_ID"[CHARACTER,24]
35 rows selected.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/197458/viewspace-1028701/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle中檢視已執行sql的執行計劃OracleSQL
- ORACLE中檢視執行計劃(轉)Oracle
- oracle dbms_xplan.display_cursor檢視執行計劃Oracle
- 儲存過程中檢視sql執行計劃的方法儲存過程SQL
- Oracle 檢視SQL的執行計劃OracleSQL
- Oracle檢視正在執行的SQL以及執行計劃分析OracleSQL
- 檢視oracle執行計劃 - 轉Oracle
- TOAD中檢視執行計劃(Explain Plan)AI
- Oracle sql執行計劃OracleSQL
- 檢視sql執行計劃SQL
- DBMS_XPLAN.DISPLAY_CURSOR()看執行計劃
- ORACLE執行計劃的檢視Oracle
- 如何檢視SQL的執行計劃SQL
- 檢視SQL的執行計劃方法SQL
- 多種方法檢視Oracle SQL執行計劃OracleSQL
- oracle10g 檢視SQL執行計劃OracleSQL
- 10G中檢視歷史執行計劃資訊
- SQL的執行計劃SQL
- sql 執行計劃SQL
- Oracle檢視執行計劃的命令Oracle
- oracle檢視執行計劃的方法Oracle
- Oracle檢視sql_id 的歷史執行計劃OracleSQL
- Oracle檢視執行計劃(五)Oracle
- Oracle檢視執行計劃(六)Oracle
- Oracle檢視執行計劃(一)Oracle
- Oracle檢視執行計劃(二)Oracle
- Oracle檢視執行計劃(三)Oracle
- Oracle檢視執行計劃(四)Oracle
- oracle如何檢視執行計劃Oracle
- 檢視Oracle SQL執行計劃方法比較、分析OracleSQL
- SQLPLUS檢視oracle sql執行計劃命令SQLOracle
- SQL Server中檢視SQL句子執行所用的時間SQLServer
- 【Explain Plan】檢視SQL的執行計劃AISQL
- 使用PL/SQL檢視執行計劃SQL
- 檢視sql執行計劃--set autotraceSQL
- oracle實用sql(15)--檢視SQL執行計劃的順序OracleSQL
- Oracle資料庫關於SQL的執行計劃(轉)Oracle資料庫SQL
- Oracle SQL Profile固定執行計劃的方法OracleSQL