Professional Documents
Culture Documents
OPTIMIZATION
Page 1
Execution Plan
To execute a DML statement, Oracle may
need to perform many steps. The combination
of the steps Oracle uses to execute a statement
is called an execution plan. An execution plan
includes an access method for each table that
the statement accesses and an ordering of the
tables.
Page 2
Steps of Execution Plan
page 3
RBO VS CBO
Page 4
Choosing the default Optimizer
An Optimizer for a database can be choose by changing
the OPTIMIZER_MODE in the database parameter file.
The following are the parameters for CBO :
CHOOSE: Enable cost based optimizer.
ALL_ROWS: Optimizes with a goal of minimum
resource use to complete the entire statement.
FIRST_ROWS: Optimizes with a goal of best response
time i.e. Minimum resource use to return the first few
rows of the result sets.
RULE: This restricts Oracle to use a Rule-based approach.
Page 5
Explain Plain
The EXPLAIN PLAN statement displays execution plans
chosen by the Oracle optimizer for any DML statements. A
statement’s execution plan is the sequence of operations
Oracle performs to execute the statement. The components of
execution plan include:
1.An ordering of the tables referenced by the statement.
2.An access method for each table mentioned in the statement.
3.A join method of tables affected by the join operations in the
statement.
Page 6
Using the EXPLAIN PLAN
Creating The Output Table:
Before issuing an EXPLAIN PLAN statement, create a table to
hold its output. Run the SQL script UTLXPLAN.SQL to create a
sample output table called PLAN_TABLE.
Run utlbstat.sql and then given the command ‘ANALYZE
TABLE EMP COMPUTE STATISTICS’ to compute the
statistics.
Displaying PLAN_TABLE output:
Display the most recent plan table output using the following
scripts:
UTLXPLS.SQL: Shows plan table output for serial processing.
UTLSPLP.SQL: Shows plan table output with parallel execution
columns and also indentify the number of rows processed by each
step in the plan, which helps to identify inefficiencies in the
After creating a explain table, we can explain the execution plan
of a query.
E.g. Explain plan set statement_id=‘a1’ for select * from emp;
page 8
USING HINTS
Hints are used to speed up the DML statements we run on our database.
Oracle makes it possible to speed performance of insert statements using
some technique to set a direct path to disk for your inserted data. A direct
path is a method for loading data in Oracle that bypass certain aspects of
the Oracle in order to improve the performance of the data. To make it
possible we have to pass some hints to the Oracle optimizer.
Let us consider the following example:
insert into balraj.emp (select * from scott.emp);
The above statement takes time to be executed as it follows the default
Oracle optimizer rule. TO speed it up we can follow the following
process:
Insert /* append */ into balraj.emp nologging (select * from scott.emp);
Page 9
The AUTOTRACE utility
This utility can be used to trace the queries automatically.
SQL*PLUS will execute the query and display the execution plan
following the results.
Consider the following example:
SET AUTOTRACE ON explain
SELECT * FROM SCOTT.EMP ORDER BY ENAME;
OR
SET AUTOTRACE TRACEONLY EXPLAIN
select * from scott.emp order by ename;
Using this we don’t need to query the plan table.
After using AUTOTRACE, we can turn it off by issuing SET
AUTOTRACE OFF.
SQL trace and TKPROF
The SQL trace facility and TKPROF let us accurately access the
efficiency of the SQL statements. When this facility is enabled,
performance statistics for all SQL statements executed in a user
session are placed into trace files. We can run the TKPROF
program to format the contents of the trace file and place the
output into a readable output file.The following steps are to be
followed to deal with the this.
Page 11
Using SQL trace and TKPROF
The following steps can be followed for using this utility.
1. Set initialization parameters for trace file management.
TIMED_STATISTICS:-Controls whether Oracle tracks CPU
and elapsed time for each statement. This should be TRUE.
MAX_DUMP_FILE_SIZE:-Control the maximum size of the
trace file generated by the SQL Trace facility.
USER_DUMP_DEST:- Points to the directory where trace files
are created.
We can find our trace file by issuing the following command:
SELECT VALUE FROM V$PARAMETER WHERE
NAME=‘USER_DUMP_DEST
2.Enabling the SQL trace
feature