2. Tuning and Monitoring
Better Technology, Better Tomorrow
2
Table of Contents
1. Viewing SQL Query Execution Result using SQL Trace and tbPROF...........................................................................................................3
1.1. Using the SQL_TRACE parameter.............................................................................................................................................................3
1.1.1. Setting the SQL_TRACE parameters...........................................................................................................................................3
1.1.2. Verifying the Created SQL Trace File..........................................................................................................................................5
1.1.3. Executing the tbPROF Command................................................................................................................................................5
1.1.4. Analyzing the Output File after Executing tbPROF.................................................................................................................6
1.2. Using the SET_SQL_TRACE_IN_SESSION Procedure............................................................................................................................8
2. Using Autotrace in tbSQL...................................................................................................................................................................................12
3. Using V$SQL_PLAN .............................................................................................................................................................................................16
4. Others.....................................................................................................................................................................................................................18
4.1. The SQL_TRACE_DEST Parameter.........................................................................................................................................................18
4.2. Privilege Issues when using AUTOTRACE in TBSQL ..........................................................................................................................18
3. Tuning and Monitoring
Better Technology, Better Tomorrow
3
Tibero SQL Execution Plan Guide
1. Viewing SQL Query Execution Results using SQL Trace and tbPROF
To collect SQL execution information, SQL trace files can be created statically or dynamically using SQL_TRACE
parameters.
- SQL statement executions are traced.
- A target statement for tuning can be easily found in the execution plan.
- Since the contents of a SQL trace are stored in a file, a query process can be slow.
1.1. Using the SQL_TRACE parameter
1.1.1. Setting the SQL_TRACE parameters
SQL_TRACE determines whether to record SQL trace information. An SQL trace enables profiling of SQL execution
information. However, using this function can severely impact performance, so it is recommended not to use this
function in the real production environment.
Property Description
Type Boolean
Default Value N
Class Optional, Adjustable, Dynamic, Session
Configuration Method Configure the TIP file and restart the server, or change the value using the
ALTER statement.
Syntax • • TIP File
- SQL_TRACE={Y|N}
• • ALTER Statement
- ALTER SYSTEM SET SQL_TRACE={Y|N}
- ALTER SESSION SET SQL_TRACE={Y|N}
Examples
-- Query the SQL_TRACE information in V$SESSION.
SQL> SELECT SID, SERIAL#, USERNAME, SQL_TRACE, TO_CHAR(LOGON_TIME, 'DD HH24:MI:SS')
LOGON_TIME
FROM V$SESSION
ORDER BY LOGON_TIME;
SID SERIAL# USERNAME SQL_TRACE LOGON_TIME
---------- ---------- --------- --------- -----------
9 4 SYS ENABLED 18 15:43:29
20 83 SYS ENABLED 18 15:44:47
19 567 SYS DISABLED 18 15:52:50
3 rows selected.
-- Use a text editor to modify the %$TB_HOME%/config/%TB_SID%.tip file.
-- Set SQL_TRACE=Y and restart Tibero via tbboot.
#-------------------------------------------------------------------------------
#
# RDBMS initialization parameter
#
4. Tuning and Monitoring
Better Technology, Better Tomorrow
4
#-------------------------------------------------------------------------------
DB_NAME=tibero
LISTENER_PORT=8629
CONTROL_FILES="C:/Tibero/tbdata/c1.ctl"
CERTIFICATE_FILE="C:/Tibero/tibero5/config/svr_wallet/tibero.crt"
#PRIVKEY_FILE="C:/Tibero/tibero5/config/svr_wallet/tibero.key"
#WALLET_FILE="C:/Tibero/tibero5/config/svr_wallet/WALLET"
DB_CREATE_FILE_DEST=C:/Tibero/tbdata
LOG_ARCHIVE_DEST= C:/Tibero/arch
MAX_SESSION_COUNT=10
TOTAL_SHM_SIZE=512M
UNDO_RETENTION=900
_TSN_TIME_MAP_SIZE=1000
SQL_TRACE=Y
---------------------------------------------------------------------------------
-- Access tbSQL.
$ tbsql sys/tibero
tbSQL 5
Copyright (c) 2008, 2009, 2011, 2012 Tibero Corporation. All rights reserved.
Connected to Tibero.
-- Verify that SQL_TRACE has been set to Y using the V$SESSION view.
SQL> SELECT SID, SERIAL#, USERNAME, SQL_TRACE, TO_CHAR(LOGON_TIME, 'DD HH24:MI:SS')
LOGON_TIME
FROM V$SESSION
ORDER BY LOGON_TIME;
SID SERIAL# USERNAME SQL_TRACE LOGON_TIME
---------- ---------- --------- --------- -----------
9 4 SYS ENABLED 18 15:43:29
20 83 SYS ENABLED 18 15:44:47
19 567 SYS ENABLED 18 15:52:50
3 rows selected.
-- Set SQL_TRACE=Y and execute an SQL query.
SQL> SELECT e.employee_id, e.first_name||' '||e.last_name emp_name, d.department_id,
d.department_name
FROM departments d
LEFT OUTER JOIN employees e
ON(e.department_id=d.department_id);
5. Tuning and Monitoring
Better Technology, Better Tomorrow
5
1.1.2. Verifying the Created SQL Trace File
The trace file is placed in the following directory with the '.trc' extension.
File Location: $TB_HOME/instance/$TB_SID/log/sqltrace
File Naming Rule
- A file name is created using the PID, SID, and serial# of the session.
E.g., tb_sqltrc_PID_SID_serial#.trc
- The PID, SID, and serial# information can be queried using the v$session view.
-- Query the PID, SID, and serial# information using the V$SESSION view.
SQL> SELECT PID, SID, SERIAL#, USERNAME, SQL_TRACE, TO_CHAR(LOGON_TIME, 'DD
HH24:MI:SS') LOGON_TIME
FROM V$SESSION
ORDER BY LOGON_TIME;
PID SID SERIAL# USERNAME SQL_TRACE LOGON_TIME
---------- ---------- --------- --------- ---------- ------------
3179 9 4 SYS ENABLED 18 15:43:29
3180 20 83 SYS ENABLED 18 15:44:47
3181 19 567 SYS ENABLED 18 15:52:50
3 rows selected.
-- Check the tb_sqltrc_3181_19_567.trc file created in the
$TB_HOME/instance/$TB_SID/log/sqltrace directory.
$ cd tibero5/instance/tibero/log/sqltrace
$ ls
tb_sqltrc_3180_1_3.trc tb_sqltrc_3181_20_3524.trc
tb_sqltrc_3180_9_4.trc tb_sqltrc_3181_20_83.trc
tb_sqltrc_3181_19_2766.trc tb_sqltrc_3181_21_7024.out
tb_sqltrc_3181_19_442.trc tb_sqltrc_3181_21_7024.trc
tb_sqltrc_3181_19_50.trc tb_sqltrc_3181_21_7829.trc
tb_sqltrc_3181_19_567.trc tb_sqltrc_3183_4_0.trc
1.1.3. Executing the tbPROF Command
This command provides the information about the SQL in the parse, execution, and fetch stages. It sorts the
result of the user-specified field to display the result.
The following is an example of checking the tbPROF parameters and executing the tbPROF command.
Checking the tbPROF parameter
$ tbprof
Usage: tbprof tracefile outputfile [print= ] [sort= ] [aggregate= ]
print=integer List only the first 'integer' SQL statements.
sys=yes|no Filter SQL statements that 'SYS' user executes.
aggregate=yes|no Aggregate statistics of same SQL statements.
sort=option Set of zero or more of the following sort options:
prscnt number of times parse was called
prscpu cpu time parsing
prsela elapsed time parsing
prsdsk number of disk reads during parse
prsqry number of buffers for consistent read during parse
prscu number of buffers for current read during parse
execnt number of execute was called
6. Tuning and Monitoring
Better Technology, Better Tomorrow
6
execpu cpu time spent executing
exeela elapsed time executing
exedsk number of disk reads during execute
exeqry number of buffers for consistent read during execute
execu number of buffers for current read during execute
exerow number of rows processed during execute
fchcnt number of times fetch was called
fchcpu cpu time spent fetching
fchela elapsed time fetching
fchdsk number of disk reads during fetch
fchqry number of buffers for consistent read during fetch
fchcu number of buffers for current read during fetch
fchrow number of rows fetched
userid userid of user that parsed the cursor
Item Description Default
tracefile
Name of the file that contains statistical information created by the SQL
trace.
-
outputfile Uses tbPROF to format the contents of the trace file into a readable text file. -
print Prints the trace results of the specified number of SQL statements only. ALL
sys Determines whether to list the SQL statements issued by the user SYS. yes
aggregate Determines whether to aggregate the information of the same SQL. no
sort
Determines the result sort method.
- Depending on SORT_OPTION, sorts results in descending order.
- More than one option can be specified. Fields are separated by a comma
',' and cannot contain whitespace.
-
1.1.4. Analyzing the Output File after Executing tbPROF
Use tbPROF to analyze the information of the SQL executed. Check the information about the executed SQL
statements including statistical information and execution plans in the output file.
Creating an output file using tbPROF
$tbprof tbprof tb_sqltrc_3181_19_567.trc tb_sqltrc_3181_19_567.out sys=no
TBPROF 5
Copyright (c) 2008, 2009, 2011, 2012 Tibero Corporation. All rights reserved.
$
Checking the output file
$ vi tb_sqltrc_3181_19_567.out
TBPROF 5
Copyright (c) 2008, 2009, 2011, 2012 Tibero Corporation. All rights reserved.
input file name : tb_sqltrc_3181_21_7024.trc
output file name : tb_sqltrc_3181_21_7024.out
sort option : default
aggregate : no
7. Tuning and Monitoring
Better Technology, Better Tomorrow
7
sys : no
print : all
=================================================================
count: number of times the procedure was executed
cpu: CPU time(seconds).
this is not quite accurate due to threaded architecture
elapsed: Total time elapsed (seconds)
disk: number of physical reads from disk
query: number of blocks for consistent read
current: number of blocks in current mode
rows: number of rows processed
=================================================================
UserID:20
SELECT e.employee_id, e.first_name||' '||e.last_name emp_name, d.department_id,
d.department_name
FROM departments d
LEFT OUTER JOIN employees e
ON(e.department_id=d.department_id)
stage count cpu elapsed current query disk rows
-----------------------------------------------------------------------------
parse 1 0.00 0.00 0 0 0 0
exec 1 0.00 0.00 0 0 0 0
fetch 1 0.00 0.00 0 10 0 20
-----------------------------------------------------------------------------
sum 3 0.00 0.00 0 10 0 20
- cpu: CPU time. (sec)
- elapsed: Total time elapsed. (sec)
- current: Number of current blocks retrieved.
- query: Number of cr blocks retrieved.
- disk: Number of physical reads from disk.
- rows: Number of rows processed
rows execution plan
----------------------------------------------------------
20 index join (left outer) (et=93, cr=0, cu=0, co=4, cpu=0, ro=20)
8 table access (rowid) DEPARTMENTS(1749) (et=426, cr=1, cu=0, co=2, cpu=0, ro=8)
8 index (full) DEPT_ID_PK(1750) (et=52, cr=1, cu=0, co=1, cpu=0, ro=8)
19 table access (rowid) EMPLOYEES(1755) (et=354, cr=7, cu=0, co=2, cpu=0, ro=2)
19 index (range scan) EMP_DEPARTMENT_IX(1764) (et=125, cr=1, cu=0, co=1, cpu=0, ro=2)
*******************************************************************************
OVERALL TOTAL
stage count cpu elapsed current query disk rows
-----------------------------------------------------------------------------
parse 1 0.00 0.00 0 0 0 0
exec 1 0.00 0.00 0 0 0 0
fetch 1 0.00 0.00 0 10 0 20
-----------------------------------------------------------------------------
sum 3 0.00 0.00 0 10 0 20
*******************************************************************************
1 SQL statements in trace file.
1 unique SQL statements in trace file.
8. Tuning and Monitoring
Better Technology, Better Tomorrow
8
16 lines in trace file.
tbPROF Statistics Information
Call Value Description
Parse
- Converts the SQL statement into an execution plan, including checks for tables,
columns, and reference objects.
Execute
- Executes INSERT, UPDATE, and DELETE statements, which modify the data
according to the execution plan.
- Indicates the number of selected rows during execution of the statement.
Fetch
- Indicates the number of rows returned by a query.
- Fetches are performed for SELECT statements.
Argument Description
Count - Number of times an SQL statement was parsed, executed, or fetched.
CPU - CPU time spent to parse, execute, or fetch calls. (sec)
Elapsed - Total elapsed time, from the start to the end of a process.
Current
- Number of dirty blocks accessed. A dirty block is a block that have been modified
by a session but not yet written to the database.
- Dirty blocks seldom occur during the execution of SELECT statements, but most
often occur during the execution of UPDATE, INSERT, and DELETE statements.
Query
- Number of unchanged blocks that have not been read, or the number of
snapshot locks and copies of uncommitted data that have been read.
- This sometimes occurs during the execution of UPDATE, DELETE, and INSERT
statements, but often occurs during the execution of SELECT statements.
Disk - Number of data blocks read from the disk.
Rows
- Total number of rows accessed by the SQL statement.
- Does not include rows extracted by sub queries.
et - Time spent on the node. (usec)
cr - Number of cr blocks read from the node.
cu - Number of current blocks read from the node.
co - Cost of the nodes computed by the optimizer.
ro - Number of rows of the node estimated by the optimizer.
1.2. Using the SET_SQL_TRACE_IN_SESSION Procedure
Determining whether to create an SQL TRACE file
- Starts/stops the creation of a SQL trace log for a session. The identifier and the serial number of the
session can be found using the V$SESSION view.
- A target statement for tuning can be easily found in the execution plan.
DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION
9. Tuning and Monitoring
Better Technology, Better Tomorrow
9
Parameter Type Description
SID NUMBER Identifier of the session.
SERIAL# NUMBER Serial number of the session.
SQL_TRACE BOOLEAN Specify as TRUE to create an SQL trace log, or specify false to stop.
SQL Syntax
- EXEC SYS.DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(SID, SERIAL#, SQL_TRACE);
- BEGIN
SYS.DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION (SID, SERIAL#, SQL_TRACE);
END
Examples
-- Access tbSQL.
$ tbsql sys/tibero
tbSQL 5
Copyright (c) 2008, 2009, 2011, 2012 Tibero Corporation. All rights reserved.
Connected to Tibero.
-- Check the SET_SQL_TRACE_IN_SESSION information.
SQL> DESC DBMS_SYSTEM
PROCEDURE 'SET_SQL_TRACE_IN_SESSION'
ARGUMENT_NAME TYPE IN/OUT
------------------------ ------------------ ----------------
SID NUMBER IN
SERIAL# NUMBER IN
SQL_TRACE BOOLEAN IN
-- Query the parameter values using the V$SESSION view.
SQL> SELECT SID, SERIAL#, USERNAME, SQL_TRACE, TO_CHAR(LOGON_TIME, 'DD HH24:MI:SS')
LOGON_TIME
FROM V$SESSION
ORDER BY LOGON_TIME;
SID SERIAL# USERNAME SQL_TRACE LOGON_TIME
---------- ---------- --------- --------- -----------
9 4 SYS ENABLED 18 15:43:29
20 83 SYS ENABLED 18 15:44:47
19 567 SYS DISABLED 18 15:52:50
3 rows selected.
-- Set SQL_TRACE to Y.
SQL> EXEC DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(19, 567, TRUE);
PSM completed.
-- Verify that SQL_TRACE has been set to Y.
SQL> SELECT SID, SERIAL#, USERNAME, SQL_TRACE, TO_CHAR(LOGON_TIME, 'DD HH24:MI:SS')
LOGON_TIME
10. Tuning and Monitoring
Better Technology, Better Tomorrow
10
FROM V$SESSION
ORDER BY LOGON_TIME;
SID SERIAL# USERNAME SQL_TRACE LOGON_TIME
---------- ---------- --------- --------- -----------
9 4 SYS ENABLED 18 15:43:29
20 83 SYS ENABLED 18 15:44:47
19 567 SYS ENABLED 18 15:52:50
3 rows selected.
-- Set SQL_TRACE to Y and execute an SQL query.
SQL> SELECT e.employee_id, e.first_name||' '||e.last_name emp_name, d.department_id,
d.department_name
FROM departments d
LEFT OUTER JOIN employees e
ON(e.department_id=d.department_id);
-- Query the PID, SID, and serial# information using the V$SESSION view.
SQL> SELECT PID, SID, SERIAL#, USERNAME, SQL_TRACE, TO_CHAR(LOGON_TIME, 'DD
HH24:MI:SS') LOGON_TIME
FROM V$SESSION
ORDER BY LOGON_TIME;
PID SID SERIAL# USERNAME SQL_TRACE LOGON_TIME
---------- ---------- --------- --------- ---------- ------------
3179 9 4 SYS ENABLED 18 15:43:29
3180 20 83 SYS ENABLED 18 15:44:47
3181 19 567 SYS ENABLED 18 15:52:50
3 rows selected.
-- Check the tb_sqltrc_3181_19_567.trc file created in the
$TB_HOME/instance/$TB_SID/log/sqltrace directory.
$ cd tibero5/instance/tibero/log/sqltrace
$ ls
tb_sqltrc_3180_1_3.trc tb_sqltrc_3181_20_3524.trc
tb_sqltrc_3180_9_4.trc tb_sqltrc_3181_20_83.trc
tb_sqltrc_3181_19_2766.trc tb_sqltrc_3181_21_7024.out
tb_sqltrc_3181_19_442.trc tb_sqltrc_3181_21_7024.trc
tb_sqltrc_3181_19_50.trc tb_sqltrc_3181_21_7829.trc
tb_sqltrc_3181_19_567.trc tb_sqltrc_3183_4_0.trc
-- Create an output file.
$tbprof tbprof tb_sqltrc_3181_19_567.trc tb_sqltrc_3181_19_567.out sys=no
TBPROF 5
Copyright (c) 2008, 2009, 2011, 2012 Tibero Corporation. All rights reserved.
$
-- Check the contents of the output file. (Statistical information)
$ vi tb_sqltrc_3181_19_567.out
11. Tuning and Monitoring
Better Technology, Better Tomorrow
11
TBPROF 5
Copyright (c) 2008, 2009, 2011, 2012 Tibero Corporation. All rights reserved.
input file name : tb_sqltrc_3181_21_7024.trc
output file name : tb_sqltrc_3181_21_7024.out
sort option : default
aggregate : no
sys : no
print : all
=================================================================
count: number of times the procedure was executed
cpu: CPU time(seconds)
this is not quite accurate due to threaded architecture
elapsed: elapsed time(seconds)
disk: number of physical reads from disk
query: number of blocks for consistent read
current: number of blocks in current mode
rows: number of rows processed
=================================================================
UserID:20
SELECT e.employee_id, e.first_name||' '||e.last_name emp_name, d.department_id,
d.department_name
FROM departments d
LEFT OUTER JOIN employees e
ON(e.department_id=d.department_id)
stage count cpu elapsed current query disk rows
-----------------------------------------------------------------------------
parse 1 0.00 0.00 0 0 0 0
exec 1 0.00 0.00 0 0 0 0
fetch 1 0.00 0.00 0 10 0 20
-----------------------------------------------------------------------------
sum 3 0.00 0.00 0 10 0 20
- cpu: CPU time. (sec)
- elapsed: Total time elapsed. (sec)
- current: Number of current blocks retrieved.
- query: Number of cr blocks retrieved.
- disk: Number of physical reads from the disk.
- rows: Number of rows processed.
rows execution plan
----------------------------------------------------------
20 index join (left outer) (et=93, cr=0, cu=0, co=4, cpu=0, ro=20)
8 table access (rowid) DEPARTMENTS(1749) (et=426, cr=1, cu=0, co=2, cpu=0, ro=8)
8 index (full) DEPT_ID_PK(1750) (et=52, cr=1, cu=0, co=1, cpu=0, ro=8)
19 table access (rowid) EMPLOYEES(1755) (et=354, cr=7, cu=0, co=2, cpu=0, ro=2)
19 index (range scan) EMP_DEPARTMENT_IX(1764) (et=125, cr=1, cu=0, co=1, cpu=0, ro=2)
*******************************************************************************
OVERALL TOTAL
stage count cpu elapsed current query disk rows
-----------------------------------------------------------------------------
parse 1 0.00 0.00 0 0 0 0
exec 1 0.00 0.00 0 0 0 0
fetch 1 0.00 0.00 0 10 0 20
12. Tuning and Monitoring
Better Technology, Better Tomorrow
12
-----------------------------------------------------------------------------
sum 3 0.00 0.00 0 10 0 20
*******************************************************************************
1 SQL statements in trace file.
1 unique SQL statements in trace file.
16 lines in trace file.
2. Using Autotrace in tbSQL
SQL Syntax
- SET AUTOT[RACE] {OFF|ON|TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]] [PLANS[TAT]]
Setting Description
SET AUTOTRACE OFF AUTOTRACE is not performed. (Default value)
SET AUTOTRACE ON
Shows the execution plan, execution results, and execution
statistics of the SQL statement.
SET AUTOTRACE TRACEONLY
Shows both the execution plan and the execution statistics of
the SQL statement. Fast processing.
SET AUTOTRACE ON EXPLAIN
Shows the execution results and the execution plan of the SQL
statement.
SET AUTOTRACE ON STATISTICS
Shows the execution results and the execution statistics of the
SQL statement.
SET AUTOTRACE ON PLANSTAT
Shows the execution results of the SQL statement and the
query executions per node.
(Time spent executing, number of rows processed, number of
executions)
- When AUTOTRACE is used, OFF, ON, or TRACE[ONLY] must be specified.
- [EXP[LAIN]] [STAT[ISTICS]] [PLANS[TAT]] shows the execution plan, execution statistics, and query
executions in each node, respectively. Execution information includes execution time, number of rows
processed, and number of executions. All, some, or none of the options can be selected.
- AUTOTRACE allows users to selectively view execution results, execution plans, or execution statistics
according to the option.
- Execution results and execution statistics are the results of query executions, and the execution plan
contains estimated values.
Privilege - DBA privilege or the PLUSTRACE role are required.
Shows Execution Result/Execution Plan/Execution Statistics/Execution Information
Category Description
SQL Query SQL> SELECT AVG(SALARY) AVG FROM employee GROUP BY DEPT_CD;
Execution Result
AVG
----------
4666.66667
1 row selected.
13. Tuning and Monitoring
Better Technology, Better Tomorrow
13
Execution Plan
SQL ID: 92
Plan Hash Value: 1910332783
Execution Plan
--------------------------------------------------------------
1 GROUP BY (HASH) (Cost: 26, %CPU: 0, Rows: 1)
2 TABLE ACCESS (FULL): EMPLOYEE (Cost: 26, %CPU: 0, Rows: 3)
Execution
Statistics
NAME VALUE
------------------------------ ---------------------------------------
db block gets 1
consistent gets 14
physical reads 0
redo size 0
sorts (disk) 0
sorts (memory) 0
rows processed 1
- db block gets: Number of times a current block was requested.
- consistent gets: Number of logical blocks read in Consistent mode.
- physical reads: Total number of data blocks read from disk.
- redo size: Size of the redo logs generated. (size)
- sorts (disk): Number of sort operations that were performed in disk.
- sorts (memory): Number of sort operations that were performed in memory.
- rows processed: Number of rows processed during the operation.
Execution
Information
SQL ID: 92
Plan Hash Value: 1910332783
Execution Stat
--------------------------------------------------
1 GROUP BY (HASH) (Time: .83 ms, Rows: 1, Starts: 1)
2 TABLE ACCESS (FULL): EMPLOYEE (Time: .15 ms, Rows: 3, Starts: 1)
Examples
-- Access TBSQL
$ tbsql sys/tibero
tbSQL 5
Copyright (c) 2008, 2009, 2011, 2012 Tibero Corporation. All rights reserved.
Connected to Tibero.
-- Enable the SET AUTOTRACE ON option.
SQL> SET AUTOTRACE ON;
-- Check the execution results, execution plan, and execution statistics of the SQL
query that was performed in the session.
SQL> SELECT AVG(SALARY) AVG FROM employee GROUP BY DEPT_CD;
AVG
14. Tuning and Monitoring
Better Technology, Better Tomorrow
14
----------
4666.66667
1 row selected.
SQL ID: 92
Plan Hash Value: 1910332783
Execution Plan
--------------------------------------------------------------------------------
1 GROUP BY (HASH) (Cost: 26, %CPU: 0, Rows: 1)
2 TABLE ACCESS (FULL): EMPLOYEE (Cost: 26, %CPU: 0, Rows: 3)
NAME VALUE
------------------------------ ----------
db block gets 1
consistent gets 14
physical reads 0
redo size 0
sorts (disk) 0
sorts (memory) 0
rows processed 1
-- Enable the SET AUTOTRACE TRACEONLY option
SQL> SET AUTOTRACE TRACEONLY;
-- Check the execution plan and the execution statistics of the SQL query that was
performed in the session.
SQL> SELECT AVG(SALARY) AVG FROM employee GROUP BY DEPT_CD;
SQL ID: 115
Plan Hash Value: 1910332783
Execution Plan
--------------------------------------------------------------------------------
1 GROUP BY (HASH) (Cost: 26, %CPU: 0, Rows: 1)
2 TABLE ACCESS (FULL): EMPLOYEE (Cost: 26, %CPU: 0, Rows: 3)
NAME VALUE
------------------------------ ----------
db block gets 1
consistent gets 14
physical reads 0
redo size 0
sorts (disk) 0
sorts (memory) 0
rows processed 1
-- Enable the SET AUTOTRACE ON EXPLAIN option
SQL> SET AUTOTRACE ON EXPLAIN;
-- Check the execution results and the execution plan of the SQL query that was
performed in the session.
SQL> SELECT AVG(SALARY) AVG FROM employee GROUP BY DEPT_CD;
AVG
15. Tuning and Monitoring
Better Technology, Better Tomorrow
15
----------
4666.66667
1 row selected.
SQL ID: 92
Plan Hash Value: 1910332783
Execution Plan
--------------------------------------------------------------------------------
1 GROUP BY (HASH) (Cost:26, %CPU:0, Rows:1)
2 TABLE ACCESS (FULL): EMPLOYEE (Cost:26, %CPU:0, Rows:3)
-- Enable the SET AUTOTRACE ON STATISTICS option
SQL> SET AUTOTRACE ON STATISTICS;
-- Check the execution results and the execution statistics of the SQL query that was
performed in the session.
SQL> SELECT AVG(SALARY) AVG FROM employee GROUP BY DEPT_CD;
AVG
----------
4666.66667
1 row selected.
NAME VALUE
------------------------------ ----------
db block gets 1
consistent gets 14
physical reads 0
redo size 0
sorts (disk) 0
sorts (memory) 0
rows processed 1
-- Enable the SET AUTOTRACE ON PLANSTAT option
SQL> SET AUTOTRACE ON PLANSTAT;
-- Check the execution results and the execution information of the SQL query
performed in the session.
SQL> SELECT AVG(SALARY) AVG FROM employee GROUP BY DEPT_CD;
AVG
----------
4666.66667
1 row selected.
SQL ID: 92
Plan Hash Value: 1910332783
Execution Stat
--------------------------------------------------------------------------------
1 GROUP BY (HASH) (Time: .83 ms, Rows: 1, Starts: 1)
16. Tuning and Monitoring
Better Technology, Better Tomorrow
16
2 TABLE ACCESS (FULL): EMPLOYEE (Time: .15 ms, Rows: 3, Starts: 1)
-- Enable the SET AUTOTRACE OFF option
SQL> SET AUTOTRACE OFF;
-- Check the execution results of the SQL query performed in the session and the
result of enabling the AUTOTRACE OFF option.
SQL> SELECT AVG(SALARY) AVG FROM employee GROUP BY DEPT_CD;
AVG
----------
4666.66667
1 row selected.
3. Using V$SQL_PLAN
Displays information about the physical plan for executing SQL statements.
Columns Information
Column Name Data Type Description
HASH_VALUE NUMBER Hash value of the physical plan
SQL_ID NUMBER SQL identifier
OPERATION VARCHAR(128) Name of an operation job
OBJECT# NUMBER Identifier of an object accessed by the job
OBJECT_OWNER VARCHAR(128) Name of the user who owns the object
OBJECT_NAME VARCHAR(128) Object name
OBJECT_TYPE VARCHAR(20) Object type
ID NUMBER Unique number assigned to each task of the physical plan
PARENT_ID NUMBER
ID of the next execution step that operates on the output of
the current step
DEPTH NUMBER Tree level of the physical plan
POSITION NUMBER Position of all operations with the same PARENT_ID
SEARCH_COLUMNS NUMBER Number of keys used for index search
COST NUMBER Cost of an operation estimated by the optimizer
CPU_COST NUMBER CPU cost of an operation estimated by the optimizer
17. Tuning and Monitoring
Better Technology, Better Tomorrow
17
IO_COST NUMBER I/O cost of an operation estimated by the optimizer
CARDINALITY NUMBER Number of rows to display estimated by the optimizer
PSTART VARCHAR(38) First partition to be accessed in the partition table.
PEND VARCHAR(38) Last partition to be accessed in the partition table.
OTHERS VARCHAR(4000)
Execution step related information, which a user can flexibly
use.
ACCESS_PREDICATES VARCHAR(4000) Predicate information for index accesses or join operations
FILTER_PREDICATES VARCHAR(4000) Predicate information for a filter processing
Examples
-- Execute an SQL query.
SQL> SELECT JOB_ID, round(AVG(SALARY)) AVG
FROM EMPLOYEES
GROUP BY JOB_ID order by 2 desc;
JOB_ID AVG
---------- ----------
AD_PRES 24000
AD_VP 17000
MK_MAN 13000
AC_MGR 12000
SA_MAN 10500
SA_REP 8867
AC_ACCOUNT 8300
IT_PROG 6400
MK_REP 6000
ST_MAN 5800
AD_ASST 4400
ST_CLERK 2925
12 rows selected.
-- Search for an SQL_ID using partial character sets of the SQL query.
SQL> select sql_id, sql_text
from v$sqltext
where sql_text like '%FROM EMPLOYEES%';
SQL_ID SQL_TEXT
---------- ----------------------------------------------------------------
521 SELECT JOB_ID, round(AVG(SALARY)) AVG FROM EMPLOYEES GROUP BY JO
-- How to view the full-sql text.
(When querying v$sqltext, the sql text is broken into pieces.)
SQL> select sql_id, aggr_concat(sql_text, '' order by piece) as sql
from v$sqltext
where sql_id=521 group by sql_id;
SQL_ID SQL
---------- --------------------------------------------------------------------------
521 SELECT JOB_ID, round(AVG(SALARY)) AVG FROM EMPLOYEES GROUP BY JOB_ID
18. Tuning and Monitoring
Better Technology, Better Tomorrow
18
order by 2 desc
1 row selected.
-- Identify the SQL query using V$SQL_PLAN. (with the SQL_ID searched.)
SQL> SELECT SUBSTRB(TO_CHAR(ID), 1, 3) || LPAD(' ', LEVEL * 2) || UPPER(OPERATION) ||
DECODE(OBJECT_NAME, NULL, NULL, ': '||OBJECT_NAME) || ' (Cost:' || COST ||
', %%CPU:' || DECODE(COST, 0, 0, TRUNC((COST-IO_COST)/COST * 100))|| ', Rows:'
|| CARDINALITY || ') ' || DECODE(PSTART, '', '', '(PS:' || PSTART || ', PE:' ||
PEND || ')') AS "Execution Plan"
FROM (SELECT * FROM V$SQL_PLAN WHERE SQL_ID = 521)
START WITH DEPTH = 1
CONNECT BY PRIOR ID = PARENT_ID AND PRIOR SQL_ID = SQL_ID
ORDER SIBLINGS BY POSITION;
Execution Plan
-----------------------------------------------------------------------------
1 ORDER BY (SORT) (Cost:26, %%CPU:0, Rows:12)
2 GROUP BY (HASH) (Cost:26, %%CPU:0, Rows:12)
3 TABLE ACCESS (FULL): EMPLOYEES (Cost:26, %%CPU:0, Rows:20)
3 rows selected
4. Others
4.1. The SQL_TRACE_DEST Parameter
SQL_TRACE_DEST specifies the directory in which the SQL trace file is stored. It must be specified as an absolute
path.
SQL trace files are generally stored in the $TB_HOME/instance/$TB_SID/log/sqltrace directory. The directory can
be modified using the following parameters.
Property Description
Type String
Default Value ""
Class Optional, Adjustable, Dynamic, System
Configuration method Set in the TIP file and restart the server, or change the directory using the
ALTER statement.
Syntax • • TIP File
- SQL_TRACE_DEST=<Directory>
• • ALTER Statement
- ALTER SYSTEM SET SQL_TRACE_DEST=<Directory>
4.2. Privilege Issues when using AUTOTRACE in TBSQL
Notes
The following message appears when an SQL query is performed without the PLUSTRACE role. The
PLUSTRACE role must be granted to the user.
-- Access TBSQL and apply the Explain option.
SQL> set autotrace on explain;
19. Tuning and Monitoring
Better Technology, Better Tomorrow
19
-- The following message occurs when an SQL query is performed in the session without
the PLUSTRACE role.
SQL> SELECT AVG(SALARY) AVG FROM employee GROUP BY DEPT_CD;
AVG
----------
4666.66667
1 row selected.
[B]TBR-8033: Specified schema object was not found.
at line 1, column 424:
"Execution Plan" FROM (SELECT * FROM V$SQL_PLAN WHERE SQL_ID = 115 AND HASH_V
^
TBS-70035: Unable to display plan: check PLUSTRACE role.
[B]TBR-8033: Specified schema object was not found.
at line 1, column 98:
RS AS "Remote SQL Information" FROM V$SQL_PLAN WHERE SQL_ID = 115 AND
^
TBS-70035: Unable to display plan: check PLUSTRACE role.
[B]TBR-8033: Specified schema object was not found.
at line 1, column 66:
with x as (select id, access_predicates, filter_predicates from v$sql_plan where
^
TBS-70035: Unable to display plan: check PLUSTRACE role.
-- Create the PLUSTRACE role.
SQL> @plustrace.sql
TBR-7070: Specified role 'PLUSTRACE' was not found.
Role 'PLUSTRACE' created.
Granted.
Granted.
Granted.
Granted.
File finished.
-- Run an SQL query to check the SQL execution plan of the query.
SQL> SELECT AVG(SALARY) AVG FROM employee GROUP BY DEPT_CD;
AVG
----------
4666.66667
1 row selected.
20. Tuning and Monitoring
Better Technology, Better Tomorrow
20
SQL ID: 115
Plan Hash Value: 1910332783
Execution Plan
--------------------------------------------------------------------------------
1 GROUP BY (HASH) (Cost:26, %CPU:0, Rows:1)
2 TABLE ACCESS (FULL): EMPLOYEE (Cost:26, %CPU:0, Rows:3)