How To Trace A Oracle Session
How To Trace A Oracle Session
SQL_TRACE_IN_SESSION procedure enables/disables SQL tracing for any user session identified by Session ID (SID) and Session Serial Number. This procedure is the most effective way of analyzing SQL related performance issues originating from SQL embedded in applications. The package SQL_TRACE_IN_SESSION produces a trace file in user_dump_dest (same format and content as 'ALTER SESSION SET SQL_TRACE = TRUE') that can be formatted with TKPROF. This package can be enabled at both the SQL*Plus command line and directly from stored procedures. SCOPE & APPLICATION ------------------Useful for database administrators and developers HOW TO CREATE A SQL TRACE FILE FROM ANOTHER SESSION --------------------------------------------------In order to generate a SQL trace from another session do the following: 1. Alter the system to set timed_statistics=true: $ sqlplus system/manager SQL> alter system set timed_statistics=true; System altered. 2. Execute a query to get the session ids in the system, this list will be compared later with a session list obtained after connecting to the application from which we want to generate the trace. Comparing both lists you get the new session id you want to trace. SQL> select sid, serial#, username 2 from v$session; SID SERIAL# USERNAME ---------- ---------- ------------------1 1 2 1 3 1 4 1 5 1 6 1 7 1078 SYSTEM In this example we have 7 sessions. 3. Execute the application and connect a user. 4. Execute the query to list the sessions and identify the new created session. SQL> select sid, serial#, username 2 from v$session;
SID SERIAL# USERNAME ---------- ---------- --------------1 1 2 1 3 1 4 1 5 1 6 1 7 1078 SYSTEM 8 121 SCOTT In this example we can see easily that the new created session is from user SCOTT which have the following data: SID=8 y SERIAL#=121. 5. In order to get a trace from the new created session we use the set_sql_trace_in_session procedure defined in DBMS_SYSTEM package. NB: Unlike other packages created by catproc.sql, a public synonym is not created for DBMS_SYSTEM and no privileges on the package are granted. Thus, as initially created, only SYS can reference and use any component in the DBMS_SYSTEM package. SYS can grant access to DBMS_SYSTEM components to any user/role: Example: GRANT EXECUTE ON DBMS_SYSTEM to <user/role>; SQL> ALTER SESSION SET TRACEFILE_IDENTIFIER = "MY_TEST_SESSION"; SQL> exec sys.dbms_system.set_sql_trace_in_session(8,121,true); PL/SQL procedure successfully completed. 6. Execute the process in the application to generate the error/sql you want to trace. 7. Turn off SQL Trace for the session in order to limit the tracing: SQL> exec sys.dbms_system.set_sql_trace_in_session(8,121,false); 8. Look for the new trace file generated in user_dump_dest. In order to know the directory execute the following query: SQL> select value 2 from v$parameter 3 where name='user_dump_dest'; VALUE -------------------------------/u02/app/oracle/admin/V804/udump 8. In order to identify the generated trace file, you can grep the directory with the sid and serial# (8 and 121 in our example) as it is shown in the next e xample: $ grep 8.121 * ora_26384.trc:***SESSIONID:(8.121) 2000.02.25.16.38.55.538 This way you see the trace file generated is ora_26384.trc
9. To close the generation of the trace file execute: SQL> exec sys.dbms_system.set_sql_trace_in_session(8,121,false); 10. If you wish you can generate the execution plan using tkprof: $ tkprof ora_26384.trc out.txt explain=<user/pwd> the out.txt file has the execution plan for the instructions executed in the process which has been run in the application. NB: See the Note:29012.1 and Note:41634.1 for more details on tkprof usage. References: =========== Note:41634.1 TKPROF and Problem Solving Note:29012.1 QREF: TKPROF Usage - Quick Reference