创建自己的oracle解释计划

 

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   

 其中常用字段说明

创建自己的oracle解释计划_第1张图片

创建自己的解释计划

 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中的相关字段来查看。

  

  

你可能感兴趣的:(oracle,工作,解释计划)