Tracing Users' SQL Statements Administration TipsCopyright © Howard Rogers 2001 10/18/2001 Page
1 of 4
Tracing Users' SQL Statements
When tuning a database, it's important to know what SQL is being issued by Users, and howit is being parsed and executed. There are a number of approaches to obtaining suchinformation.
If you know the SQL being submitted, you can simply generate an explain plan for it, andhave 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 inthe 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 doneonce), you issue this sort of command:
EXPLAIN PLAN FOR
This doesn't actually execute the query (so there are no performance or I/O complicationsto worry about), but the plan that
be produced were it to be submitted by a Userfor real is written into PLAN_TABLE. You can thus simply query the table, and see whatsort of execution plan is being produced for each statement. You probably want totruncate the table after each analysis, so it doesn't get cluttered with a myriad ofexecution plans.
SQL TRACE and TKPROF
Instead of writing the plan into a static table, Oracle is able to generate a user-processtrace file containing the crucial information. Unfortunately, the tracefile output is onlyreadable by humans if they have advanced degrees in hieroglyphics and the subtleties ofSumerian palace intrigues. Therefore, we have to pipe the raw stuff through the TKPROFutility 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 parametertimed_statistics is set to TRUE, and that user_dump_dest is set to something sensible (adirectory where trace files can be found easily). You might also want to check thatmax_dump_file_size is set to, say, 1M so as to prevent absolutely mammoth trace filesbeing generated by mistake.Then you need simply switch tracing on. If it's your own session, you simply issue thecommand: