Tracing forms an important method when it comes to database and/or SQL performance tuning. In this blog post let's see the various methods we can trace a session or an application or an entire database and how to read the trace files generated
Types of tracing:
- Session level
- Application level
- Database level
- Check timed_statistics value is TRUE;
- Check statistics_level is typical or all
- Set max_dump_file_size to unlimited so that the trace is not abruptly ended due to file size reached
Example tracing own session:
When we need to trace the same session that we are connected to trouble shoot a query, we can perform the below
SQL_TRACE
[oracle@linux75-2 ~]$ sql soe/soe
SQLcl: Release 12.2.0.1.0 RC on Thu Dec 02 23:53:32 2021
Copyright (c) 1982, 2021, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL> show parameter timed_statistics
NAME TYPE VALUE
---------------- ------- -----
timed_statistics boolean TRUE
SQL> show parameter statistics_level
NAME TYPE VALUE
---------------- ------ -------
statistics_level string TYPICAL
SQL> show parameter max_dump_file_size
NAME TYPE VALUE
------------------ ------ ---------
max_dump_file_size string unlimited
SQL> alter session set tracefile_identifier='MyTrace';
Session altered.
SQL> alter session set sql_trace=true;
Session altered.
SQL> -- Run the query that needs to be traced now
SQL> select count(*) from soe.addresses where country='India';
COUNT(*)
----------
19536
SQL> alter session set sql_trace=false;
Session altered.
SQL>
SQL> -- set the identifier to find the trace file easily in the trace directory
SQL> alter session set tracefile_identifier='My10046';
Session altered.
SQL> alter session set events '10046 trace name context forever, level 12';
Session altered.
SQL> -- Run the query to be traced now
SQL> select count(*) from soe.addresses where country='India';
COUNT(*)
----------
19536
SQL> alter session set events '10046 trace name context off';
Session altered.
SQL> show parameter diag
NAME TYPE VALUE
--------------- ------ ---------
diagnostic_dest string /u01/orcl
SQL> exit
- 0 - Off
- 2 - Similar to regular sql_trace
- 4 - Same as 2, but with addition of bind variable values
- 8 - Same as 2, but with addition of wait events
- 12 - same as 2, but with both bind variable values and wait events
SQL> alter session set tracefile_identifier='MySess';
Session altered.
SQL> exec DBMS_SESSION.SESSION_TRACE_ENABLE(waits=> true, binds=> true);
PL/SQL procedure successfully completed.
SQL> select count(*) from soe.addresses where country='India';
COUNT(*)
----------
19536
SQL> exec DBMS_SESSION.SESSION_TRACE_DISABLE();
PL/SQL procedure successfully completed.
SQL> exit
[oracle@linux75-2 ~]$ cd /u01/orcl/diag/rdbms/orcl/orcl/trace/ [oracle@linux75-2 trace]$ ls -lrt *My* -rw-r-----. 1 oracle oinstall 1310 Dec 2 23:55 orcl_ora_8930_MyTrace.trm -rw-r-----. 1 oracle oinstall 2560 Dec 2 23:55 orcl_ora_8930_MyTrace.trc -rw-r-----. 1 oracle oinstall 2984 Dec 2 23:55 orcl_ora_8930_My10046.trm -rw-r-----. 1 oracle oinstall 26449 Dec 2 23:55 orcl_ora_8930_My10046.trc -rw-r-----. 1 oracle oinstall 3404 Dec 3 00:38 orcl_ora_12142_MySess.trm -rw-r-----. 1 oracle oinstall 33729 Dec 3 00:38 orcl_ora_12142_MySess.trc [oracle@linux75-2 trace]$
WHEN ( user LIKE '&USERNAME' ) DECLARE
lcommand VARCHAR(200);
BEGIN
EXECUTE IMMEDIATE 'alter session set statistics_level=ALL';
EXECUTE IMMEDIATE 'alter session set max_dump_file_size=UNLIMITED';
DBMS_MONITOR.SESSION_TRACE_ENABLE (WAITS=> TRUE , BINDS=> TRUE);
END SET_TRACE ;
/
SQL> -- Finding session id and serial # to enable trace SQL> select username, sid, serial# from v$session where username='SOE'; USERNAME SID SERIAL# ------------------------------ ---------- ---------- SOE 783 48974 SQL> -- Enable trace for the session SQL> EXECUTE DBMS_MONITOR.SESSION_TRACE_ENABLE(SESSION_ID=>783, SERIAL_NUM=>48974, WAITS=>TRUE, BINDS=>FALSE); PL/SQL procedure successfully completed. SQL> -- Now all the activities on session 783,48974 will be traced SQL> -- We can disable trace once we have confirmation from user he/she completed their work to investigate SQL> SQL> EXECUTE DBMS_MONITOR.SESSION_TRACE_DISABLE(SESSION_ID=>783, SERIAL_NUM=>48974); PL/SQL procedure successfully completed. SQL>If the session is disconnected before we stop tracing, tracing will automatically be stopped and upon trying to stop the tracing, we will get the below error
BEGIN DBMS_MONITOR.SESSION_TRACE_DISABLE(SESSION_ID=>7, SERIAL_NUM=>44); END; * ERROR at line 1: ORA-00030: User session ID does not exist. ORA-06512: at "SYS.DBMS_MONITOR", line 144 ORA-06512: at line 1
SQL> select username, sid, serial# from v$session where username='SOE'; USERNAME SID SERIAL# ------------------------------ ---------- ---------- SOE 1151 6659 SQL> exec sys.dbms_system.set_sql_trace_in_session(1151, 6659, TRUE); PL/SQL procedure successfully completed. SQL> exec sys.dbms_system.set_sql_trace_in_session(1151, 6659, FALSE); PL/SQL procedure successfully completed. SQL> desc sys.dbms_system ... ... PROCEDURE SET_SQL_TRACE_IN_SESSION Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- SID NUMBER IN SERIAL# NUMBER IN SQL_TRACE BOOLEAN IN ... ... SQL>
























