10046入门:使用 10046 查看执行计划并读懂 trace文件

*************************************************************
1.开启10046
*************************************************************

SQL> oradebug setmypid
Statement processed.  

// 激活 10046 事件
SQL> oradebug event 10046 trace name context forever,level 12;
Statement processed.  

SQL> select count(*) from scott.tblorders;

  COUNT(*)
----------
       167

// 在当前 session 关闭 10046 事件
SQL> oradebug event 10046 trace name context off;
Statement processed.  

// 使用 oradebug tracefile_name 可以直接看到生成的 trace 文件的位置
SQL> oradebug tracefile_name;
/u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_2551.trc

*************************************************************
2.直接读取文件
*************************************************************

----------------------trace文件内容

Trace file /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_2551.trc
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1
System name:	Linux
Node name:	timesten.ocp.com
Release:	2.6.32-431.el6.x86_64
Version:	#1 SMP Fri Nov 22 03:15:09 UTC 2013
Machine:	x86_64
Instance name: orcl
Redo thread mounted by this instance: 1
Oracle process number: 20
Unix process pid: 2551, image: [email protected] (TNS V1-V3)

*** 2015-02-10 17:34:40.127
*** SESSION ID:(132.9911) 2015-02-10 17:34:40.127
*** CLIENT ID:() 2015-02-10 17:34:40.127
*** SERVICE NAME:(SYS$USERS) 2015-02-10 17:34:40.127
*** MODULE NAME:([email protected] (TNS V1-V3)) 2015-02-10 17:34:40.127
*** ACTION NAME:() 2015-02-10 17:34:40.127

Processing Oradebug command 'setmypid'

*** 2015-02-10 17:34:40.127
Oradebug command 'setmypid' console output: <none>

*** 2015-02-10 17:34:48.483
Processing Oradebug command 'event 10046 trace name context forever,level 12'

*** 2015-02-10 17:34:48.484
Oradebug command 'event 10046 trace name context forever,level 12' console output: <none>
WAIT #0: nam='SQL*Net message to client' ela= 2 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1423560888484461

*** 2015-02-10 17:35:09.044
WAIT #0: nam='SQL*Net message from client' ela= 20559954 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1423560909044435
=====================
PARSING IN CURSOR #140199425457600 len=37 dep=0 uid=0 oct=3 lid=0 tim=1423560909045924 hv=2120411436 ad='28d609c60' sqlid='23uhtxpz65u9c'
 select count(*) from scott.tblorders
END OF STMT
PARSE #140199425457600:c=1000,e=1393,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,plh=1853518045,tim=1423560909045922
EXEC #140199425457600:c=0,e=54,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=1853518045,tim=1423560909046084
WAIT #140199425457600: nam='SQL*Net message to client' ela= 2 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1423560909046130
FETCH #140199425457600:c=0,e=99,p=0,cr=1,cu=0,mis=0,r=1,dep=0,og=1,plh=1853518045,tim=1423560909046258
STAT #140199425457600 id=1 cnt=1 pid=0 pos=1 obj=0 op='SORT AGGREGATE (cr=1 pr=0 pw=0 time=110 us)'
STAT #140199425457600 id=2 cnt=167 pid=1 pos=1 obj=89045 op='INDEX FULL SCAN PK_ORDERID (cr=1 pr=0 pw=0 time=76 us cost=1 size=0 card=167)'
WAIT #140199425457600: nam='SQL*Net message from client' ela= 148 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1423560909046527
FETCH #140199425457600:c=0,e=1,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=0,plh=1853518045,tim=1423560909046560
WAIT #140199425457600: nam='SQL*Net message to client' ela= 1 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1423560909046581

*** 2015-02-10 17:35:27.589
WAIT #140199425457600: nam='SQL*Net message from client' ela= 18543034 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1423560927589630
CLOSE #140199425457600:c=0,e=10,dep=0,type=0,tim=1423560927589767

*** 2015-02-10 17:35:27.589
Processing Oradebug command 'event 10046 trace name context off'

*** 2015-02-10 17:35:27.590
Oradebug command 'event 10046 trace name context off' console output: <none>

*** 2015-02-10 17:35:35.918
Processing Oradebug command 'tracefile_name'

*** 2015-02-10 17:35:35.918
Oradebug command 'tracefile_name' console output:
/u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_2551.trc

***************************解释******************************************************

第1部分:parse(解析) 

PARSING IN CURSOR #140199425457600 len=37 dep=0 uid=0 oct=3 lid=0 tim=1423560909045924 hv=2120411436
ad='28d609c60' sqlid='23uhtxpz65u9c'

