在Oracle中跟踪某几个用户的SQL
在Oracle中做SQL跟踪,估计大部分都会用sqlplus中的autotrace或者设置10046跟踪。但是,如果是调试某个应用系统的话,特别是打开了连接池的系统,靠以上两种方法基本上就歇菜了。还是SQL Server的SQL Server Profiler好啊。
最近在看冯大辉翻译的
create role trace_10046_role; CREATE OR REPLACE TRIGGER SYS.set_10046_trace_on_logon AFTER LOGON ON DATABASE BEGIN if ( dbms_session.is_role_enabled('trace_10046_role') and user not in ('SYS','SYSTEM') ) then EXECUTE IMMEDIATE 'alter session set statistics_level=ALL'; EXECUTE IMMEDIATE 'alter session set max_dump_file_size=UNLIMITED'; EXECUTE IMMEDIATE 'alter session set tracefile_identifier='''||user||'_10046'''; EXECUTE IMMEDIATE 'alter session set events ''10046 trace name context forever, level 12'''; end if; END set_10046_trace_on_logon;
启用和禁用对系统进行做10046跟踪SQL:
SQL> show user; USER is "SYS" SQL> grant trace_10046_role to cms_text; SQL> select * from dba_ROLE_PRIVS where granted_role='TRACE_10046_ROLE'; GRANTEE GRANTED_ROLE ADM DEF ------------------------------ ------------------------------ --- --- SYS TRACE_10046_ROLE YES YES CMS_TEXT TRACE_10046_ROLE NO YES Grant succeeded. SQL> revoke trace_10046_role from cms_text; SQL> select * from dba_ROLE_PRIVS where granted_role='TRACE_10046_ROLE'; GRANTEE GRANTED_ROLE ADM DEF ------------------------------ ------------------------------ --- --- SYS TRACE_10046_ROLE YES YES
其实就是将 trace_10046_role grant/revoke 给相应的用户。
当具有 trace_10046_role 角色的用户登录后在udump下就会出现相应的trc文件:
[oracle@test-server udump]$ pwd /u01/app/admin/ora8i/udump [oracle@test-server udump]$ ls ora8i_ora_8259_CMS_TEXT_10046.trc