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%';

其他参数请参考官档:https://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_xplan.htm#ARPLS70125