cursor	 cursor number
 len		sql 语句长度
 dep		sql 语句递归深度
 uid		user id
 oct		oracle command type
 lid		privilege user id
 tim		timestamp,时间戳
 hv		hash id
 ad		sql address 地址, 用在 v$sqltext
 sqlid	 sql id

第2部分:exec(执行)

PARSE #140199425457600:c=1000,e=1393,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,plh=1853518045,tim=1423560909045922
EXEC #140199425457600:c=0,e=54,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=1853518045,tim=1423560909046084

c	 CPU 消耗的时间
 e	 Elapsed time
 p	 number of physical reads 物理读的次数
 cr	 number of buffers retrieved for CR reads   逻辑读的数据块
 cu	 number of buffers retrieved in current mode (current 模式读取的数据块)
 mis	 cursor missed in the cache 库缓存中丢失的游标, 硬解析次数
 r	 number of rows processed 处理的行数
 dep	 递归深度
 og	 optimizer mode 【1:all_rows, 2:first_rows, 3:rule, 4:choose】
 plh	 plan hash value
 tim	 timestamp 时间戳

第3部分:执行过程中的发生的等待事件

WAIT #0: nam='SQL*Net message from client' ela= 20559954 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1423560909044435

nam	 an event that we waited for 等待事件
 ela	 此操作消耗的时间
 p3	 block 块号
 trm	 timestamp 时间戳

第4部分:该游标的执行计划

STAT #140199425457600 id=1 cnt=1 pid=0 pos=1 obj=0 op='SORT AGGREGATE (cr=1 pr=0 pw=0 time=110 us)'
STAT #140199425457600 id=2 cnt=167 pid=1 pos=1 obj=89045 op='INDEX FULL SCAN PK_ORDERID
(cr=1 pr=0 pw=0 time=76 us cost=1 size=0 card=167)'

cnt	 当前行源返回的行数
 pid	 parent id of this row source 当前行源的父结点 id
 pos	 position in explain plan 执行计划的位置
 obj	 object id of row source (if this is a base object)
 op 	 the row source access operation

******************************************************************************************************************************************************************

*************************************************************
3.使用 tkprof 命令翻译 trace 文件
*************************************************************

tkprof  /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_2551.trc ./test.trc

TKPROF: Release 11.2.0.4.0 - Development on 星期二 2月 10 17:47:28 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Trace file: /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_2551.trc
Sort options: default

********************************************************************************
count    = number of times OCI procedure was executed
cpu      = cpu time in seconds executing
elapsed  = elapsed time in seconds executing
disk     = number of physical reads of buffers from disk
query    = number of buffers gotten for consistent read
current  = number of buffers gotten in current mode (usually for update)
rows     = number of rows processed by the fetch or execute call
********************************************************************************

SQL ID: 23uhtxpz65u9c Plan Hash: 1853518045

select count(*)
from
 scott.tblorders

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        2      0.00       0.00          0          1          0           1
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.00       0.00          0          1          0           1

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: SYS
Number of plan statistics captured: 1

Rows (1st) Rows (avg) Rows (max)  Row Source Operation
---------- ---------- ----------  ---------------------------------------------------
         1          1          1  SORT AGGREGATE (cr=1 pr=0 pw=0 time=110 us)
       167        167        167   INDEX FULL SCAN PK_ORDERID (cr=1 pr=0 pw=0 time=76 us cost=1 size=0 card=167)(object id 89045)

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       2        0.00          0.00
  SQL*Net message from client                     2       18.54         18.54

********************************************************************************

OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          0          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        2      0.00       0.00          0          1          0           1
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.00       0.00          0          1          0           1

Misses in library cache during parse: 1

Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       3        0.00          0.00
  SQL*Net message from client                     3       20.55         39.10

OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        0      0.00       0.00          0          0          0           0
Execute      0      0.00       0.00          0          0          0           0
Fetch        0      0.00       0.00          0          0          0           0
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        0      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0

    1  user  SQL statements in session.
    0  internal SQL statements in session.
    1  SQL statements in session.
********************************************************************************
Trace file: /u01/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_2551.trc
Trace file compatibility: 11.1.0.7
Sort options: default

       1  session in tracefile.
       1  user  SQL statements in trace file.
       0  internal SQL statements in trace file.
       1  SQL statements in trace file.
       1  unique SQL statements in trace file.
      66  lines in trace file.
       0  elapsed seconds in trace file.

时间: 2024-10-26 10:29:50

10046入门:使用 10046 查看执行计划并读懂 trace文件的相关文章

使用 10046 查看执行计划并读懂 trace 文件

