查看执行计划之AUTOTRACE开关
在sqlplus中将autotrace开关打开也能得到目标sql的执行计划,而且,除此之外还能得到目标sql在执行时的资源消耗量,即通过设置AUTOTRACE开关我们可以额外观察到目标sql执行时所耗费的物理读,逻辑读,产生redo的数量已经排序的数量等。
(1)在sqlplus的当前session中执行命令set autotrace on,可以在当前session中将autotrace开关完全打开。这样,在这个session中随后执行的所有sql除了显示sql执行结果之外,还会额外显示这些sql所对应的的执行计划和资源消耗情况。
(2)在sqlplus的当前session中执行命令set autotrace off,可以在当前session中将autotrace开关关闭,这样,在这个session中随后执行的所有sql都只会显示sql执行结果,autotrace开关默认值是OFF。
(3)在sqlplus的当前session中执行set autotrace traceonly,可以在当前session中将autotrace开关以不显示sql执行结果的具体内容的方式完全打开,这种方式与set autotrace on的唯一区别就在于,对于set autotrace traceonly而言,oracle 只会显示sql执行结果的数量,而不会显示执行结果的具体内容,这种情况下我们往往并不关心这些sql的执行结果的具体内容,而只是关心他们的执行计划和资源消耗情况。
(4)在sqlplus的当前session中执行set autotrace traceonly explain,可以在当前session中将autotrace开关以只显示sql执行计划的范式打开。这种方式与set autotrace traceonly的区别就在于,set autotrace traceonly explain不会显示目标sql的资源消耗量和执行结果,而只会显示目标sql的执行计划。
(5)在sqlplus的当前session中执行命令set autotrace traceonly statistic,可以在当前session中将autotrace开关以只显示sql的资源消耗方式打开。这种方式与set autotrace traceonly的唯一区别在于,set autotrace traceonly statistic不显示目标sql的执行计划,而只会显示目标sql的执行结果的数量和资源消耗量。
设置autotrace开关的相关命令也沿用oracle一贯的可以使用简写的惯例,具体来说:
关键字autotrace 可以简写为autot
关键字traceonly可以简写为trace
关键字explain可以简写为exp
关键字statistic可以简写为stat
按照上述简写规则,如下的写法都是等价的
set autotrace on简写为set autot on
set autotrace off 简写为 set autot off
set autotrace traceonly 简写为 set autot trace
set autotrace traceonly explain 简写为set autot trace exp
set autotrace traceonly statistic 简写为set autot trace stat
这里还是以select empno,ename,dname from emp a,dept b where a.deptno=b.deptno;来说明:
先使用set autotrace on
SQL> select empno,ename,dname from emp a,dept b where a.deptno=b.deptno;
EMPNO ENAME DNAME
---------- ---------- --------------
7782 CLARK ACCOUNTING
7839 KING ACCOUNTING
7934 MILLER ACCOUNTING
7566 JONES RESEARCH
7902 FORD RESEARCH
7876 ADAMS RESEARCH
7369 SMITH RESEARCH
7788 SCOTT RESEARCH
7521 WARD SALES
7844 TURNER SALES
7499 ALLEN SALES
EMPNO ENAME DNAME
---------- ---------- --------------
7900 JAMES SALES
7698 BLAKE SALES
7654 MARTIN SALES
14 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 844388907
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 364 | 6 (17)| 00:00:01 |
| 1 | MERGE JOIN | | 14 | 364 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 52 | 2 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)| 00:00:01 |
|* 4 | SORT JOIN | | 14 | 182 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 14 | 182 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("A"."DEPTNO"="B"."DEPTNO")
filter("A"."DEPTNO"="B"."DEPTNO")
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
10 consistent gets
0 physical reads
0 redo size
941 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
14 rows processed
我们可以看到,执行set autotrace on后再执行目标sql,除了显示具体的执行结果之外还会显示该sql的执行计划和资源消耗情况。
接着使用set autotrace traceonly
SQL> select empno,ename,dname from emp a,dept b where a.deptno=b.deptno;
14 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 844388907
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 364 | 6 (17)| 00:00:01 |
| 1 | MERGE JOIN | | 14 | 364 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 52 | 2 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)| 00:00:01 |
|* 4 | SORT JOIN | | 14 | 182 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 14 | 182 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("A"."DEPTNO"="B"."DEPTNO")
filter("A"."DEPTNO"="B"."DEPTNO")
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
10 consistent gets
0 physical reads
0 redo size
941 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
14 rows processed
我们可以看到,执行set autotrace traceonly后再执行目标sql,除了该sql的执行结果的具体内容没有显示出来之外,其他的都和使用set autotrace on的显示结果一模一样。
我们来使用set autotrace traceonly explain
SQL> set autotrace traceonly explain
SQL> select empno,ename,dname from emp a,dept b where a.deptno=b.deptno;
Execution Plan
----------------------------------------------------------
Plan hash value: 844388907
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 364 | 6 (17)| 00:00:01 |
| 1 | MERGE JOIN | | 14 | 364 | 6 (17)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 52 | 2 (0)| 00:00:01 |
| 3 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)| 00:00:01 |
|* 4 | SORT JOIN | | 14 | 182 | 4 (25)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 14 | 182 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("A"."DEPTNO"="B"."DEPTNO")
filter("A"."DEPTNO"="B"."DEPTNO")
我们可以看到,执行set autotrace traceonly explain后再执行目标sql,只显示该sql的执行计划。
最后我们来使用set autotrace traceonly statistic
SQL> select empno,ename,dname from emp a,dept b where a.deptno=b.deptno;
14 rows selected.
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
10 consistent gets
0 physical reads
0 redo size
941 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
14 rows processed
我们可以看到,执行set autotrace traceonly statistic后再执行目标sql,只显示该sql的执行结果的数量和资源使用消耗量。