Generating SQL Trace Files
Generating SQL Trace Files
The following Tip is from the outstanding book "Oracle PL/SQL Tuning: Expert Secrets for High Performance
Programming" by Dr. Tim Hall, Oracle ACE of the year, 2006:
There are numerous ways to enable, disable and vary the contents of this trace. The following
methods have been available for several versions of the database.
-- All versions.
SQL> ALTER SESSION SET sql_trace=TRUE;
SQL> ALTER SESSION SET sql_trace=FALSE;
SQL> ALTER SESSION SET EVENTS '10046 trace name context forever, level 8';
SQL> ALTER SESSION SET EVENTS '10046 trace name context off';
The dbms_support package is not present by default, but can be loaded as the SYS user by
executing the @$ORACLE_HOME/rdbms/admin/dbmssupp.sql script.
For methods that require tracing levels, the following are valid values:
· 12 - The same as 2, but with both bind variable values and wait events.
The same combinations are possible for those methods with boolean parameters for waits and
binds.
With the advent of Oracle 10g, the SQL tracing options have been centralized and extended
using the dbms_monitor package. The examples below show a few possible variations for
enabling and disabling SQL trace in Oracle 10g.
-- Oracle 10g
SQL> EXEC DBMS_MONITOR.session_trace_enable;
SQL> EXEC DBMS_MONITOR.session_trace_enable(waits=>TRUE, binds=>FALSE);
SQL> EXEC DBMS_MONITOR.session_trace_disable;
The package provides the conventional session level tracing along with two new variations. First,
tracing can be enabled on multiple sessions based on the value of the client_identifier column of
the v$session view, set using the dbms_session package.
Second, tracing can be activated for multiple sessions based on various combinations of the
service_name, module, action columns in the v$session view, set using the
dbms_application_info package, along with the instance_name in RAC environments. With all
the possible permutations and default values, this provides a high degree of flexibility.
trcsess
Activating trace on multiple sessions means that trace information is spread throughout many
trace files. For this reason Oracle 10g introduced the trcsess utility, allowing trace information
from multiple trace files to be identified and consolidated into a single trace file. The trcsess
usage is listed below.
trcsess [output=<output file name >] [session=<session ID>] [clientid=<clientid>]
[service=<service name>] [action=<action name>] [module=<module name>] <trace file
names>
output=<output file name> output destination default being standard output.
session=<session Id> session to be traced.
Session id is a combination of session Index & session serial number e.g. 8.13.
clientid=<clientid> clientid to be traced.
service=<service name> service to be traced.
action=<action name> action to be traced.
module=<module name> module to be traced.
<trace_file_names> Space separated list of trace files with wild card '*' supported.
With all these options, the consolidated trace file can be as broad or as specific as needed.
tkprof
The SQL trace files produced by the methods discussed previously can be read in their raw form,
or they can be translated by the tkprof utility into a more human readable form. The output below
lists the usage notes from the tkprof utility in Oracle 10g.
$ tkprof
Usage: tkprof tracefile outputfile [explain= ] [table= ]
[print= ] [insert= ] [sys= ] [sort= ]
table=schema.tablename Use 'schema.tablename' with 'explain=' option.
explain=user/password Connect to ORACLE and issue EXPLAIN PLAN.
print=integer List only the first 'integer' SQL statements.
aggregate=yes|no
insert=filename List SQL statements and data inside INSERT statements.
sys=no TKPROF does not list SQL statements run as user SYS.
record=filename Record non-recursive statements found in the trace file.
waits=yes|no Record summary for any wait events found in the trace file.
sort=option Set of zero or more of the following sort options:
The waits parameter was only added in Oracle 9i, so prior to this version wait information had to
be read from the raw trace file. The values of bind variables must be read from the raw files as
they are not displayed in the tkprof output.
The following section shows an example of gathering SQL trace for a session as an example of
the whole process.
================================================================================
==========
tkprof
tkprof is one of the most useful utilities available to DBAs for diagnosing performance issues. It
essentially formats a trace file into a more readable format for performance analysis. The DBA
can then identify and resolve performance issues such as poor SQL, indexing, and wait events.
tkprof has been historically difficult to use for many reasons. First, the entire process of enabling
tracing, finding trace files, and executing the utility against them is a burdensome task. Once the
DBA finally has the trace file output the typical response is “Now what do I do”? Second, even
though tkprof formats the data, it lacks any additional insight needed to remedy the problems
revealed. In fact, problems are not even highlighted, thereby putting more work on the DBA to
analyze the output, assess the problems, and determine what to do.
Why, When tkprof?
The DBA will use tkprof and session tracing when the database or a particular session is having
performance problems. tkprof will generally be used infrequently, when researching a very
particular performance issue. A user may complain that the response time for a session is
abysmal compared to the prior week. Session tracing and tkprof can be used to see exactly what
is happening on the database, enabling the DBA to take corrective action.
The utility can also be used to view SQL that is being executed for an application. In some
situations, this will be the only mechanism a DBA will have to view SQL. These situations
include the execution of encrypted PL/SQL code on the database or submission of SQL
statements from third party applications.
Analyzing tkprof Results
So what should DBAs be looking for? Here’s a small checklist of items to watch for in tkprof
formatted files:
· Compare the number of parses to number of executions. A well-tuned system will have one
parse per n executions of a statement and will eliminate the re-parsing of the same statement.
· Search for SQL statements that do not use bind variables (:variable). These statements
should be modified to use bind variables.
· Identify those statements that perform full table scans, multiple disk reads, and high CPU
consumption. These performance benchmarks are defined by the DBA and need to be tailored to
each database. What may be considered a high number of disk reads for an OLTP application
may not even be minimal for a data warehouse implementation.
The tkprof process will be explained in six easy steps.
Step 1: Check the Environment
Before tracing can be enabled, the environment must first be configured by performing the
following steps:
· Enable Timed Statistics – This parameter enables the collection of certain vital statistics such
as CPU execution time, wait events, and elapsed times. The resulting trace output is more
meaningful with these statistics. The command to enable timed statistics is:
ALTER SYSTEM SET TIMED_STATISTICS = TRUE;
· Check the User Dump Destination Directory – The trace files generated by Oracle can be
numerous and large. These files are placed by Oracle in user_dump_dest directory as specified in
the init.ora. The user dump destination can also be specified for a single session using the alter
session command. Make sure that enough space exists on the device to support the number of
trace files that you expect to generate.
SQL> select value
from v$parameter
where name = 'user_dump_dest';
VALUE
---------------------------------
C:\oracle9i\admin\ORCL92\udump
Once the directory name is obtained, the corresponding space command (OS dependent) will
report the amount of available space. Delete unwanted trace files before starting a new trace to
free up the disk space.
Step 2: Turn Tracing On
The next step in the process is to enable tracing. By default, tracing is disabled due to the burden
(5-10%) it places on the database. Tracing can be defined at the session level:
ALTER SESSION SET SQL_TRACE = TRUE;
DBMS_SESSION.SET_SQL_TRACE(TRUE);
A DBA may enable tracing for another user’s session by:
DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(sid,serial#,true);
where the sid (Session ID) and serial# can be obtained from the v$session view. This package is
owned by the SYS user and therefore the executor must be SYS or be granted EXECUTE the
privilege by SYS user.
Once tracing is enabled, Oracle generates and stores the statistics in the trace file. The trace file
name is version specific. Table 5.1 below contains the version naming conventions for
foreground processes.