1、解釋計劃
當使用explain plan來為一個查詢生成預期的執行計劃時,輸出將包括一下幾種:
SQL訪問的每一張表;
訪問每張表的方法;
每一個需要聯結的數據源所使用的聯結方法;
按次序列出的所有需要完成的運算;
計劃中各步驟的謂語列表信息等等
explain plan for select t1.name, t2.grade from table1 t1 left join table2 t2 on t1.id = t2.id where t1.id =704 ; Explained
查詢得:(與謂語有關的運算都會有*號標注)
select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- Plan hash value: 2814340807 -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (% -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 141 | 8 |* 1 | HASH JOIN OUTER | | 1 | 141 | 8 | 2 | TABLE ACCESS BY INDEX ROWID| TABLE1 | 1 | 115 | 2 |* 3 | INDEX RANGE SCAN | INDEX_TABLE1_ID | 1 | | 1 |* 4 | TABLE ACCESS FULL | TABLE2 | 1 | 26 | 5 -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T1"."ID"="T2"."ID"(+)) 3 - access("T1"."ID"=704) 4 - filter("T2"."ID"(+)=704) Note PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- - dynamic sampling used for this statement (level=2) 22 rows selected
2、系統解釋計劃表的內容
desc plan_table Name Type Nullable Default Comments ----------------- -------------- -------- ------- -------- STATEMENT_ID VARCHAR2(30) Y PLAN_ID NUMBER Y TIMESTAMP DATE Y REMARKS VARCHAR2(4000) Y OPERATION VARCHAR2(30) Y OPTIONS VARCHAR2(255) Y OBJECT_NODE VARCHAR2(128) Y OBJECT_OWNER VARCHAR2(30) Y OBJECT_NAME VARCHAR2(30) Y OBJECT_ALIAS VARCHAR2(65) Y OBJECT_INSTANCE INTEGER Y OBJECT_TYPE VARCHAR2(30) Y OPTIMIZER VARCHAR2(255) Y SEARCH_COLUMNS NUMBER Y ID INTEGER Y PARENT_ID INTEGER Y DEPTH INTEGER Y POSITION INTEGER Y COST INTEGER Y CARDINALITY INTEGER Y BYTES INTEGER Y OTHER_TAG VARCHAR2(255) Y PARTITION_START VARCHAR2(255) Y PARTITION_STOP VARCHAR2(255) Y PARTITION_ID INTEGER Y OTHER LONG Y OTHER_XML CLOB Y DISTRIBUTION VARCHAR2(30) Y CPU_COST INTEGER Y IO_COST INTEGER Y TEMP_SPACE INTEGER Y ACCESS_PREDICATES VARCHAR2(4000) Y FILTER_PREDICATES VARCHAR2(4000) Y PROJECTION VARCHAR2(4000) Y TIME INTEGER Y QBLOCK_NAME VARCHAR2(30) Y
其中常用字段說明
創建自己的解釋計劃
select id,parent_id, lpad(' ',level)||operation||' ' ||options||' '||object_name as operation from plan_table start with id=0 connect by prior id = parent_id;
結果為
ID PARENT_ID OPERATION --------------------------------------- --------------------------------------- ----------------------------------------- 0 SELECT STATEMENT 1 0 HASH JOIN OUTER 2 1 TABLE ACCESS BY INDEX ROWID TABLE1 3 2 INDEX RANGE SCAN INDEX_TABLE1_ID 4 1 TABLE ACCESS FULL TABLE2
通過查看解釋計劃,可以針對性的修改自己的SQL語句來提升效率。比如修改或者增加索引等等
查看執行計劃與此相似,可以通過dbms.display_cursor函數來查看,也可以通過查詢V$SQL_PLAN_STATISTICS_ALL中的相關字段來查看。