查看 sql 执行计划的方法有许多种, 10046 事件就是其中的一种. 与其他查看 sql 执行计划不同, 当我们遇到比较复杂的 sql 语句, 我们可以通过 10046 跟踪 sql 得到执行计划中每一个步骤的逻辑读, 物理读以及花费的时间等. 这种细粒度的跟踪对于我们分析 sql 性能尤其有用. 一般来说, 使用 10046 事件得到 sql 执行计划的步骤如下: 1. 激活当前 session 10046 事件 2. 在当前 session 中执行 sql 语句 3. 关闭当前 sess

oracle查看执行计划入门

基于Oracle的应用系统很多的性能问题都是由应用系统的SQL性能低劣引起的,因此SQL的性能优化非常重要.要分析与优化SQL的性能,一般是通过查看该SQL的执行计划,然后通过执行计划有针对性地对SQL进行相应的优化. 什么是执行计划(Explain Plan) 执行计划是一条查询语句在Oracle中的执行过程或访问路径的描述. SQL是一门傻瓜式语言,每一个条件就是一个需求,访问的顺序不同就形成了不同的执行计划.Oracle必须要做出选择,一次只能有一种访问路径,也就是一次只能有一个执行计划.

简单对比查看执行计划的两种方法EXPLAIN PLAN 和 AUTOTRACE

EXPLAIN PLAN 和 AUTOTRACE 都可以查看执行计划. 值得一提的是:前者只是优化器通过读取数据字典的统计信息做出'最佳'访问路径判断,并没有真正去执行语句:后者是实际去执行了SQL语句,同时把访问记录数.执行计划.统计信息等打印出来. 下面粘出实验结果加以说明,注意对比两者的耗时: <p>SQL> CONNECT /AS SYSDBA Connected.</p><p>SQL> SET LINESIZE 300; SQL> SET T

Oracle数据库查看执行计划

基于ORACLE的应用系统很多性能问题,是由应用系统SQL性能低劣引起的,所以,SQL的性能优化很重要,分析与优化SQL的性能我们一般通过查看该SQL的执行计划,本文就如何看懂执行计划,以及如何通过分析执行计划对SQL进行优化做相应说明. 一.什么是执行计划(explain plan) 执行计划:一条查询语句在ORACLE中的执行过程或访问路径的描述. 二.如何查看执行计划 1: 在PL/SQL下按F5查看执行计划.第三方工具toad等. 很多人以为PL/SQL的执行计划只能看到基数.优化器.耗

oracle如何查看执行计划

1.在PL/SQL Developer中得到一个SQL的执行计划 输入想要查看执行计划的目标SQL,再按一下快捷键F5就可以了.2.explain plan 命令 explain plan for + 目标SQL select * from table(dbms_xplan.display)3. DBMS_XPLAN 包 1) select * from table(dbms_xplan.display_cursor(null,null,'advanced')) 它用于在SQLPLUS中查看刚刚

T-SQL备忘(5):查看执行计划

先理解几个概念:表扫描.聚集索引扫描.聚集索引查找.索引扫描.书签查找. [查看执行计划] 在理解概念之前先得知道如何查看执行计划—Ctrl+L.如下图: 注:SQL Server的执行计划是从右向左看的. [理解几个概念] 1.表扫描(Table scan):发生于堆表,并且没有可用的索引可用时,会发生表扫描,表示整个表扫描一次. select * from T_UserInfo 查看执行计划如下: 2.聚集索引扫描(Clustered Index Scan):发生于聚集表,也相当于全表扫描操

创建跟踪和查看执行计划需要的权限

多看书,多积累,温故知新.只有要用的时候,才会发现知识欠缺.不要说知道有这么回事,要很有底气的回答应该怎么做.查看跟踪和查看执行计划需要怎样的权限?sysadmin/db_owner肯定可以,但不应该给这么大的权限.创建登录名 --create login use master go create login TracePlan with password='123qwe' go 创建用户 --create user use AdventureWorks2008R2 go create user

如何查看执行计划

SSMS里的设置方法就不多说了上图看 如何以SQL命令的方式查看执行计划: 一.查看实际的执行计划 方式一,查看文本格式 SET STATISTICS PROFILE ON; SET STATISTICS PROFILE OFF; 方式二.查看XML格式(可点击转到图形界面查看,因为类似全屏,界面比较大,对于比较大的执行计划会有用些) SET STATISTICS XML ON; SET STATISTICS XML OFF; 二.查看估计的执行计划 方式一,查看全部 SET SHOWPLAN_

hive高阶1--sql和hive语句执行顺序、explain查看执行计划、group by生成MR

hive语句执行顺序 msyql语句执行顺序 代码写的顺序: select ... from... where.... group by... having... order by.. 或者 from ... select ... 代码的执行顺序: from... where...group by... having.... select ... order by... hive 语句执行顺序 大致顺序 from... where.... select...group by... having