1.autotrace
set autotrace off
不产生autotrace报告,默认值
set autotrace on explain
autotrace报告只展示最优的执行方式(optimizer execution path)
SQL> set autotrace on explain;
—插入数据测试是否执行了sql
SQL> insert into scott.emp values('5566','explain','','','','','','');
1 row created.
—已经表明执行了sql
—只展示了执行计划
Execution Plan
----------------------------------------------------------
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | INSERT STATEMENT | | 1 | 100 | 1 (0)| ss00:00:01 |
| 1 | LOAD TABLE CONVENTIONAL | EMP | | | | |
---------------------------------------------------------------------------------
SQL> select * from scott.emp whereempno=5566;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ------------------- ---------- ---------- ----------
5566 explain
Execution Plan
----------------------------------------------------------
Plan hash value: 2949544139
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1| 87 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 87 | 2 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified byoperation id):
---------------------------------------------------
2- access("EMPNO"=5566)
set autotrace on statistics
autotrace报告sql执行统计信息(SQL statement execution statistics. )
SQL> set autotrace on statistics;
SQL> insert into scott.empvalues('6677','statistics','','','','','','');
1 row created.
—说明执行了sql
—只展示sql执行的统计信息
Statistics
----------------------------------------------------------
52 recursive calls
5 dbblock gets
80 consistent gets
0 physical reads
520 redo size
839 bytes sent via SQL*Net to client
826 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
10 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select * from scott.emp whereempno=6677;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ------------------- ---------- ---------- ----------
6677 statistics
Statistics
----------------------------------------------------------
1 recursive calls
0 dbblock gets
2 consistent gets
0 physical reads
0 redosize
869 bytes sent via SQL*Net to client
512 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
set autotrace on
autotrace报告包含了最优执行方式和sql执行的信息
SQL> set autotraceon;
SQL> insert into scott.emp values('8899','on','','','','','','');
1 row created.
—相当于set autotrace on explain和set autotrace on statistics的和,既输出执行计划,也输出统计信息,同时也执行sql
Execution Plan
----------------------------------------------------------
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | INSERT STATEMENT | | 1 | 100 | 1 (0)| 00:00:01 |
| 1 | LOAD TABLE CONVENTIONAL | EMP | | | | |
---------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
45 recursive calls
5 dbblock gets
77 consistent gets
0 physical reads
512 redo size
839 bytes sent via SQL*Net to client
816 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
10 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select * from scott.emp whereempno=8899;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ------------------- ---------- ---------- ----------
8899 on
Execution Plan
----------------------------------------------------------
Plan hash value: 2949544139
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1| 87 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 87 | 2 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified byoperation id):
---------------------------------------------------
2- access("EMPNO"=8899)
Statistics
----------------------------------------------------------
9 recursive calls
0 db blockgets
18 consistent gets
0 physical reads
0 redosize
861 bytes sent via SQL*Net to client
512 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
set autotrace traceonly
和set autotrace on类似,但是不输出用户查询的结果
SQL> set autotrace traceonly;
SQL> insert into scott.empvalues('9900','traceonly','','','','','','');
1 row created.
—与set autotrace on 类似,都会输出执行计划和统计信息
Execution Plan
----------------------------------------------------------
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | INSERT STATEMENT | | 1 | 100 | 1 (0)| 00:00:01 |
| 1 | LOAD TABLE CONVENTIONAL | EMP | | | | |
---------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
108 recursive calls
5 dbblock gets
191 consistent gets
1 physical reads
520 redo size
839 bytes sent via SQL*Net to client
825 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
15 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select * from scott.emp whereempno=9900;
—与set autotrace on不同的是,没有输出查询结果
Execution Plan
----------------------------------------------------------
Plan hash value: 2949544139
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1| 87 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 87 | 2 (0)| 00:00:01 |
|* 2 | INDEXUNIQUE SCAN | PK_EMP | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified byoperation id):
---------------------------------------------------
2- access("EMPNO"=9900)
Statistics
----------------------------------------------------------
9 recursive calls
0 dbblock gets
18 consistent gets
0 physical reads
0 redosize
867 bytes sent via SQL*Net to client
512 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
2.dbms_xplan
概述
dbms_xplan包提供了多样的,预定义的方式去解释执行计划。sql执行计划,执行时间等信息都存储与v$sql_plan,v$sql_plan_statistics_all视图中。此包运行的权限是执行用户,不是包的拥有者sys。
当执行display_awr时,需要DBA_HIST_SQL_PLAN, DBA_HIST_SQLTEXT, andV$DATABASE的查询权限
当执行display_cursor时需要V$SQL_PLAN, V$SESSION and V$SQL_PLAN_STATISTICS_ALL的查询权限
当执行display_sql_plan_baseline时,需要DBA_SQL_PLAN_BASELINES的查询权限
当执行display_sqlset时,需要ALL_SQLSET_STATEMENTS and ALL_SQLSET_PLANS的查询权限
以上的所有权限都在SELECT_CATALOG角色中,所以直接赋权SELECT_CATALOG就可以了。
dbms_xplan中有许多功能,我这里着重研究其中几个常用的
SQL> desc dbms_xplan;
…
FUNCTION DISPLAY_AWR RETURNSDBMS_XPLAN_TYPE_TABLE
Argument Name Type In/Out Default?
----------------------------------------------------- ------ --------
SQL_ID VARCHAR2 IN
PLAN_HASH_VALUE NUMBER(38) IN DEFAULT
DB_ID NUMBER(38) IN DEFAULT
FORMAT VARCHAR2 IN DEFAULT
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
FUNCTION DISPLAY_PLAN RETURNS CLOB
Argument Name Type In/Out Default?
----------------------------------------------------- ------ --------
TABLE_NAME VARCHAR2 IN DEFAULT
STATEMENT_ID VARCHAR2 IN DEFAULT
FORMAT VARCHAR2 IN DEFAULT
FILTER_PREDS VARCHAR2 IN DEFAULT
TYPE VARCHAR2 IN DEFAULT
…
dbms_xplan.display
展示执行计划内容
简单的执行一次display
SQL> explain plan for insert intoscott.emp values(1111,'xplan','','','','','','');
Explained.
SQL> select * from table(dbms_xplan.display);
7 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2137789089
---------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|Time |
---------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 8168 | 16336 | 29 (0)| 00:00:01 |
| 1 | COLLECTION ITERATOR PICKLER FETCH| DISPLAY | 8168 | 16336 | 29 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
1890 recursive calls
40 dbblock gets
3635 consistent gets
0 physical reads
0 redosize
1102 bytes sent via SQL*Net toclient
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
38 sorts (memory)
0 sorts (disk)
7 rowsprocessed
SQL> select * from scott.emp whereempno=1111;
no rows selected
—explain plan for ,dbms_xplan.display都没有执行sql。explain plan for是产生sql的执行计划并保存在PLAN_TABLE表中,dbms_xplan.display从PLAN_TABLE中提取计划并展示出来。
定义
DBMS_XPLAN.DISPLAY(
table_name IN VARCHAR2 DEFAULT 'PLAN_TABLE',
statement_id IN VARCHAR2 DEFAULT NULL,
format IN VARCHAR2 DEFAULT 'TYPICAL',
filter_preds IN VARCHAR2 DEFAULT NULL);
table_name:指定的是计划的存储表表名(不是执行表的表名)。默认是PLAN_TABLE。
statement_id:指定计划的statement_id,如果没有指定,则该值为explain plan的statement_id , 如果没有expain plan,则该值为最近一次解释的执行计划。
format:解释计划的level
• BASIC: Displays the minimum information inthe plan—the operation ID, the operation name and its option.展示最少的信息
• TYPICAL: This is the default. Displays themost relevant information in the plan (operation id, name and option, #rows,#bytes and optimizer cost). Pruning, parallel and predicate information areonly displayed when applicable. Excludes only PROJECTION, ALIAS and REMOTE SQLinformation (see below).默认值
• SERIAL: Like TYPICAL except that theparallel information is not displayed, even if the plan executes in parallel.没有并发信息
• ALL: Maximum user level. Includesinformation displayed with the TYPICAL level with additional information(PROJECTION, ALIAS and information about REMOTE SQL if the operation isdistributed).展示最多的信息,包含了分布式操作的远程sql执行信息
filter_preds:sql过滤,限制从计划表中返回的行。
dbms_xplan.display_awr
展示在awr中存储的sql的执行计划
定义
DBMS_XPLAN.DISPLAY_AWR(
sql_id IN VARCHAR2,
plan_hash_value IN NUMBER DEFAULT NULL,
db_id IN NUMBER DEFAULT NULL,
format IN VARCHAR2 DEFAULT TYPICAL);
sql_id:在dba_hist_sqltext中可以找到sql_id
plan_hash_value:sql执行计划的hash值。如果该值被忽略,则函数返回sql_id中的所有执行计划
db_id:database_id。如果不知道则该值为V$DATABASE视图中的database_id,也就是本地数据库。
format:与display类似。总共有4个level:BASIC,TYPICAL,SERIAL,ALL。
SQL> select * from scott.emp whererownum<=1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ------------------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
—手动生成快照
SQL> EXECUTE DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT();
PL/SQL procedure successfully completed.
SQL> select sql_id,sql_text from dba_hist_sqltext where sql_text like '%rownum%';
bd3tcy3ar02px
select * from scott.emp where rownum<=1
SQL>
—指定awr中存在sql
SQL> select * fromtable(dbms_xplan.display_awr('bd3tcy3ar02px'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID bd3tcy3ar02px
--------------------
select * from scott.emp where rownum<=1
Plan hash value: 1973284518
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | COUNT STOPKEY | | | | | |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
| 2 | TABLE ACCESS FULL| EMP | 15 | 1305 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------
Note
-----
-dynamic sampling used for this statement (level=2)
18 rows selected.
SQL>
dbms_xplan.display_cursor
展示cursor中的执行计划
定义
DBMS_XPLAN.DISPLAY_CURSOR(
sql_id IN VARCHAR2 DEFAULT NULL,
cursor_child_no IN NUMBER DEFAULT 0,
format IN VARCHAR2 DEFAULT ‘TYPICAL');
sql_id
cursor_child_no:子游标标志。如果没有指定,则展示sql_id下的所有执行计划
format 展示方式
默认值执行
SQL> select * from scott.emp whererownum<=2;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ------------------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
—默认查询了当前session最后一次执行sql
SQL> select * fromtable(dbms_xplan.display_cursor);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 90ud69jbjz75c,child number 0
-------------------------------------
select * from scott.emp where rownum<=2
Plan hash value: 1973284518
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | COUNT STOPKEY | | | | | |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
| 2 | TABLE ACCESS FULL| EMP | 15 | 1305 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified byoperation id):
---------------------------------------------------
1- filter(ROWNUM<=2)
Note
-----
-dynamic sampling used for this statement (level=2)
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
23 rows selected.
SQL>
指定sql_id查找plan,首先要找到sql_id
SQL> select sql_id,sql_text fromv$sqlarea where sql_text like '%rownum<=2%';
SQL_ID
-------------
SQL_TEXT
--------------------------------------------------------------------------------
75gpskbx0uk8w
select sql_id,sql_text from v$sqlarea wheresql_text like '%rownum<=2%'
90ud69jbjz75c
select * from scott.emp where rownum<=2
SQL> select * fromtable(dbms_xplan.display_cursor('90ud69jbjz75c'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 90ud69jbjz75c,child number 0
-------------------------------------
select * from scott.emp where rownum<=2
Plan hash value: 1973284518
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | COUNT STOPKEY | | | | | |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
| 2 | TABLE ACCESS FULL| EMP | 15 | 1305 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified byoperation id):
---------------------------------------------------
1- filter(ROWNUM<=2)
Note
-----
-dynamic sampling used for this statement (level=2)
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
23 rows selected.
如果sql不在内存中,则不能通过cursor方式查询
SQL> select * fromtable(dbms_xplan.display_cursor('90ud69jbjz75c'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID: 90ud69jbjz75c, child number: 0cannot be found
这时就可以通过生成快照的方式从awr中查询plan
3.10046事件
概述
这是一个特殊的事件,可以被用来发起sql_trace类的活动
使用10046的优势就是可以在trace文件中看到详细的数据库活动细节,细节信息也取决于事件的level。
level
1:标准的sql_trace
4:level 1加上绑定变量 [ bind=true ]
8:levle 1加上等待。在定位等待事件的时候很有用 [ wait=true ]
12:常用的level,包含了level 1,绑定变量,等待,默认stat line。相当于(4+8)
16:每次执行都生成stat line。11g新增。 [ plan_stat=all_executions ]
32:不生成执行统计。11g新增 。[ plan_stat=never ]
64:Adaptive dump of STAT lines. This dumps the STATinformation if a SQL took more than about 1 minute thereby giving informationfor the more expensive SQLs and for different executions of such SQLs。11gR2新增。 [ plan_stat=adaptive ]
使用10046事件
session级追踪
—查看trace的位置
SQL> show parameter user_dump_dest
NAME TYPE VALUE
----------------------------------------------- ------------------------------
user_dump_dest string /u01/app/oracle/diag/rdbms/zld
b/zldb/trace
—tracefile标志
SQL> alter session settracefile_identifier='liuzhilong';
Session altered.
—开启10046
SQL> alter session set events '10046trace name context forever,level 12';
Session altered.
SQL> select * from scott.emp whererownum<=1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ------------------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
SQL>
退出sqlplus(session级)或者使用以下语句关闭事件
alter session set events '10046 trace namecontext off’;
[root@lzl Desktop]# cd/u01/app/oracle/diag/rdbms/zldb/zldb/trace/
[root@lzl trace]# ls *liu*
zldb_ora_2990_liuzhilong.trc zldb_ora_2990_liuzhilong.trm
使用oradebug工具
SQL> oradebug setmypid
Statement processed.
SQL> oradebug event 10046 trace name context forever, level 12;
Statement processed.
SQL> select ename from scott.emp whererownum<=1;
ENAME
----------
SMITH
SQL> oradebug tracefile_name;
/u01/app/oracle/diag/rdbms/zldb/zldb/trace/zldb_ora_2990.trc
SQL>
参考文档
AUTOTRACE Option inSQL*Plus (文档 ID 43214.1)
Display Execution Plans withDBMS_XPLAN.DISPLAY (文档 ID 236026.1)
Using DBMS_XPLAN.DISPLAY_CURSOR toIdentify Bind Values Used During Parse (文档 ID 416324.1)
EVENT: 10046 "enable SQL statementtracing (including binds/waits)" (文档 ID 21154.1)
* How To Collect 10046 Trace (SQL_TRACE)Diagnostics for Performance Issues (文档 ID 376442.1)