--========================
-- 启用用户进程跟踪
--========================
一、用户进程跟踪文件
用户跟踪文件在根据需要跟踪会话实际操作的时候根据要求产生
通常用于帮助调整应用程序,比如检查由SQL的不良写法所致的相关问题等等
由用户进程发出,服务器进程产生该类文件
包含跟踪SQL命令的统计信息、包含用户的错误信息
缺省情况下当用户出现会话错误时产生
位置由user_dump_dest设定
文件大小由max_dump_file_size 决定
可以设定记录会话的所有信息
分为基于会话级别和基于实例级别,大多数情况下,在session级别进行跟踪
在专用服务器模式中
仅仅需要标识该会话并为该会话启用跟踪(专用模式为一对一模式,即一个用户进程对应一个服务器进程)
在共享模式中
对任何一个会话的跟踪会分布到每个共享服务器进程所生成的跟踪文件内
二、不同级别的跟踪
instance level:
alter system set sql_trace = true;
session level:
使用alter session命令启用跟踪
alter session set sql_trace = true;
使用dbms包来启用跟踪
dbms_system.SET_SQL_TRACE_IN_SESSION
上述两类级别跟踪文件位置由user_dump_dest设定,且生成的跟踪文件名为<SID>_ora_<SPID>.trc
其中SID为实例名称,SPID为系统进程号(Unix)或线程号(Windows)
1.演示基于实例级别的跟踪
SQL> show parameter user_dump --查看用户跟踪文件所在的位置
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
user_dump_dest string /u01/app/oracle/admin/orcl/udu
mp
SQL> ho rm -f /u01/app/oracle/admin/orcl/udump/* --清除跟踪文件夹下先前产生的跟踪文件*/
SQL> alter system set sql_trace = true; --设置sql_trace参数启用实例级别的跟踪
System altered.
--开启一个会话连接
SQL> conn scott/tiger@orcl
Connected.
SQL> select s.username,s.sid,p.spid from v$session s,v$process p
2 where s.paddr = p.addr and s.username = 'SCOTT'; --获取需要跟踪的特定SPID
USERNAME SID SPID
------------------------------ ---------- ------------
SCOTT 134 7826
[oracle@robinson ~]$ ls $ORACLE_BASE/admin/orcl/udump
orcl_ora_4457.trc orcl_ora_4462.trc orcl_ora_6283.trc orcl_ora_7826.trc
orcl_ora_4459.trc orcl_ora_5027.trc orcl_ora_6524.trc orcl_ora_7833.trc
--使用tail -f 逐步跟踪会话文件
[oracle@robinson ~]$ tail -f $ORACLE_BASE/admin/orcl/udump/orcl_ora_7826.trc
=====================
PARSING IN CURSOR #3 len=40 dep=0 uid=54 oct=3 lid=54 tim=1253678938645444 hv=3933222116 ad='2dbb8df8'
SELECT DECODE('A','A','1','2') FROM DUAL
END OF STMT
PARSE #3:c=1000,e=1076,p=,cr=,cu=,mis=1,r=,dep=,og=1,tim=1253678938645434
EXEC #3:c=,e=41,p=,cr=,cu=,mis=,r=,dep=,og=1,tim=1253678938645588
FETCH #3:c=,e=20,p=,cr=,cu=,mis=,r=1,dep=,og=1,tim=1253678938645673
STAT #3 id=1 cnt=1 pid=0 pos=1 obj=0 op='FAST DUAL (cr=0 pr=0 pw=0 time=8 us)'
XCTEND rlbk=, rd_only=1
XCTEND rlbk=, rd_only=1
*** 2010-09-06 18:04:56.482
=====================
PARSING IN CURSOR #4 len=44 dep=0 uid=54 oct=3 lid=54 tim=1253679195783434 hv=24946478 ad='2d43cb14'
select * from scott.emp where ename ='SCOTT'
END OF STMT
PARSE #4:c=1999,e=2262,p=,cr=,cu=,mis=1,r=,dep=,og=1,tim=1253679195783423
EXEC #4:c=,e=52,p=,cr=,cu=,mis=,r=,dep=,og=1,tim=1253679195783643
FETCH #4:c=,e=208,p=,cr=7,cu=,mis=,r=1,dep=,og=1,tim=1253679195783930
FETCH #4:c=,e=73,p=,cr=1,cu=,mis=,r=,dep=,og=1,tim=1253679195826698
STAT #4 id=1 cnt=1 pid=0 pos=1 obj=51148 op='TABLE ACCESS FULL EMP (cr=8 pr=0 pw=0 time=194 us)'
--在scott登陆的会话中执行下面的语句,则跟踪文件orcl_ora_7826.trc同步显示所执行的数据操纵语句
SQL> select * from scott.emp where ename ='SCOTT';
EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- -------------
SAL COMM DEPTNO
---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-4月-87
3100 20
--由上可知,整个语句的执行详细处理步骤,最后提示使用了全表扫描
关于实例级别的跟踪,专用服务器模式每个服务器进程都会生成自己的跟踪信息流
共享模式则每个共享服务器都存在一个跟踪文件,且跟踪文件包含的跟踪信息与该共享服务器为其服务的所有会话
而执行的所有SQL语句相关
--关闭实例级别的跟踪
SQL> alter system set sql_trace = false;
System altered.
2.演示基于会话级别的跟踪
一个会话自身能够交互地启用针对该会话的跟踪,其它会话也可以编程启用针对该会话进行跟踪
--清理上次跟踪的会话文件,且同样可以使用上面的方式来进行跟踪,只不过修改跟踪的参数不同而已
SQL> ho rm -f /u01/app/oracle/admin/orcl/udump/* --*/
SQL> conn system/redhat
Connected.
SQL> alter session set sql_trace=true;
Session altered.
--执行SQL 语句则开始跟踪,跟踪完毕后执行下面的语句关闭跟踪
--此处跟踪的方法同实例级别跟踪
SQL> alter session set sql_trace = false;
Session altered.
--开启另一会话使用dbms包进行跟踪
SQL> conn system/redhat
Connected.
SQL> select sid,serial# from v$session where username = 'SYSTEM';
SID SERIAL#
---------- ----------
133 1141
SQL> exec dbms_monitor.session_trace_enable(session_id=>133,serial_num=>1141);
PL/SQL procedure successfully completed.
SQL> update scott.emp set sal = sal + 500 where ename = 'SCOTT';
1 row updated.
SQL> exec dbms_monitor.session_trace_disable(session_id=>133,serial_num=>1141);
PL/SQL procedure successfully completed.
SQL> select s.username,s.sid,p.spid from v$session s,v$process p
2 where s.paddr = p.addr and s.sid = 133;
USERNAME SID SPID
------------------------------ ---------- ------------
SYSTEM 133 10170
SQL> ho cat $ORACLE_BASE/admin/orcl/udump/orcl_ora_10170.trc
/u01/app/oracle/admin/orcl/udump/orcl_ora_10170.trc
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
ORACLE_HOME = /u01/app/oracle/10g
System name: Linux
Node name: robinson
Release: 2.6.18-164.el5
Version: #1 SMP Tue Aug 18 15:51:54 EDT 2009
Machine: i686
Instance name: orcl
Redo thread mounted by this instance: 1
Oracle process number: 18
Unix process pid: 10170, image: oracle@robinson (TNS V1-V3)
*** 2010-09-06 19:49:33.906
*** ACTION NAME:() 2010-09-06 19:49:33.903
*** MODULE NAME:(SQL*Plus) 2010-09-06 19:49:33.903
*** SERVICE NAME:(SYS$USERS) 2010-09-06 19:49:33.903
*** SESSION ID:(133.1141) 2010-09-06 19:49:33.903
=====================
PARSING IN CURSOR #2 len=81 dep=0 uid=5 oct=47 lid=5 tim=1253685326077891 hv=1816613408 ad='2dbf7d40'
BEGIN dbms_monitor.session_trace_enable(session_id=>133,serial_num=>1141); END;
END OF STMT
EXEC #2:c=,e=466,p=,cr=,cu=,mis=1,r=1,dep=,og=1,tim=1253685326077881
WAIT #2: nam='SQL*Net message to client' ela= 4 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1253685326080388
WAIT #2: nam='SQL*Net message from client' ela= 1823 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1253685326082309
WAIT #0: nam='SQL*Net message to client' ela= 2 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1253685326082495
*** 2010-09-06 19:50:20.709
WAIT #0: nam='SQL*Net message from client' ela= 45704475 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1253685371787005
=====================
PARSING IN CURSOR #1 len=58 dep=0 uid=5 oct=6 lid=5 tim=1253685371793390 hv=3123987885 ad='2dbf7aa4'
update scott.emp set sal = sal + 500 where ename = 'SCOTT'
END OF STMT
PARSE #1:c=4000,e=6173,p=,cr=,cu=,mis=1,r=,dep=,og=1,tim=1253685371793377
EXEC #1:c=999,e=1381,p=,cr=7,cu=3,mis=,r=1,dep=,og=1,tim=1253685371794945
WAIT #1: nam='SQL*Net message to client' ela= 3 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1253685371795058
WAIT #1: nam='SQL*Net message from client' ela= 4984 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1253685371800090
STAT #1 id=1 cnt=0 pid=0 pos=1 obj=0 op='UPDATE EMP (cr=7 pr=0 pw=0 time=993 us)'
STAT #1 id=2 cnt=1 pid=1 pos=1 obj=51148 op='TABLE ACCESS FULL EMP (cr=7 pr=0 pw=0 time=272 us)'
WAIT #0: nam='SQL*Net message to client' ela= 2 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1253685371800388
*** 2010-09-06 19:50:53.414
WAIT #0: nam='SQL*Net message from client' ela= 31924456 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1253685403724882
=====================
PARSING IN CURSOR #2 len=82 dep=0 uid=5 oct=47 lid=5 tim=1253685403729071 hv=3536340123 ad='2dbf7744'
BEGIN dbms_monitor.session_trace_disable(session_id=>133,serial_num=>1141); END;
END OF STMT
PARSE #2:c=1000,e=4016,p=,cr=,cu=,mis=1,r=,dep=,og=1,tim=1253685403729061
EXEC #2:c=,e=150,p=,cr=,cu=,mis=,r=1,dep=,og=1,tim=1253685
三、启用会话跟踪查看show parameter sga 的源码
SQL> conn system/redhat --使用system帐户连接
Connected.
SQL> alter session set sql_trace = true; --启用会话跟踪
Session altered.
SQL> show parameter sga --查看sga
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
lock_sga boolean FALSE
pre_page_sga boolean FALSE
sga_max_size big integer 448M
sga_target big integer 448M
SQL> alter session set sql_trace = false; --关闭会话跟踪
Session altered.
SQL> select s.username,s.sid,p.spid from v$session s,v$process p
2 where s.paddr = p.addr and s.username = 'SCOTT'; --获取需要跟踪的特定SPID
USERNAME SID SPID
------------------------------ ---------- ------------
SYSTEM 159 3365
SQL>ho cat $ORACLE_BASE/admin/orcl/udump/orcl_ora_3365.trc --查看跟踪文件
--以下内容为执行show parameter sga的实际操作语句,实质上是查询v$parameter视图
SELECT NAME NAME_COL_PLUS_SHOW_PARAM,
DECODE(TYPE,1,'boolean',2,'string',3,'integer',4,'file',5,'number', 6,'big integer', 'unknown') TYPE,
DISPLAY_VALUE VALUE_COL_PLUS_SHOW_PARAM
FROM V$PARAMETER WHERE UPPER(NAME) LIKE UPPER('%sga%')
ORDER BY NAME_COL_PLUS_SHOW_PARAM,ROWNUM
四、启用实例级别会话跟踪产生的问题
使用下面的语句启用实例级别会话跟踪后
alter system set sql_trace = true;
再将其关闭后,重新启动实例出现下列提示
SQL> startup --启动实例
ORA-32004: obsolete and/or deprecated parameter(s) specified
ORACLE instance started.
Total System Global Area 469762048 bytes
Fixed Size 1220048 bytes
Variable Size 180355632 bytes
Database Buffers 285212672 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
[oracle@robinson ~]$ oerr ora 32004 --查看具体的错误信息描述有参数设置不妥
32004, 00000, "obsolete and/or deprecated parameter(s) specified"
// *Cause: One or more obsolete and/or parameters were specified in
// the SPFILE or the PFILE on the server side.
// *Action: See alert log for a list of parameters that are obsolete.
// or deprecated. Remove them from the SPFILE or the server
// side PFILE.
--查看告警日志
SQL> ho cat $ORACLE_BASE/admin/orcl/bdump/alert_orcl.log
Deprecated system parameters with specified values:
sql_trace --描述为sql_trace
End of deprecated system parameter listing
SQL> show parameter sql_trace; --查看该参数已经置为false
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
sql_trace boolean FALSE
--使用reset将其从参数文件中清除后启动正常
SQL> alter system reset sql_trace scope = spfile sid = '*';
System altered.
SQL> startup force;
ORACLE instance started.
Total System Global Area 469762048 bytes
Fixed Size 1220048 bytes
Variable Size 184549936 bytes
Database Buffers 281018368 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
五、更多
Oracle 冷备份
SPFILE错误导致数据库无法启动
Oracle 用户、对象权限、系统权限
Oracle 角色、配置文件
Oracle 联机重做日志文件(ONLINE LOG FILE)
Oracle 控制文件(CONTROLFILE)
Oracle 表空间与数据文件
Oracle 归档日志