Professional Documents
Culture Documents
Introduction
As a DBA, you are responsible for optimizing a SQL statement using the EXPLAIN PLAN
statement in case of performance problems. Your jobs responsibilities dictate that you should at
least be informed of the following basic fundamental subjects:
Using the EXPLAIN PLAN statement
Creating the PLAN_TABLE table
Submitting a SQL statement using the EXPLAIN PLAN statement
Using the SET STATEMENT_ID clause
Recalling the EXECUTION plan from the PLAN_TABLE table
Understanding of the following operations:
TABLE ACCESS FULL
TABLE ACCESS BY INDEX
INDEX UNIQUE SCAN
NESTED LOOPS
MERGE JOIN
FILTER
SORT AGGREGATE
Commands:
START %ORACLE_HOME%
EXPLAIN PLAN SET STATEMENT_ID=
Hands-on
In this exercise you will learn how to use the explain plan statement to determine how the
optimizer will execute the query in question.
First, let's connect to SQLPlus as the ISELF user.
SQL> CONNECT iself/schooling
Check to see if the PLAN_TABLE exists in the user's schema.
SQL> DESC plan_table
Create PLAN_TABLE
In order to optimize a SQL statement, you execute the EXPLAIN PLAN statement to populate a
list plan of execution in PLAN_TABLE. Then you write a SQL statement against the table to
query a plan of execution list generated by EXPLANIN PLAN.
If PLAN_TABLE does not exist, run the utlxplan.sql script provided in the rdbmsfolder to create
the PLAN_TABLE table.
SQL> START %ORACLE_HOME%
Now, the PLAN_TABLE table was created.
Check the number of records in the table.
SQL> SELECT count(1)
FROM plan_table
/
There should be no records in the table.
SORT GROUP BY
"SORT GROUP BY" means Oracle will perform a sort on the data obtained for the user.
1 0 SORT GROUP BY
FILTER
"FILTER" means that this is an operation that adds selectivity to a TABLE ACCESS FULL
operation, based on the contents of the where clause.
2 1 FILTER
NESTED LOOPS
"NESTED LOOPS" indicates that the join statement is occurring.
3 2 NESTED LOOPS
MERGE JOIN
"MERGE JOIN" indicates that the join statement is occurring.
4 3 MERGE JOIN
SORT JOIN
"SORT JOIN" indicates that the join statement is sorting. "TABLE ACCESS FULL" means that
Oracle will look at every row in the table (slowest way).
5 4 SORT JOIN
6 5 TABLE ACCESS FULL STOCKS
7 4 SORT JOIN
8 7 TABLE ACCESS FULL PORTFOLIO
TABLE ACCESS BY INDEX
"TABLE ACCESS BY INDEX" means that Oracle will use the ROWID method to find a row in
the table. It is very fast.
9 3 TABLE ACCESS BY INDEX ROWID CUSTOMERS
INDEX UNIQUE SCAN
"INDEX UNIQUE SCAN" means Oracle will use the primary or unique key. This is the most
efficient way to search an index.
10 9 INDEX UNIQUE SCAN SYS_C003126
SORT AGGREGATE
"SORT AGGREGATE" means Oracle will perform a sort on the data obtained for the user.
11 2 SORT AGGREGATE
12 11 TABLE ACCESS FULL STOCKS
Read your output and see what the problem of the query is