SlideShare a Scribd company logo
1 of 4
Download to read offline
Tracing Users' SQL Statements                                                 Administration Tips




Tracing Users' SQL Statements

When tuning a database, it's important to know what SQL is being issued by Users, and how
it is being parsed and executed. There are a number of approaches to obtaining such
information.


EXPLAIN PLAN
If you know the SQL being submitted, you can simply generate an explain plan for it, and
have that plan sotred within a table created specially for the purpose.

To create the explain plan table, you execute the utlxplan.sql script provided by Oracle in
the ORACLE_HOME/rdbms/admin directory. It creates a table called, cunningly enough,
PLAN_TABLE. Once that's done (and, since it's a genuine table, it only needs to be done
once), you issue this sort of command:

EXPLAIN PLAN FOR
SELECT    *   FROM SCOTT.EMP;


This doesn't actually execute the query (so there are no performance or I/O complications
to worry about), but the plan that would be produced were it to be submitted by a User
for real is written into PLAN_TABLE. You can thus simply query the table, and see what
sort of execution plan is being produced for each statement. You probably want to
truncate the table after each analysis, so it doesn't get cluttered with a myriad of
execution plans.




SQL TRACE and TKPROF
Instead of writing the plan into a static table, Oracle is able to generate a user-process
trace file containing the crucial information. Unfortunately, the tracefile output is only
readable by humans if they have advanced degrees in hieroglyphics and the subtleties of
Sumerian palace intrigues. Therefore, we have to pipe the raw stuff through the TKPROF
utility to turn it into something mere mortals can make sense of.

To enable this functionality, you need first to make sure that the init.ora parameter
timed_statistics is set to TRUE, and that user_dump_dest is set to something sensible (a
directory where trace files can be found easily). You might also want to check that
max_dump_file_size is set to, say, 1M so as to prevent absolutely mammoth trace files
being generated by mistake.

Then you need simply switch tracing on. If it's your own session, you simply issue the
command:


Copyright © Howard Rogers 2001           10/18/2001                                    Page 1 of 4
Tracing Users' SQL Statements                                                  Administration Tips


ALTER SESSION SET SQL_TRACE=TRUE


But if you are doing this on behalf of another User, you need first to determine the SID and
SERIAL# that uniquely identify their session:

SELECT USERNAME, SID, SERIAL# FROM V$SESSION;


...followed by:

EXECUTE DBMS_SESSION.SET_SQL_TRACE_IN_SESSION(7,32,TRUE)


Those two numbers in the brackets are the SID and SERIAL# just determined by querying
v$session. Note that you'll probably need to be connected as SYS for this to work.

Any new SQL statements issued by the specified session will now be traced, and the output
will be going to some trace file in the directory pointed at by the user_dump_dest
parameter. You may find it tricky to work out which tracefile belongs to which session,
because if you do tracing even vaguely regularly, there will probably be dozens of likely
candidates to choose from.

The simplest way to sort that out is to use the time and date stamp on the files themselves,
but if you need a more accurate way to determine the filename in which to get interested,
try this query:

SELECT P.SPID
FROM V$PROCESS P,V$SESSION S
WHERE P.ADDR=S.PADDR
AND S.SID=&SID AND S.SERIAL#=&SERIALNO;


When run, you'll be prompted to supply the SID and SERIAL# values previously used when
switching tracing on in the first place ...and that gives you the process number that will be
included in the trace file name (on NT, you'll have a file called
ORA0<process_number>.TRC, and on Unix it will generally be called
sid_ora_<process_no>.trc).

Do not attempt to work with the tracefile, though, until you have remembered to
switch SQL tracing OFF for the relevant session -otherwise the tracefile doesn't get
closed properly, and if it has any contents at all, they probably won't be useable.

To switch tracing off for a session, simply enter the command:

EXECUTE DBMS_SESSION.SET_SQL_TRACE_IN_SESSION(7,32,FALSE)


Again, the two numbers specified there are the SID and SERIAL# numbers, determined from
v$session, that uniquely identify any Oracle session.

