现在的位置: 首页 > 综合 > 正文

Oracle SQL Trace

2013年05月17日 ⁄ 综合 ⁄ 共 3360字 ⁄ 字号 评论关闭
文章目录

SQL Trace收集方法

Current Session Level

1.Enable Sql Trace
alter session set sql_trace = true;
alter session set statistics_level = ALL;
alter session set timed_statistics = true;
alter session set max_dump_file_size = unlimited;
alter session set tracefile_identifier = ptian123;

蓝色部分,非必须执行的脚本;tracefile_identifier为trace标识符,用于快速找到sqltrace文件;

2.set TRACING LEVELS
--Setting level 12 trace to get bind values and wait states information
ALTER SESSION SET EVENTS '10046 trace name context forever, level 12';

3.Execute your sqls

4.Disable Sql Trace
alter session set events '10046 trace name context off';

ALTER SESSION SET sql_trace = false;

Other Session Level

1.Get other session SID & SERIAL#
select sid,serial#,username from v$session where username = 'xxx';
SID        SERIAL#
-----        ----------
11           13442

2.Enable Sql Trace
alter system set timed_statistics = true;
execute dbms_system.set_sql_trace_in_session (<SID>, <SERIAL#>, true);

3.

Wait for a while,capture the sql

4.Stop the trace
execute dbms_system.set_sql_trace_in_session(<SID>, <SERIAL#>, false);

Whole Database Level

方法一:
1.Start SQL Trace on the complete database:
alter system set sql_trace = true scope=memory;

2.Stop SQL Trace on the complete database:
alter system set sql_trace = false scope=memory;

方法二:
参数文件(pfile/spfile)中指定:  SQL_TRACE = true

如何找到SQL Trace文件

查看user_dump_dest目录, 执行linux命令

ls *ptian01*
就会找到类似"SID_ora_nnnn_PTIAN01.trc"名字的trace文件。

或者执行下边这个Query,可以获取当前session Sql Trace的文件目录+名称

SELECT
  u_dump.value   || '/'     ||
  db_name.value  || '_ora_' ||
  v$process.spid ||
  nvl2(v$process.traceid,  '_' || v$process.traceid, null )
  || '.trc'  "Trace File"
from
             v$parameter u_dump
  cross join v$parameter db_name
  cross join v$process
        join v$session
          on v$process.addr = v$session.paddr
where
 u_dump.name   = 'user_dump_dest' and
 db_name.name  = 'db_name'        and
 v$session.audsid=sys_context('userenv','sessionid');

output like:

/u01/oracle/mc3yd213/db/tech_st/11.1.0/admin/mc3yd213_bej301441/diag/rdbms/mc3yd213/mc3yd213/trace/mc3yd213_ora_9593.trc

Parameters

TIMED_STATISTICS
This enables and disables the collection of timed statistics, such as CPU and elapsed times, by the SQL Trace facility, as well as the collection of various statistics in the dynamic performance tables. The default value of false disables timing. A value of
true enables timing. Enabling timing causes extra timing calls for low-level operations. This is a dynamic parameter. It is also a session parameter.

MAX_DUMP_FILE_SIZE
When the SQL Trace facility is enabled at the instance level, every call to the server produces a text line in a file in the operating system's file format. The maximum size of these files (in operating system blocks) is limited by this initialization parameter.
The default is 500. If you find that the trace output is truncated, then increase the value of this parameter before generating another trace file. This is a dynamic parameter. It is also a session parameter.

STATISTICS_LEVEL
controls all major statistics collections or advisories in the database.
BASIC: No advisories or statistics are collected.
TYPICAL: The following advisories or statistics are collected:
    Buffer cache advisory
    MTTR advisory
    Shared Pool sizing advisory
    Segment level statistics
    PGA target advisory
    Timed statistics
ALL: All of the preceding advisories or statistics are collected, plus the following:
    Timed operating system statistics
    Row source execution statistics

10046 Event Levels
10046  0  No statistics generated
10046  1  Standard trace output including parsing, executes and fetches plus more
10046  2  Same as Level 1
10046  4  Level 1 + Bind Variables
10046  8  Level 1 + Waits
10046  12  Level 1 + Bind Variables & Waits

Good Reference:http://www.jlcomp.demon.co.uk/faq/Tracing_Oracle_session.pdf

转载请注明出处:http://blog.csdn.net/pan_tian/article/details/8028297

抱歉!评论已关闭.