ORACLE的执行计划分为预估执行计划和实际执行计划。其中,你用Toad、PL/SQL Developer、SQL Developer、EXPLAIN PLAN FOR或者SET ATUOTRACE TRACEONLY等获取的执行计划都是预估的执行计划。有时候预估执行计划和实际执行计划有很大的差别,所以有时候,调优的时候需要对比实际执行计划和预估的执行计划,不能被预估的执行计划给欺骗了。那么我们怎么查看实际的执行计划呢?
方法1:查询v$sql_plan视图中的实际执行计划
1:在窗口执行下面SQL语句
SELECT ENAME, SAL FROM SCOTT.EMP, SCOTT.DEPT
WHERE EMP.DEPTNO =DEPT.DEPTNO
AND DEPT.LOC='CHICAGO';
2:查看执行SQL的SQL_ID
SELECT SQL_ID, SQL_TEXT FROM V$SQL WHERE SQL_TEXT LIKE '%SELECT ENAME, SAL FROM SCOTT.EMP, SCOTT.DEPT%'
3: 查看v$sql_plan视图
SELECT * FROM v$sql_plan WHERE SQL_ID='7m5qbhn98j1xw';
通过v$sql_plan视图查看实际执行计划,其实是不太现实的,因为阅读困难,可读性差,尤其是复杂SQL语句。
方法2:SET AUTOTRACE ON查看实际执行计划
其实这种方式有很大的限制,例如SQL输出大量的记录,那么在SQL Plus里面就会刷屏,而且等待的时间较长;另外DML操作不能用这种方法。所以其实也并不实用,只是作为一种方法罗列于此。
方法3:DBMS_XPLAN.DISPLAY_CURSOR查看实际执行计划
DBMS_XPLAN这个包最初是在ORACLE 9i R2中引入的,用来查看Explain Plan生成的执行计划。DBMS_XPLAN 在ORACLE 10g 以及ORACLE 11g中,功能都有所增长。
在ORACLE 10g提供了下面4个函数的功能。
· DISPLAY - to format and display the contents of a plan table.
· DISPLAY_CURSOR - to format and display the contents of the execution plan of any loaded cursor.
· DISPLAY_AWR - to format and display the contents of the execution plan of a stored SQL statement in the AWR.
· DISPLAY_SQLSET - to format and display the contents of the execution plan of statements stored in a SQL tuning set.
在ORACLE 11g提供了下面5个函数的功能。
· 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.
这里不打算详细介绍DBMS_XPLAN各个函数的功能,具体可以参考官方文档。这里只介绍DISPLAY_CURSOR函数,它显示存储在库缓存(library cache)中的实际执行计划,当然你要查询某个SQL语句的实际执行计划,前提是这个SQL的执行计划还在库缓存中,如果它已经被刷出库缓存,就无法获取其实际执行计划。
DISPLAY_CURSOR 的参数介绍如下:
FUNCTION DISPLAY_CURSOR RETURNS DBMS_XPLAN_TYPE_TABLE
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
SQL_ID VARCHAR2 IN DEFAULT
CURSOR_CHILD_NO NUMBER(38) IN DEFAULT
FORMAT VARCHAR2 IN DEFAULT
SQL_ID
指定位于库缓存执行计划中SQL语句的父游标。默认值为null。当使用默认值时当前会话的最后一条SQL语句的执行计划将被返回
可以通过查询V$SQL 或 V$SQLAREA的SQL_ID列来获得SQL语句的SQL_ID。
CURSOR_CHILD_NO
指定父游标下子游标的序号。即指定被返回执行计划的SQL语句的子游标。默认值为0。如果为null,则sql_id所指父游标下所有子游标的执行计划都将被返回。
FORMAT
控制SQL语句执行计划的输出部分,即哪些可以显示哪些不显示。使用与display函数的format参数与修饰符在这里同样适用。
除此之外当在开启statistics_level=all时或使用gather_plan_statistics提示可以获得执行计划中实时的统计信息
· BASIC: Displays the minimum information in the plan—the operation ID, the operation name and its option.
BASIC:仅显示最少信息。基本上只包括操作ID、操作名称和操作对象。
· TYPICAL: This is the default. Displays the most relevant information in the plan (operation id, name and option, #rows, #bytes and optimizer cost). Pruning, parallel and predicate information are only displayed when applicable. Excludes onlyPROJECTION, ALIAS and REMOTE SQL information (see below).
TYPICAL:这个是默认值,显示执行计划中大部分信息(operation id, name and option, #rows, #bytes and optimizer cost),并行、谓词信息等, 除了别名,提纲和字段投影外。
· SERIAL: Like TYPICAL except that the parallel information is not displayed, even if the plan executes in parallel.
SERIAL:跟TYPICAL类似,除了并行操作信息没有显示,即使执行计划有并行处理。
· ALL: Maximum user level. Includes information displayed with theTYPICAL level with additional information (PROJECTION, ALIASand information about REMOTE SQL if the operation is distributed).
ALL:显示所有信息。
下面我们来看看,首先我们执行下面一个SQL语句
SQL> SELECT ENAME, SAL
2 FROM SCOTT.EMP E, SCOTT.DEPT D
3 WHERE E.DEPTNO =D.DEPTNO
4 AND D.LOC='CHICAGO';
ENAME SAL
---------- ----------
ALLEN 1600
WARD 1250
MARTIN 1250
BLAKE 2850
TURNER 1500
JAMES 950
6 rows selected.
然后查询v$sql视图,找到该语句的sql_id,有可能该SQL语句不在Share Pool里面了,此时表明该SQL已经被踢出Share Pool
SQL> SELECT SQL_ID,CHILD_NUMBER
2 FROM V$SQL
3 WHERE SQL_TEXT LIKE '%SELECT ENAME, SAL FROM SCOTT.EMP E, SCOTT.DEPT D WHERE E.DEPTNO =D.DEPTNO AND D.LOC=''CHICAGO''%';
SQL_ID CHILD_NUMBER
------------- ------------
7wga0v6nhkjug 0
SQL>
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('7wga0v6nhkjug',0));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 7wga0v6nhkjug, child number 0
-------------------------------------
SELECT ENAME, SAL FROM SCOTT.EMP E, SCOTT.DEPT D WHERE E.DEPTNO
=D.DEPTNO AND D.LOC='CHICAGO'
Plan hash value: 615168685
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 5 (100)| |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|* 1 | HASH JOIN | | 5 | 120 | 5 (20)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| DEPT | 1 | 11 | 2 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMP | 14 | 182 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."DEPTNO"="D"."DEPTNO")
2 - filter("D"."LOC"='CHICAGO')
22 rows selected.
SQL>
不传递任何参数给display_cursor函数,表示显示当前会话最后一条SQL语句的执行计划.例如,我在SQL*Plus中执行下面SQL语句
SQL> SELECT * FROM SCOTT.DEPT;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(null,null));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 4db4txmchwqqh, child number 0
-------------------------------------
SELECT * FROM SCOTT.DEPT
Plan hash value: 3383998547
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | TABLE ACCESS FULL| DEPT | 4 | 80 | 2 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------
13 rows selected.
参考资料:
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_xplan.htm#CACFJGHG
https://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_xplan.htm