Copyright © Howard Rogers 2001            10/18/2001                                    Page 2 of 4
Tracing Users' SQL Statements                                                   Administration Tips




Now you have a tracefile, properly identifiable, it's time to format it using the tkprof
utility. Just type the following command at the operating system prompt:

TKPROF TRACEFILE.TRC OUTPUTFILE.TXT


...and that will produce a text file (of whatever name you specify) which contains the full
parse and execute statistics for the previously-selected session for the time during which
tracing was switched on. Obviously, when you do this for real, substitute in the correct
name for the trace file being formatted, and choose an appropriate output file name.

The tkprof utility takes a number of options, but probably the most useful one would be to
switch off recursive SQL. This means that all the internal queries that go on under the
hood when a User issues a SQL command are stripped out of the output file, leaving behind
only the commands that the User would identify as having been issued by himself. To do
that, you'd type the following command:

TKPROF TRACEFILE.TRC OUTPUT.TXT SYS=NO




AUTOTRACE
An alternative way of obtaining trace information is to use the SQL Plus feature called
'autotrace'. For this to work, you still need an explain plan table (see above for how to
create one, but the script involved is called utlxplan.sql). Additionally, you need to
execute the plustrace.sql script (supplied by Oracle, and available in the
ORACLE_HOME/sqlplus/admin directory), in order to create a PLUSTRACE role. You can
then grant that role to Users who don't have the DBA role, and they'll be able to generate
traces successfully. Without this step, only DBAs can enable autotrace.

The final step is to issue the command:

SET AUTOTRACE ON


Note that this is an internal SQL Plus command, not a SQL statement -so there's no
concluding semi-colon, and the command won't work in something like Server Manager.

After that, every SQL statement issued in the session will not only cause the statement to
be executed, but will display trace statistics for that statement, too.

The advantages of this over setting sql traceing on are obvious: the feedback is immediate,
and there's no need to run tkprof to format the output before it makes sense. There are
severe drawbacks, too, though: you can't set autotrace on on behalf of another User -it
has to be enabled by a session, and then only that session's SQL statements are traced.
What's more, unlike the Explain Plan, the SQL statements being issued within the session

Copyright © Howard Rogers 2001             10/18/2001                                    Page 3 of 4
Tracing Users' SQL Statements                                                   Administration Tips


are actually executed. When you use the "explain plan for..." method, the ensuing SQL
statement is not actually executed, but merely parsed. The final killer drawback is that
autotrace is SQL Plus specific -it can't work in 3rd party applications, nor in Server Manager.

Note that autotrace can be set to various other states than simply ON and OFF. For
example, there are these variants:

SET AUTOTRACE TRACEONLY
SET AUTOTRACE ON EXPLAIN
SET AUTOTRACE TRACEONLY STATISTICS


The first of these variants suppresses the display of the actual results of the SQL
statements being issued. The execution of the SQL still takes place, but no results are
actually returned to the session. All you see are the trace statistics for the parse and
execute phases.

The second option displays the result set of the SQL statement being traced, along with
the execution plan, but no parse or execute statistics are displayed.

And the third option suppresses the result set of the SQL statement being traced, along
with the parse and execute statistics, but the execution plan is not shown

When you've finished tracing a session, just issue the command:

SET AUTOTRACE OFF


...and SQL Plus will revert to its normal behaviour.




Copyright © Howard Rogers 2001            10/18/2001                                     Page 4 of 4

More Related Content

What's hot

你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能maclean liu
 
DBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowDBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...
Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...
Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...Massimo Cenci
 
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...Michael Rosenblum
 
A New View of Database Views
A New View of Database ViewsA New View of Database Views
A New View of Database ViewsMichael Rosenblum
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, proceduresVaibhav Kathuria
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 
SQL injection: Not Only AND 1=1 (updated)
SQL injection: Not Only AND 1=1 (updated)SQL injection: Not Only AND 1=1 (updated)
SQL injection: Not Only AND 1=1 (updated)Bernardo Damele A. G.
 
