dbms_xplan.display_cursor
执行示例如下,以下模拟你执行过的语句SQL> BEGIN2FOR cur IN (SELECT deptno FROM dept) LOOP3EXECUTE IMMEDIATE 'select /*1234*/ /*+ GATHER_PLAN_STATISTICS*/ * from emp where deptno = :deptno'4
执行查询前先改下参数:
SQL> show parameter level
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
plsql_optimize_level integer 2
statistics_level string TYPICAL
SQL> alter session set statistics_level = all;
会话已更改。
执行示例如下,以下模拟你执行过的语句
SQL> BEGIN
2 FOR cur IN (SELECT deptno FROM dept) LOOP
3 EXECUTE IMMEDIATE 'select /*1234*/ /*+ GATHER_PLAN_STATISTICS*/ * from emp where deptno = :deptno'
4 USING cur.deptno;
5 END LOOP;
6 END;
7 /
PL/SQL procedure successfully completed
SQL> SELECT v.SQL_ID,v.SQL_TEXT FROM v$sql v WHERE v.SQL_TEXT LIKE '%1234%';
SQL_ID SQL_TEXT
------------- --------------------------------------------------------------------------------
7tjhpj1f4sb46 BEGIN FOR cur IN (SELECT deptno FROM dept) LOOP EXECUTE IMMEDIATE 'select
8cytzt7wnu03n select /*1234*/ /*+ GATHER_PLAN_STATISTICS*/ * from emp where deptno = :deptno
36k4hdz29mf45 SELECT v.SQL_ID,v.SQL_TEXT FROM v$sql v WHERE v.SQL_TEXT LIKE '%1234%'
这儿明显是8cytzt7wnu03n,然后查看对应的plan
SQL> select * from table(dbms_xplan.display_cursor(sql_id =>'8cytzt7wnu03n',cursor_child_no =>0,format =>'ALL ALLSTATS LAST NOTE ADVANCED -projection'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 8cytzt7wnu03n, child number 0
-------------------------------------
select /*1234*/ /*+ GATHER_PLAN_STATISTICS*/ * from emp where deptno =
:deptno
Plan hash value: 1223387170
--------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Byte
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 3 | 147
|* 2 | INDEX RANGE SCAN | IDX_DEPTNO_ENAME | 1 | 3 |
--------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / EMP@SEL$1
2 - SEL$1 / EMP@SEL$1
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.1')
DB_VERSION('11.2.0.1')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
INDEX_RS_ASC(@"SEL$1" "EMP"@"SEL$1" ("EMP"."DEPTNO" "EMP"."ENAME"))
END_OUTLINE_DATA
*/
Peeked Binds (identified by position):
--------------------------------------
1 - (NUMBER): 10
Predicate Information (identified by operation id):
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------
2 - access("DEPTNO"=:DEPTNO)
45 rows selected
也可以在sqlplus下面对刚刚执行过的sql查看plan
首先设置如下
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> set linesize 200 pagesize 200
SQL> show parameter level;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
plsql_optimize_level integer 2
statistics_level string TYPICAL
SQL> alter session set statistics_level=all
2 /
会话已更改。
然后执行sql
SQL> select * from emp inner join dept on dept.deptno = emp.deptno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DNAME SN DEPTNO DNAME LOC
---------- ---------- --------- ---------- -------------- ---------- ---------- ---------- -------------------------------------------------- ---------- ---------- -------------- -------------
7782 CLARK MANAGER 7839 09-6月 -81 2450 10 UNKNOWN 7 10 ACCOUNTING NEW YORK
7839 KING PRESIDENT 17-11月-81 5000 10 UNKNOWN 9 10 ACCOUNTING NEW YORK
7934 MILLER CLERK 7782 23-1月 -82 1300 10 UNKNOWN 14 10 ACCOUNTING NEW YORK
7566 JONES MANAGER 7839 02-4月 -81 2975 20 RESEARCH 4 20 RESEARCH DALLAS
7902 FORD ANALYST 7566 03-12月-81 3000 20 RESEARCH 13 20 RESEARCH DALLAS
7876 ADAMS CLERK 7788 23-5月 -87 1100 20 RESEARCH 11 20 RESEARCH DALLAS
7369 SMITH CLERK 7902 17-12月-80 800 20 RESEARCH 1 20 RESEARCH DALLAS
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20 RESEARCH 8 20 RESEARCH DALLAS
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 SALES 3 30 SALES CHICAGO
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 SALES 10 30 SALES CHICAGO
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 SALES 2 30 SALES CHICAGO
7900 JAMES CLERK 7698 03-12月-81 950 30 SALES 12 30 SALES CHICAGO
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 SALES 6 30 SALES CHICAGO
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 SALES 5 30 SALES CHICAGO
已选择14行。
ok,我们可以直接用display_cursor看plan了。
语句为
select * from table(dbms_xplan.display_cursor(sql_id =>NULL,cursor_child_no =>0,format =>'ALL ALLSTATS LAST NOTE ADVANCED -projection'));
SQL> select * from table(dbms_xplan.display_cursor(sql_id =>NULL,cursor_child_no =>0,format =>'ALL ALLSTATS LAST NOTE ADVANCED -projection'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 91r8jh6q152tc, child number 0
-------------------------------------
select * from emp inner join dept on dept.deptno = emp.deptno
Plan hash value: 844388907
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers | Reads | OMem | 1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 6 (100)| | 14 |00:00:00.02 | 7 | 2 | | | |
| 1 | MERGE JOIN | | 1 | 14 | 966 | 6 (17)| 00:00:01 | 14 |00:00:00.02 | 7 | 2 | | | |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 4 | 80 | 2 (0)| 00:00:01 | 4 |00:00:00.02 | 4 | 2 | | | |
| 3 | INDEX FULL SCAN | PK_DEPT | 1 | 4 | | 1 (0)| 00:00:01 | 4 |00:00:00.01 | 2 | 1 | | | |
|* 4 | SORT JOIN | | 4 | 14 | 686 | 4 (25)| 00:00:01 | 14 |00:00:00.01 | 3 | 0 | 2048 | 2048 | 2048 (0)|
| 5 | TABLE ACCESS FULL | EMP | 1 | 14 | 686 | 3 (0)| 00:00:01 | 14 |00:00:00.01 | 3 | 0 | | | |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$58A6D7F6
2 - SEL$58A6D7F6 / DEPT@SEL$1
3 - SEL$58A6D7F6 / DEPT@SEL$1
5 - SEL$58A6D7F6 / EMP@SEL$1
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.1')
DB_VERSION('11.2.0.1')
ALL_ROWS
OUTLINE_LEAF(@"SEL$58A6D7F6")
MERGE(@"SEL$1")
OUTLINE(@"SEL$2")
OUTLINE(@"SEL$1")
INDEX(@"SEL$58A6D7F6" "DEPT"@"SEL$1" ("DEPT"."DEPTNO"))
FULL(@"SEL$58A6D7F6" "EMP"@"SEL$1")
LEADING(@"SEL$58A6D7F6" "DEPT"@"SEL$1" "EMP"@"SEL$1")
USE_MERGE(@"SEL$58A6D7F6" "EMP"@"SEL$1")
END_OUTLINE_DATA
*/
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("DEPT"."DEPTNO"="EMP"."DEPTNO")
filter("DEPT"."DEPTNO"="EMP"."DEPTNO")
已选择51行。
SQL>
display函数
1、display函数的几个参数
table_name
指定计划表的名字,缺省值为'PLAN_TABLE'.
statement_id
SQL语句的ID号,是在生成执行计划时使用set statement_id名令设定的值,默认值为NULL,当使用默认值时,将显示最近
插入计划表中的执行计划(filter_preds参数的值为空时)
format
用于控制display函数输出的内容。其常用取值为basic,typical,serial,all,advanced。其中typical为缺省值
除了上述几个取值外,还可以配合一些额外的修饰符来显示不同的内容。如:
alias、bytes、cost、note、outline、parallel、paration、predicate等
常用取值组合修饰符的例子:
basic +predicate、basic +outline(需要某个修饰符使用"+"号来连接)
typical -bytes、typical +alias -bytes -cost(不需要某个修饰符使用"-"号来连接)
注:"+"号与"-"号前面应保留空格
filter_preds
过滤谓词。用于过滤从plan_table表中返回的记录。当该值为NULL时,执行计划显示最近插入计划表中的执行计划。
如:filter_preds=>'plan_id = 223'
2、format参数常用值描述
basic 仅仅显示最少的信息。基本上包括操作和操作的对象
typical 显示大部分信息。基本上包括除了别名,提纲和字段投影外的所有信息,此为缺省值。
serial 类似于typical,但不显示并行操作
all 显示除提纲之外的所有信息
advanced 显示所有信息
3、format参数修饰符
alias 控制包含查询块与别名的显示部分
bytes 控制执行计划表中字段bytes的显示
cost 控制执行计划表中字段cost的显示
note 控制包含注释信息的显示部分
outline 控制包含提纲信息的显示部分
parallel 控制包含并行处理信息的提示
partition 控制并行处理信息的显示,尤其是执行计划表中字段TQ、IN-OUT、PQ Distrib的显示
peeked_binds 控制包含绑定变量窥探部分的显示。仅当生成执行计划时使用了绑定变量是可见
predicate 控制包含谓词filter和access显示部分
projection 控制包含投影信息的显示部分
remote 控制远程执行的SQL语句的显示
rows 控制执行计划表中字段rows的显示
更多推荐
所有评论(0)