Oracle Trace (SQL_TRACE)

SQL_TRACE means is provided for Oracle SQL Trace, is a powerful tool for diagnosis in routine database problem diagnosis and resolution of, SQL_TRACE is a very commonly used method.
This article briefly discusses the use of SQL_TRACE, and will be described by using sql_trace specific cases.


A basic introduction

(A) SQL_TRACE Description

SQL_TRACE can be enabled as a global initialization parameter, you can enable specific session through the command line.
1. Enabling the global
specified in the parameter file (pfile / spfile) in:

sql_trace =true

Enabling the global SQL_TRACE will lead the activities of all processes being followed, including background process and all user processes, which often lead to serious performance problems, so use it with caution in a production environment, this parameter after the 10g dynamic parameters, you can any time to adjust, very effective in certain diagnosis.
Tip: By enabling sql_trace globally, we can track all active background processes, many of abstraction in the documentation, and real-time change tracking file, we can clearly see the close coordination between the various processes.

2. In the current session-level setting
most of the time we use sql_trace track of the current process can be found backstage database activities recursively current operation (This is particularly effective in the study database of new features) by tracking the current process,
research SQL execution errors found in the background.
In session level, enabling and disabling sql_trace as follows:

Current session tracing enabled: 
SQL> ALTER SQL_TRACE = SET session to true; 

the Session Altered. 

At this time, the operation will be tracked SQL: 
SQL> SELECT COUNT (*) from DBA_USERS; 

  COUNT (*) 
--------- - 
        34 is 
the end of the track: 
the SQL> SET SQL_TRACE = ALTER the session to false; 

the Session Altered.
       


3. Track other user process
in many cases we need to track other users of the process, rather than the current user, this system can be provided by the Oracle package DBMS_SYSTEM. SET_SQL_TRACE_IN_SESSION
to complete

SET_SQL_TRACE_IN_SESSION through the program to provide three parameters:

SQL> desc dbms_system
...
PROCEDURE SET_SQL_TRACE_IN_SESSION
 Argument Name                     Type                    In/Out Default?
 ------------------------------           -----------------------   ------ --------
 SID                               NUMBER                  IN
 SERIAL#                          NUMBER                  IN
 SQL_TRACE                        BOOLEAN                 IN
...

 

By v $ session we can get sid, serial # and other information:

The process of obtaining process information, select the desired track: 

SQL> the SELECT sid, Serial #, username from v $ the session 
  2 the WHERE username IS not null; 

       SID SERIAL # USERNAME 
---------- ------ ------------------------------ ---- 
         8 2041 SYS 
         9 437 eygle 

set up tracking: 
SQL> Exec DBMS_SYSTEM.set_sql_trace_in_session ( 9,437, to true) 

. PL / SQL Procedure Completed successfully 

.... 
can wait a moment, session tracking tasks, capture sql operation ... 
.... 

stop tracking: 
SQL> Exec DBMS_SYSTEM.set_sql_trace_in_session (9,437, false) 

PL / SQL procedure successfully completed.
  

(b) 10046 Event Description
10046 events are internal to Oracle offer is an enhancement to the SQL_TRACE.
10046 event can set the following four levels:
1 - Enable standard SQL_TRACE function, equivalent to SQL_TRACE
4 - Level 1 plus binding value (the bind values)
. 8 - level event tracking wait. 1 +
12 - level 1 + level 4 + level 8
similar sql_trace, 10046 may be provided on a global event, may be provided at the session level.
1. In the Global Settings
increase in the parameter file: 

event="10046 trace name context forever,level 12" 

This setting is for all processes take effect for all users, including background processes.

2. Set the current session
by session alter the way changes need to alter the system of rights session: 

SQL> alter session set events '10046 trace name context forever';

Session altered.

SQL> alter session set events '10046 trace name context forever, level 8';

Session altered.

SQL> alter session set events '10046 trace name context off';

Session altered.

      

3. 对其他用户session设置
通过DBMS_SYSTEM.SET_EV系统包来实现:


SQL> desc dbms_system
...
PROCEDURE SET_EV
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 SI                             BINARY_INTEGER          IN
 SE                             BINARY_INTEGER          IN
 EV                             BINARY_INTEGER          IN
 LE                             BINARY_INTEGER          IN
 NM                             VARCHAR2                IN

...

其中的参数SI、SE来自v$session视图:
查询获得需要跟踪的session信息:
SQL> select sid,serial#,username from v$session where username is not null;

SID SERIAL# USERNAME
---------- ---------- ------------------------------
8 2041 SYS
9 437 EYGLE

执行跟踪:
SQL> exec dbms_system.set_ev(9,437,10046,8,'eygle');

PL/SQL procedure successfully completed.

结束跟踪:
SQL> exec dbms_system.set_ev(9,437,10046,0,'eygle');

PL/SQL procedure successfully completed.

(c) 获取跟踪文件
以上生成的跟踪文件位于user_dump_dest目录中,位置及文件名可以通过以下SQL查询获得:

SQL> select

  2    d.value||'/'||lower(rtrim(i.instance, chr(0)))||'_ora_'||p.spid||'.trc' trace_file_name
  3  from
  4    ( select p.spid
  5      from sys.v$mystat m,sys.v$session s,sys.v$process p
  6      where m.statistic# = 1 and s.sid = m.sid and p.addr = s.paddr) p,
  7    ( select t.instance from sys.v$thread  t,sys.v$parameter  v
  8      where v.name = 'thread' and (v.value = 0 or t.thread# = to_number(v.value))) i,
  9    ( select value from sys.v$parameter where name = 'user_dump_dest') d
 10  / 

TRACE_FILE_NAME
--------------------------------------------------------------------------------
/opt/oracle/admin/hsjf/udump/hsjf_ora_1026.trc 


(d) 读取当前session设置的参数
当我们通过alter session的方式设置了sql_trace,这个设置是不能通过show parameter的方式得到的,我们需要通过dbms_system.read_ev来获取:

SQL> set feedback off

SQL> set serveroutput on 

SQL> declare 
2 event_level number; 
3 begin 
4 for event_number in 10000..10999 loop 
5 sys.dbms_system.read_ev(event_number, event_level); 
6 if (event_level > 0) then 
7 sys.dbms_output.put_line(
8 'Event ' ||
9 to_char(event_number) ||
10 ' is set at level ' || 
11 to_char(event_level)
12 ); 
13 end if; 
14 end loop; 
15 end; 
16 /
Event 10046 is set at level 1

发布了35 篇原创文章 · 获赞 61 · 访问量 16万+

Guess you like

Origin blog.csdn.net/woailyoo0000/article/details/79962241