Validating The Existence of a Table Without Using DBA Metadata
Validating The Existence of a Table Without Using DBA MetadataValidating The Existence of a Table Without Using DBA Metadata
Validating The Existence of a Table Without Using DBA MetadataLuiscarlo Palomino Romero
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 
Introduction to embedded sql for NonStop SQL
Introduction to embedded sql for NonStop SQLIntroduction to embedded sql for NonStop SQL
Introduction to embedded sql for NonStop SQLFrans Jongma
 
Oracle Data redaction - GUOB - OTN TOUR LA - 2015
Oracle Data redaction - GUOB - OTN TOUR LA - 2015Oracle Data redaction - GUOB - OTN TOUR LA - 2015
Oracle Data redaction - GUOB - OTN TOUR LA - 2015Alex Zaballa
 
Database administration commands
Database administration commands Database administration commands
Database administration commands Varsha Ajith
 
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321maclean liu
 

What's hot (20)

你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能
 
DBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowDBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should Know
 
Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...
Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...
Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...
 
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
 
A New View of Database Views
A New View of Database ViewsA New View of Database Views
A New View of Database Views
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
PHP tips by a MYSQL DBA
PHP tips by a MYSQL DBAPHP tips by a MYSQL DBA
PHP tips by a MYSQL DBA
 
SQL injection: Not Only AND 1=1 (updated)
SQL injection: Not Only AND 1=1 (updated)SQL injection: Not Only AND 1=1 (updated)
SQL injection: Not Only AND 1=1 (updated)
 
Oracle ORA Errors
Oracle ORA ErrorsOracle ORA Errors
Oracle ORA Errors
 
Validating The Existence of a Table Without Using DBA Metadata
Validating The Existence of a Table Without Using DBA MetadataValidating The Existence of a Table Without Using DBA Metadata
Validating The Existence of a Table Without Using DBA Metadata
 
Pl sql using_xml
Pl sql using_xmlPl sql using_xml
Pl sql using_xml
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
 
Sqlmap
SqlmapSqlmap
Sqlmap
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
 
Pl sql-ch3
Pl sql-ch3Pl sql-ch3
Pl sql-ch3
 
Introduction to embedded sql for NonStop SQL
Introduction to embedded sql for NonStop SQLIntroduction to embedded sql for NonStop SQL
Introduction to embedded sql for NonStop SQL
 
Oracle Data redaction - GUOB - OTN TOUR LA - 2015
Oracle Data redaction - GUOB - OTN TOUR LA - 2015Oracle Data redaction - GUOB - OTN TOUR LA - 2015
Oracle Data redaction - GUOB - OTN TOUR LA - 2015
 
Database administration commands
Database administration commands Database administration commands
Database administration commands
 
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
 

Viewers also liked (13)

Createclone
CreatecloneCreateclone
Createclone
 
Configuremts
ConfiguremtsConfiguremts
Configuremts
 
Columndrop
ColumndropColumndrop
Columndrop
 
Corruptbkp
CorruptbkpCorruptbkp
Corruptbkp
 
Applyinga blockcentricapproach
Applyinga blockcentricapproachApplyinga blockcentricapproach
Applyinga blockcentricapproach
 
Perfstats
PerfstatsPerfstats
Perfstats
 
Rollbackshrinks
RollbackshrinksRollbackshrinks
Rollbackshrinks
 
Rollbacksizes
RollbacksizesRollbacksizes
Rollbacksizes
 
Rollbackblocking
RollbackblockingRollbackblocking
Rollbackblocking
 
Userlimit
UserlimitUserlimit
Userlimit
 
Tablerename
TablerenameTablerename
Tablerename
 
Sequencereset
SequenceresetSequencereset
Sequencereset
 
Undo internalspresentation
Undo internalspresentationUndo internalspresentation
Undo internalspresentation
 

Similar to Usertracing

Performance tuning a quick intoduction
Performance tuning   a quick intoductionPerformance tuning   a quick intoduction
Performance tuning a quick intoductionRiyaj Shamsudeen
 
Watch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML UtilitiesWatch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML Utilitiesdpcobb
 
Sql interview question part 8
Sql interview question part 8Sql interview question part 8
Sql interview question part 8kaashiv1
 
