Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Explain Plan

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

EXPLAIN PLAN

EXPLAIN PLAN statement display execution plans chosen by the oracle


optimezer for SELECT, UPDATE, INSERT and DELETE statements. Execution
plan is the sequence of operations oracle performs to run the statements.
Explain plan cannot tell what plan was actually used to run a given query in
the past, because that query execution could have taken place in a session
with very different settings.
For example, a query run in a session with a large sort area size may well use
a different plan than the same query in a session with a small sort area size.
IT SHOWS THE FOLLOWING INFORMATION:
An ordering of the tables referenced by the statement.
An access method for each table mentioned in the statement.
A join method for tables affected by join operations in the statement.
Data operations like filter, sort , or aggeration.
optimization, such as the cost and cardinality of each operation.
partitioning, such as the set of accessed partitions.
parallel execution, such as the distribution method of join inputs.

First we have to create PLAN_TABLE: Execute utlxplan.sql script which


contains a DDL statements for PLAN_TABLE. This is the table in which
EXPALIN PLAN places the query plan.
use utlxplp.sql, to display the contents of the plan table, including
information specific to parallel-query plans.
use utlxpls.sql to display the contents of the plan table for normal, serial
plans.
The above scripts are available in @ORACLE_HOME/rdbms/admin path.
DBMS_XPALN package contains DBMS_XPLAN.DISPLAY that makes it very
easy to query the plan table. The function accepts options for display the
plan table output.
Explain plan command is

EXPLAIN PLAN

[SET STATEMENT_ID='TEXT']
[INTO [OWNER.]TABLE_NAME]

/*OPTIONAL*/
/*OPTIONAL*/

FOR
SQL STATEMENT;

