今天在测试过程中,偶然发现在Oracle10g Release 2中,Autotrace的功能已经被极大加强和改变。
这是一个很有意思的小的变化,关于autotrace的使用,请参考:启用AutoTrace
接下来让我们先来看一下什么地方发生了改变: SQL> set linesize 120
SQL> set autotrace on
SQL> select count(*) from v$session
2/
COUNT(*)
----------
21
Execution Plan
----------------------------------------------------------
Plan hash value: 2376410614
---------------------------------------------------------------------------------------------
Id Operation Name Rows Bytes Cost (%CPU) Time
---------------------------------------------------------------------------------------------
0 SELECT STATEMENT 1 65 0 (0) 00:00:01
1 SORT AGGREGATE 1 65
2 NESTED LOOPS 1 65 0 (0) 00:00:01
*3 FIXED TABLE FULL X$KSUSE 1 52 0 (0) 00:00:01
*4 FIXED TABLE FIXED INDEX X$KSLED (ind:2) 1 13 0 (0) 00:00:01
---------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("S"."INST_ID"=USERENV('INSTANCE') AND BITAND("S"."KSSPAFLG",1)<>0 AND
BITAND("S"."KSUSEFLG",1)<>0)
4 - filter("S"."KSUSEOPC"="E"."INDX")
Statistics
----------------------------------------------------------
1recursive calls
0db block gets
0consistent gets
0physical reads
0redo size
411bytes sent via SQL*Net to client
385bytes received via SQL*Net from client
2SQL*Net roundtrips to/from client
0sorts (memory)
0sorts (disk)
1rows processed
注重,此时autotrace的输出被良好格式化,并给出关于执行计划部分的简要注释。
其实这里并没有带来新的东西,从Oracle9i开始,Oracle提供了一个新的工具dbms_XPlan用以格式化和查看SQL的执行计划。其原理是通过对plan_table的查询和格式化提供更友好的用户输出。
dbms_xplan的调用的语法类似:
select * from table(dbms_xplan.display(format=>'BASIC'))
使用 TABLE() 操作符,或者 CAST 操作。
具体用法可以参考Oracle官方文档。
实际上从Oracle9i开始我们就经常使用如下方式调用dbms_xplan:
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - ProdUCtion
With the Partitioning option
JServer Release 9.2.0.4.0 - Production
SQL> explain plan for
2select count(*) from dual;
Explained.
SQL> @?/rdbms/admin/utlxplp;
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
--------------------------------------------------------------------
Id OperationName Rows Bytes Cost
--------------------------------------------------------------------
0 SELECT STATEMENT
1 SORT AGGREGATE
2 TABLE Access FULL DUAL
--------------------------------------------------------------------
Note: rule based optimization
10 rows selected.
utlxplp.sql脚本中正是调用了dbms_xplan:
SQL> get ?/rdbms/admin/utlxplp;
1Rem
2Rem $Header: utlxplp.sql 23-jan-2002.08:55:23 bdagevil Exp $
3Rem
4Rem utlxplp.sql
5Rem
6Rem Copyright (c) 1998, 2002, Oracle Corporation.All rights reserved.
7Rem
8RemNAME
9Remutlxplp.sql - UTiLity eXPLain Parallel plans
10Rem
11RemDESCRIPTION
12Remscript utility to display the explain plan of the last explain plan
13Rem command. Display also Parallel Query information if the plan happens to
14Remrun parallel
15Rem
16RemNOTES
17RemAssume that the table PLAN_TABLE has been created. The script
18Remutlxplan.sql should be used to create that table
19Rem
20RemWith SQL*plus, it is recomended to set linesize and pagesize before
21Remrunning this script. For example:
22Remset linesize 130
23Remset pagesize 0
24Rem
25RemMODIFIED (MM/DD/YY)
26Rembdagevil01/23/02 - rewrite with new dbms_xplan package
27Rembdagevil04/05/01 - include CPU cost
28Rembdagevil02/27/01 - increase Name column
29Remjihuang 06/14/00 - change order by to order siblings by.
30Remjihuang 05/10/00 - include plan info for recursive SQL in LE row source
31Rembdagevil01/05/00 - make deterministic with order-by
32Rembdagevil05/07/98 - Explain plan script for parallel plans
33Rembdagevil05/07/98 - Created
34Rem
35set markup Html preformat on
36Rem
37Rem Use the display table function from the dbms_xplan package to display the last
38Rem explain plan. Use default mode which will display only relevant information
39Rem
40* select * from table(dbms_xplan.display());
41
SQL>
而在Oracle10gR2中,Oracle帮我们简化了这个过程,一个autotrace就完成了所有的输出,这也是易用性上的一个进步吧。在使用 Oracle的过程中,我们经常能够感受到Oracle针对用户需求或易用性的改进,这也许是很多人喜爱Oracle的一个原因吧。
假如足够信息我们还会注重到,在Oracle10g中PLAN_TABLE不再需要创建,Oracle缺省增加了一个字典表PLAN_TABLE$,然后基于PLAN_TABLE$创建公用同义词供用户使用。
关于dbms_xplan工具的使用还可以参考Itpub上的讨论:使用dbms_xplan工具查看执行计划.