Performance tuning
Performance tuningPerformance tuning
Performance tuningami111
 
Live Query Statistics & Query Store in SQL Server 2016
Live Query Statistics & Query Store in SQL Server 2016Live Query Statistics & Query Store in SQL Server 2016
Live Query Statistics & Query Store in SQL Server 2016Antonios Chatzipavlis
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...Alex Zaballa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...Alex Zaballa
 
Oracle Sql Tuning
Oracle Sql TuningOracle Sql Tuning
Oracle Sql TuningChris Adkin
 
Top 5 things to know about sql azure for developers
Top 5 things to know about sql azure for developersTop 5 things to know about sql azure for developers
Top 5 things to know about sql azure for developersIke Ellis
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Netwebhostingguy
 
One Click Ownage Ferruh Mavituna (3)
One Click Ownage Ferruh Mavituna (3)One Click Ownage Ferruh Mavituna (3)
One Click Ownage Ferruh Mavituna (3)Ferruh Mavituna
 
Applying profilers to my sql (fosdem 2017)
Applying profilers to my sql (fosdem 2017)Applying profilers to my sql (fosdem 2017)
Applying profilers to my sql (fosdem 2017)Valeriy Kravchuk
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Alex Zaballa
 
SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6Mahesh Vallampati
 

Similar to Usertracing (20)

Performance tuning a quick intoduction
Performance tuning   a quick intoductionPerformance tuning   a quick intoduction
Performance tuning a quick intoduction
 
Watch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML UtilitiesWatch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML Utilities
 
Using AWR for SQL Analysis
Using AWR for SQL AnalysisUsing AWR for SQL Analysis
Using AWR for SQL Analysis
 
Sql interview question part 8
Sql interview question part 8Sql interview question part 8
Sql interview question part 8
 
Ebook8
Ebook8Ebook8
Ebook8
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
Oracle Tracing
Oracle TracingOracle Tracing
Oracle Tracing
 
Live Query Statistics & Query Store in SQL Server 2016
Live Query Statistics & Query Store in SQL Server 2016Live Query Statistics & Query Store in SQL Server 2016
Live Query Statistics & Query Store in SQL Server 2016
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
Oracle Sql Tuning
Oracle Sql TuningOracle Sql Tuning
Oracle Sql Tuning
 
Mysql
MysqlMysql
Mysql
 
Sherlock holmes for dba’s
Sherlock holmes for dba’sSherlock holmes for dba’s
Sherlock holmes for dba’s
 
Ash and awr deep dive hotsos
Ash and awr deep dive hotsosAsh and awr deep dive hotsos
Ash and awr deep dive hotsos
 
Top 5 things to know about sql azure for developers
Top 5 things to know about sql azure for developersTop 5 things to know about sql azure for developers
Top 5 things to know about sql azure for developers
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
 
One Click Ownage Ferruh Mavituna (3)
One Click Ownage Ferruh Mavituna (3)One Click Ownage Ferruh Mavituna (3)
One Click Ownage Ferruh Mavituna (3)
 
Applying profilers to my sql (fosdem 2017)
Applying profilers to my sql (fosdem 2017)Applying profilers to my sql (fosdem 2017)
Applying profilers to my sql (fosdem 2017)
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
 
SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6
 

More from oracle documents (20)

Applyinga blockcentricapproachtotuning
Applyinga blockcentricapproachtotuningApplyinga blockcentricapproachtotuning
Applyinga blockcentricapproachtotuning
 
Whatistnsnames
WhatistnsnamesWhatistnsnames
Whatistnsnames
 
Whatisadatabaselink
WhatisadatabaselinkWhatisadatabaselink
Whatisadatabaselink
 
Varraysandnestedtables
VarraysandnestedtablesVarraysandnestedtables
Varraysandnestedtables
 
Userpasswrd
UserpasswrdUserpasswrd
Userpasswrd
 
Undo internals paper
Undo internals paperUndo internals paper
Undo internals paper
 
Tablespacelmt
TablespacelmtTablespacelmt
Tablespacelmt
 