DBMS_XPALN.DISPLAY:
SYNTAX:
SELECT PLAN_TABLE_OUTPUT
FROM TABLE(DBMS_XPALN.DISPLAY
( PLAN_TABLE ,

/*plan table other than the plan_table , optional*/

statement_id,
optional*/

/*text in quotes to identify a plan in plan table,

level of detail

/*BASIC, SERIAL, TYPICAL,ALL, optional*/

));
Example:
SELECT PLAN_TABLE.OUTPUT FROM TABLE(DBMS_XPALN.DISPLAY());
SELECT PLAN_TABLE_OUTPUT FROM
TABLE(DBMS_XPLAN.DISPLAY('MY_PLAN_TABLE', 'ST1','TYPICAL'));
EXAMPLE:
EXPLAIN PLAN
SET STATEMENT_ID='STMT001'
FOR
SELECT LAST_NAME, FIRST_NAME,SALARY, DEPT_NAME
FROM HR.EMP A , DEPT B
WHERE A.DEPT_ID<10
SELECT PLAN_TABLE_OUTPUT
FROM TABLE(DBMS_XPALN.DISPLAY('PLAN_TABLE','STMT001', 'TYPICAL');

COMMAND:
SELECT PLAN_TABLE_OUTPUT
FROM TABLE(DBMS_XPALN.DISPLAY('PLAN_TABLE',STATEMENT _ID, LEVEL OF
DETAILS));

FULL TABLE SCAN:

INDEX SCAN:

HASH JOIN:

LIMITATIONS:
EXPALIN PLAN is a way to get the query plan for a given Sql query , to
current environment.
using AUTOTRACE we can view the plan as well as the statistics details.
using V$SQL_PLAN we can view the actual plan that is used to execute the
query.
using TKPROF utility, we can capture the actual plan used at runtime. it
requires that tracing was enabled when the query was executed.
The best way to review a query plan is using
DBMS_XPLAN.DISPLAY_CURSOR which display optimizer estimates side
by side with actual execution metrics.

AUTOTRACE
In SQL*Plus you can automatically get a report on the execution path used by the
SQL optimizer and the statement execution statistics. The report is generated after a
successful SQL DML statement, such asSELECT, DELETE, UPDATE or INSERT. It is useful
for monitoring and tuning the performance of these DML statements.

Configuring the Autotrace Report

You can control the report by setting the AUTOTRACE system variable. See Table 11-1.
Table 11-1 Autotrace Settings
Autotrace Setting

Result

SET AUTOTRACE
OFF

No AUTOTRACE report is generated. This is the default.

SET AUTOTRACE ON
EXPLAIN

The AUTOTRACE report shows only the optimizer execution path.

SET AUTOTRACE ON
STATISTICS

The AUTOTRACE report shows only the SQL statement execution statistics.

SET AUTOTRACE ON

The AUTOTRACE report includes both the optimizer execution path and the
SQL statement execution statistics.

SET AUTOTRACE
TRACEONLY

Similar to SET AUTOTRACE ON, but suppresses the printing of the user's
query output, if any. If STATISTICS is enabled, query data is still fetched,
but not printed.

Setups Required for the Autotrace Report

To use this feature, the PLUSTRACE role must be granted to the user, such as HR. DBA
privileges are required to grant the PLUSTRACE role.
Additionally, a PLAN_TABLE table must be created in the user's schema, such as
the HR schema. For information on creating the PLAN_TABLE, see "Creating the
PLAN_TABLE Output Table".
To create the PLUSTRACE role and grant it to the DBA, run the commands
in Example 11-1 from a SQL*Plus session.
Example 11-1 Creating the PLUSTRACE Role
CONNECT / AS SYSDBA
@$ORACLE_HOME/SQLPLUS/ADMIN/PLUSTRCE.SQL
drop role plustrace;
Role dropped.
create role plustrace;
Role created.

.
grant plustrace to dba with admin option;
Grant succeeded.

To grant the PLUSTRACE role to the HR user, run the commands in Example 11-2 from a
SQL*Plus session.
Example 11-2 Granting the PLUSTRACE Role
CONNECT / AS SYSDBA
GRANT PLUSTRACE TO HR;
Grant succeeded.

How is AUTOTRACE Different from EXPLAIN


PLAN?
The main difference between the AUTOTRACE and EXPLAIN PLAN commands
in Oracle is that AUTOTRACE actually executes the query (in the way TRACE
does) and automatically queries the plan table, whereas EXPLAIN PLAN does
neither. The AUTOTRACE command generates similar information, as shown
in the next listing. To use AUTOTRACE, the user must possess the PLUSTRACE
role (by running plustrce.sql, which is usually located in the
ORACLE_HOME/sqlplus/admin directory).

AUTOTRACE
In SQL*Plus you can automatically get a report on the execution path used
by the SQL optimizer and the statement execution statistics. The report is
generated after a successful SQL DML statement, such asSELECT, DELETE,
UPDATE or INSERT. It is useful for monitoring and tuning the performance of
these DML statements.
Configuring the Autotrace Report
You can control the report by setting the AUTOTRACE system variable. See
Table 11-1.
Table 11-1 Autotrace Settings
Autotrace Setting Result
SET AUTOTRACE OFF
default.

No AUTOTRACE report is generated. This is the

SET AUTOTRACE ON EXPLAIN The AUTOTRACE report shows only the


optimizer execution path.
SET AUTOTRACE ON STATISTICS
The AUTOTRACE report shows only the
SQL statement execution statistics.
SET AUTOTRACE ON
The AUTOTRACE report includes both the optimizer
execution path and the SQL statement execution statistics.
SET AUTOTRACE TRACEONLY Similar to SET AUTOTRACE ON, but suppresses
the printing of the user's query output, if any. If STATISTICS is enabled, query
data is still fetched, but not printed.
Setups Required for the Autotrace Report
To use this feature, the PLUSTRACE role must be granted to the user, such as
HR. DBA privileges are required to grant the PLUSTRACE role.
Additionally, a PLAN_TABLE table must be created in the user's schema, such
as the HR schema. For information on creating the PLAN_TABLE, see
"Creating the PLAN_TABLE Output Table".
To create the PLUSTRACE role and grant it to the DBA, run the commands in
Example 11-1 from a SQL*Plus session.
Example 11-1 Creating the PLUSTRACE Role
CONNECT / AS SYSDBA
@$ORACLE_HOME/SQLPLUS/ADMIN/PLUSTRCE.SQL

drop role plustrace;


Role dropped.
create role plustrace;
Role created.
.
grant plustrace to dba with admin option;
Grant succeeded.

To grant the PLUSTRACE role to the HR user, run the commands in Example
11-2 from a SQL*Plus session.

Example 11-2 Granting the PLUSTRACE Role


CONNECT / AS SYSDBA
GRANT PLUSTRACE TO HR;
Grant succeeded.

How is AUTOTRACE Different from EXPLAIN PLAN?


The main difference between the AUTOTRACE and EXPLAIN PLAN commands
in Oracle is that AUTOTRACE actually executes the query (in the way TRACE
does) and automatically queries the plan table, whereas EXPLAIN PLAN does
neither. The AUTOTRACE command generates similar information, as shown
in the next listing. To use AUTOTRACE, the user must possess the PLUSTRACE
role (by running plustrce.sql, which is usually located in the
ORACLE_HOME/sqlplus/admin directory).

You might also like