DBMS_XPLAN 包關于執行計劃的功能如下:(每個函數的參數可以desc dbms_xplan檢視)
•DISPLAY - to format and display the contents of a plan table.
•DISPLAY_AWR - to format and display the contents of the execution plan of a stored SQL statement in the AWR.
•DISPLAY_CURSOR - to format and display the contents of the execution plan of any loaded cursor.
•DISPLAY_SQL_PLAN_BASELINE - to display one or more execution plans for the SQL statement identified by SQL handle
•DISPLAY_SQLSET - to format and display the contents of the execution plan of statements stored in a SQL tuning set.
1 explain plan for sql_text-----SELECT * FROM table(DBMS_XPLAN.DISPLAY);
explain plan解釋SQL,相關資訊預設放在全局臨時表PLAN_TABLE中,是以各個會話隻看到自己執行SQL的執行計劃(可以通過dbms_metadata.get_ddl檢視此表),但需要了解的是explain plan for 并不會去執行sql
DBMS_XPLAN.DISPLAY有以下幾個參數:
table_name,預設'PLAN_TABLE',跟PLAN_TABLE一樣的表結構,也可以讀取.
statement_id 預設null,即查該session最後的一條explain plan解釋的語句。
format 預設'TYPICAL',全部是'BASIC','TYPICAL','ALL'
'ALL -PROJECTION (-/+) xxx' ,format參數基本是通用的,而參數詳情請參考官檔,這裡就不一樣羅列了。這裡要注意: (-/+)加減号的前面要有一個空格,要不會報錯。
2 DBMS_XPLAN.DISPLAY_AWR
sql_id --輸入存儲在AWR中的sql_id,你可以先查dba_hist_sql_plan,
plan_hash_value --如果是null的話該sql_id所有的執行計劃會輸出(預設null)
db_id --如果忽略,預設就是目前的database
format --通用,預設typical
1).請確定AWR已經運作。
2).權限:使用者需要select on DBA_HIST_SQL_PLAN,DBA_HIST_SQLTEXT,V$DATABASE的權限。
3).查sql_id,根據sql文本查出sql_id ,可以從dba_hist_sqltext查。
4).來源:展示的執行計劃的資訊,來源于dba_hist_sql_plan。
例:SELECT * FROM table(DBMS_XPLAN.DISPLAY_AWR('&sql_id',&dbid,null,'all'));
3 DBMS_XPLAN.DISPLAY_CURSOR()
sql_id 指定位于library cache執行計劃中SQL父遊标,如果不指定就傳回最後一條SQL的sql_id。
child_number 預設是0,如果是null,則傳回sql_id父遊标下的所有子遊标的執行計劃。
format --通用,預設typical
例:select * from table(dbms_xplan.display_cursor('&sql_id',&child_number,'all allstats last outline'));
ALLSTATS - A shortcut for 'IOSTATS MEMSTATS'
LAST - By default, plan statistics are shown for all executions of the cursor. The keyword LAST can be specified to see only the statistics for the last execution.
直接關聯查詢SQL執行計劃:
SELECT t.*
FROM v$sql s,
table(DBMS_XPLAN.DISPLAY_CURSOR(s.sql_id, s.child_number)) t
WHERE sql_text LIKE '%XXXXX%';