Sql scripting sorcerypresentation
Sql scripting sorcerypresentationSql scripting sorcerypresentation
Sql scripting sorcerypresentation
 
Sql scripting sorcerypaper
Sql scripting sorcerypaperSql scripting sorcerypaper
Sql scripting sorcerypaper
 
Sql for dbaspresentation
Sql for dbaspresentationSql for dbaspresentation
Sql for dbaspresentation
 
Rollbacklmt
RollbacklmtRollbacklmt
Rollbacklmt
 
Rollback1555s
Rollback1555sRollback1555s
Rollback1555s
 
Redosize
RedosizeRedosize
Redosize
 
Real liferecoverypresentation
Real liferecoverypresentationReal liferecoverypresentation
Real liferecoverypresentation
 
Real liferecoverypaper
Real liferecoverypaperReal liferecoverypaper
Real liferecoverypaper
 
Oracledates
OracledatesOracledates
Oracledates
 
Ora12154
Ora12154Ora12154
Ora12154
 
Nologging
NologgingNologging
Nologging
 
Migration
MigrationMigration
Migration
 
Loadblobs
LoadblobsLoadblobs
Loadblobs
 

Usertracing

  • 1. Tracing Users' SQL Statements Administration Tips Tracing Users' SQL Statements When tuning a database, it's important to know what SQL is being issued by Users, and how it is being parsed and executed. There are a number of approaches to obtaining such information. EXPLAIN PLAN If you know the SQL being submitted, you can simply generate an explain plan for it, and have that plan sotred within a table created specially for the purpose. To create the explain plan table, you execute the utlxplan.sql script provided by Oracle in the ORACLE_HOME/rdbms/admin directory. It creates a table called, cunningly enough, PLAN_TABLE. Once that's done (and, since it's a genuine table, it only needs to be done once), you issue this sort of command: EXPLAIN PLAN FOR SELECT * FROM SCOTT.EMP; This doesn't actually execute the query (so there are no performance or I/O complications to worry about), but the plan that would be produced were it to be submitted by a User for real is written into PLAN_TABLE. You can thus simply query the table, and see what sort of execution plan is being produced for each statement. You probably want to truncate the table after each analysis, so it doesn't get cluttered with a myriad of execution plans. SQL TRACE and TKPROF Instead of writing the plan into a static table, Oracle is able to generate a user-process trace file containing the crucial information. Unfortunately, the tracefile output is only readable by humans if they have advanced degrees in hieroglyphics and the subtleties of Sumerian palace intrigues. Therefore, we have to pipe the raw stuff through the TKPROF utility to turn it into something mere mortals can make sense of. To enable this functionality, you need first to make sure that the init.ora parameter timed_statistics is set to TRUE, and that user_dump_dest is set to something sensible (a directory where trace files can be found easily). You might also want to check that max_dump_file_size is set to, say, 1M so as to prevent absolutely mammoth trace files being generated by mistake. Then you need simply switch tracing on. If it's your own session, you simply issue the command: Copyright © Howard Rogers 2001 10/18/2001 Page 1 of 4
  • 2. Tracing Users' SQL Statements Administration Tips ALTER SESSION SET SQL_TRACE=TRUE But if you are doing this on behalf of another User, you need first to determine the SID and SERIAL# that uniquely identify their session: SELECT USERNAME, SID, SERIAL# FROM V$SESSION; ...followed by: EXECUTE DBMS_SESSION.SET_SQL_TRACE_IN_SESSION(7,32,TRUE) Those two numbers in the brackets are the SID and SERIAL# just determined by querying v$session. Note that you'll probably need to be connected as SYS for this to work. Any new SQL statements issued by the specified session will now be traced, and the output will be going to some trace file in the directory pointed at by the user_dump_dest parameter. You may find it tricky to work out which tracefile belongs to which session, because if you do tracing even vaguely regularly, there will probably be dozens of likely candidates to choose from. The simplest way to sort that out is to use the time and date stamp on the files themselves, but if you need a more accurate way to determine the filename in which to get interested, try this query: SELECT P.SPID FROM V$PROCESS P,V$SESSION S WHERE P.ADDR=S.PADDR AND S.SID=&SID AND S.SERIAL#=&SERIALNO; When run, you'll be prompted to supply the SID and SERIAL# values previously used when switching tracing on in the first place ...and that gives you the process number that will be included in the trace file name (on NT, you'll have a file called ORA0<process_number>.TRC, and on Unix it will generally be called sid_ora_<process_no>.trc). Do not attempt to work with the tracefile, though, until you have remembered to switch SQL tracing OFF for the relevant session -otherwise the tracefile doesn't get closed properly, and if it has any contents at all, they probably won't be useable. To switch tracing off for a session, simply enter the command: EXECUTE DBMS_SESSION.SET_SQL_TRACE_IN_SESSION(7,32,FALSE) Again, the two numbers specified there are the SID and SERIAL# numbers, determined from v$session, that uniquely identify any Oracle session. Copyright © Howard Rogers 2001 10/18/2001 Page 2 of 4
  • 3. Tracing Users' SQL Statements Administration Tips Now you have a tracefile, properly identifiable, it's time to format it using the tkprof utility. Just type the following command at the operating system prompt: TKPROF TRACEFILE.TRC OUTPUTFILE.TXT ...and that will produce a text file (of whatever name you specify) which contains the full parse and execute statistics for the previously-selected session for the time during which tracing was switched on. Obviously, when you do this for real, substitute in the correct name for the trace file being formatted, and choose an appropriate output file name. The tkprof utility takes a number of options, but probably the most useful one would be to switch off recursive SQL. This means that all the internal queries that go on under the hood when a User issues a SQL command are stripped out of the output file, leaving behind only the commands that the User would identify as having been issued by himself. To do that, you'd type the following command: TKPROF TRACEFILE.TRC OUTPUT.TXT SYS=NO AUTOTRACE An alternative way of obtaining trace information is to use the SQL Plus feature called 'autotrace'. For this to work, you still need an explain plan table (see above for how to create one, but the script involved is called utlxplan.sql). Additionally, you need to execute the plustrace.sql script (supplied by Oracle, and available in the ORACLE_HOME/sqlplus/admin directory), in order to create a PLUSTRACE role. You can then grant that role to Users who don't have the DBA role, and they'll be able to generate traces successfully. Without this step, only DBAs can enable autotrace. The final step is to issue the command: SET AUTOTRACE ON Note that this is an internal SQL Plus command, not a SQL statement -so there's no concluding semi-colon, and the command won't work in something like Server Manager. After that, every SQL statement issued in the session will not only cause the statement to be executed, but will display trace statistics for that statement, too. The advantages of this over setting sql traceing on are obvious: the feedback is immediate, and there's no need to run tkprof to format the output before it makes sense. There are severe drawbacks, too, though: you can't set autotrace on on behalf of another User -it has to be enabled by a session, and then only that session's SQL statements are traced. What's more, unlike the Explain Plan, the SQL statements being issued within the session Copyright © Howard Rogers 2001 10/18/2001 Page 3 of 4
  • 4. Tracing Users' SQL Statements Administration Tips are actually executed. When you use the "explain plan for..." method, the ensuing SQL statement is not actually executed, but merely parsed. The final killer drawback is that autotrace is SQL Plus specific -it can't work in 3rd party applications, nor in Server Manager. Note that autotrace can be set to various other states than simply ON and OFF. For example, there are these variants: SET AUTOTRACE TRACEONLY SET AUTOTRACE ON EXPLAIN SET AUTOTRACE TRACEONLY STATISTICS The first of these variants suppresses the display of the actual results of the SQL statements being issued. The execution of the SQL still takes place, but no results are actually returned to the session. All you see are the trace statistics for the parse and execute phases. The second option displays the result set of the SQL statement being traced, along with the execution plan, but no parse or execute statistics are displayed. And the third option suppresses the result set of the SQL statement being traced, along with the parse and execute statistics, but the execution plan is not shown When you've finished tracing a session, just issue the command: SET AUTOTRACE OFF ...and SQL Plus will revert to its normal behaviour. Copyright © Howard Rogers 2001 10/18/2001 Page 4 of 4