Oracle
Tools and Utilities TKPROF
(Transient Kernel Profiling) (Source Oracle Docs) Understanding TKPROF You can run the TKPROF program to format the contents of the trace file and place the output into a readable output file. TKPROF can also: Create
a SQL script that stores the statistics in the database --------------------------------------------------------------------- Note:
TKPROF reports each statement executed with the resources it has consumed, the number of times it was called, and the number of rows which it processed. This information lets you easily locate those statements that are using the greatest resource. With experience or with baselines available, you can assess whether the resources used are reasonable given the work done. Using the SQL Trace Facility and TKPROF Step
1: Setting Initialization Parameters for Trace File Management Check the settings of the TIMED_STATISTICS, MAX_DUMP_FILE_SIZE, and USER_DUMP_DEST initialization parameters. TIMED_STATISTICS This
enables and disables the collection of timed statistics, such as CPU and
elapsed times, by the SQL Trace facility, as well as the collection of
various statistics in the dynamic performance tables. The default value
of false disables timing. A value of true enables timing. Enabling timing
causes extra timing calls for low-level operations. This is a dynamic
parameter. It is also a session parameter. When
the SQL Trace facility is enabled at the instance level, every call to
the server produces a text line in a file in the operating system's file
format. The maximum size of these files (in operating system blocks) is
limited by this initialization parameter. The default is 500. If you find
that the trace output is truncated, then increase the value of this parameter
before generating another trace file. This is a dynamic parameter. It
is also a session parameter.
This must fully specify the destination for the trace file according to
the conventions of the operating system. The default value is the default
destination for system dumps on the operating system.This value can be
modified with ALTER SYSTEM SET USER_DUMP_DEST= newdir. This is a dynamic
parameter. It is also a session parameter. See Also: "Interpreting
Statistics" for considerations when setting the STATISTICS_LEVEL,
DB_CACHE_ADVICE, TIMED_STATISTICS, or TIMED_OS_STATISTICS initialization
parameters Devise a way of recognizing the resulting trace file. Be sure you know how to distinguish the trace files by name. Oracle writes them to the user dump destination specified by USER_DUMP_DEST. However, this directory can soon contain many hundreds of files, usually with generated names. It might be difficult to match trace files back to the session or process that created them. You can tag trace files by including in your programs a statement like SELECT 'program_name' FROM DUAL. You can then trace each file back to the process that created it. You can also set the TRACEFILE_IDENTIFIER initialization parameter to specify a custom identifier that becomes part of the trace file name. For example, you can add my_trace_id to subsequent trace file names for easy identification with the following: ALTER SESSION SET TRACEFILE_IDENTIFIER = 'my_trace_id'; See
Also: If the operating system retains multiple versions of files, then be sure that the version limit is high enough to accommodate the number of trace files you expect the SQL Trace facility to generate. The generated trace files can be owned by an operating system user other than yourself. This user must make the trace files available to you before you can use TKPROF to format them. See Also: "Setting
the Level of Statistics Collection" for information about STATISTICS_LEVEL
settings Step
2: Enabling the SQL Trace Facility DBMS_SESSION.SET_SQL_TRACE
procedure ------------------------------------------------------------------------------------------------ Because running the SQL Trace facility increases system overhead, enable it only when tuning SQL statements, and disable it when you are finished. You might need to modify an application to contain the ALTER SESSION statement. For example, to issue the ALTER SESSION statement in Oracle Forms, invoke Oracle Forms using the -s option, or invoke Oracle Forms (Design) using the statistics option. For more information on Oracle Forms, see the Oracle Forms Reference. ------------------------------------------------------------------------------------------------ To disable the SQL Trace facility for the session, enter: ALTER SESSION SET SQL_TRACE = FALSE;
You can enable the SQL Trace facility for an instance by setting the value of the SQL_TRACE initialization parameter to TRUE in the initialization file. SQL_TRACE = TRUE After the instance has been restarted with the updated initialization parameter file, SQL Trace is enabled for the instance and statistics are collected for all sessions. If the SQL Trace facility has been enabled for the instance, you can disable it for the instance by setting the value of the SQL_TRACE parameter to FALSE. ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- After the SQL Trace facility has generated a number of trace files, you can: Run
TKPROF on each individual trace file, producing a number of formatted
output files, one for each session. Sample
TKPROF Output SELECT
* FROM emp, dept
Misses
in library cache during parse: 1
For this statement, TKPROF output includes the following information: The
text of the SQL statement Syntax
of TKPROF tkprof
filename1 filename2 [waits=yes|no] [sort=option] [print=n]
Table
20-2 TKPROF Arguments The specified user must be able to issue INSERT, SELECT, and DELETE statements against the table. If the table does not already exist, then the user must also be able to issue CREATE TABLE and DROP TABLE statements. For the privileges to issue these statements, see the Oracle Database SQL Reference. This option allows multiple individuals to run TKPROF concurrently with the same user in the EXPLAIN value. These individuals can specify different TABLE values and avoid destructively interfering with each other's processing on the temporary plan table.
If
no plan table exists, TKPROF creates the table PROF$PLAN_TABLE and then
drops it at the end.
The syntax for the trcsess utility is: trcsess
[output=output_file_name]
Output
specifies the file where the output is generated. If this option is not
specified, then standard output is used for the output. Explain Plan Understanding EXPLAIN PLAN The EXPLAIN PLAN statement displays execution plans chosen by the Oracle optimizer for SELECT, UPDATE, INSERT, and DELETE statements. A statement's execution plan is the sequence of operations Oracle performs to run the statement. The row source tree is the core of the execution plan. It shows the following information: An
ordering of the tables referenced by the statement Optimization,
such as the cost and cardinality of each operation With the query optimizer, execution plans can and do change as the underlying optimizer inputs change. EXPLAIN PLAN output shows how Oracle runs the SQL statement when the statement was explained. This can differ from the plan during actual execution for a SQL statement, because of differences in the execution environment and explain plan environment. Execution plans can differ due to the following: Different Schemas The
execution and explain plan happen on different databases. Different Costs Even if the schemas are the same, the optimizer can choose different execution plans if the costs are different. Some factors that affect the costs include the following: Data
volume and statistics Looking Beyond Execution Plans The
execution plan operation alone cannot differentiate between well-tuned
statements and those that perform poorly. For example, an EXPLAIN PLAN
output that shows that a statement uses an index does not necessarily
mean that the statement runs efficiently. Sometimes indexes can be extremely
inefficient. In this case, you should examine the following: Their selectivity (fraction of table being accessed) It is best to use EXPLAIN PLAN to determine an access plan, and then later prove that it is the optimal plan through testing. When evaluating a plan, examine the statement's actual resource consumption. Using V$SQL_PLAN Views In addition to running the EXPLAIN PLAN command and displaying the plan, you can use the V$SQL_PLAN views to display the execution plan of a SQL statement: After the statement has executed, you can display the plan by querying the V$SQL_PLAN view. V$SQL_PLAN contains the execution plan for every statement stored in the cursor cache. Its definition is similar to the PLAN_TABLE. See "PLAN_TABLE Columns". The advantage of V$SQL_PLAN over EXPLAIN PLAN is that you do not need to know the compilation environment that was used to execute a particular statement. For EXPLAIN PLAN, you would need to set up an identical environment to get the same plan when executing the statement. The V$SQL_PLAN_STATISTICS view provides the actual execution statistics for every operation in the plan, such as the number of output rows and elapsed time. All statistics, except the number of output rows, are cumulative. For example, the statistics for a join operation also includes the statistics for its two inputs. The statistics in V$SQL_PLAN_STATISTICS are available for cursors that have been compiled with the STATISTICS_LEVEL initialization parameter set to ALL. The V$SQL_PLAN_STATISTICS_ALL view enables side-by-side comparisons of the estimates that the optimizer provides for the number of rows and elapsed time. This view combines both V$SQL_PLAN and V$SQL_PLAN_STATISTICS information for every cursor. Installing the Trace Analyzer Download the latest version of this tool from TRCA.zip Unzip into a dedicated directory on either the database server, or a client that can connect to the database server. Connect into SQL*Plus with USER that created Raw SQL Trace to be analyzed. If using Oracle Apps, connect as APPS USER. Execute script to create Staging Repository and Package to be used by the Trace Analyzer: sqlplus scott/tiger SQL> START TRCACREA.sql; When the Raw SQL Trace resides under the UDUMP Directory where it was created, there is no need to create any other Directory Alias. But if the Raw SQL Trace was moved into any other Directory on the database server, a new Directory Alias pointing to this non UDUMP Directory must be created: sqlplus system/<system_pwd> SQL> START TRCADIRA.sql my_directory D:\ORACLE\ADMIN\SRIDEVI\UDUMP\ SCOTT; If you get some PLS-00201 errors while installing the Staging Repository or executing the Trace Analyzer, you may need to create some GRANTs for the USER which will be using the Trace Analyzer. If this is the case, use the example below. You would have to connect into SQL*Plus as SYS or SYSTEM. SQL> START TRCAGRNT.sql scott; If you are on RDBMS 9.0.x, and you got some 'ORA-00942: table or view does not exist' errors, you need to install this TRCA tool on a dictionary managed tablespace. Create a dictionary managed tablespace using example below, and modify script TRCAREPO.sql to use this new tablespace. # sqlplus scott/tiger SQL>
create tablespace TRCA datafile '/oracle/em40db/oradata/em40/trca01.dbf' Trace Analyzer reports, by default, all SQL commands executed while tracing was active, including recursive SYS commands. If for any reason, you want to exclude from Trace Analyzer report the recursive SQL executed by user SYS, use the TRCAISYS.sql script provided. This script, when executed with parameter value of NO, provides same functionality than TKPROF and sys=no parameter. To reset to default behavior (sys=yes), execute with value of YES. #
sqlplus apps/<apps_pwd> -------------------------------------------------------------------------------- Maintaining
the Staging Repository |