Explain Plan
Explain Plan
Explain Plan
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 ,
statement_id,
optional*/
level of detail
));
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));
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.
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
SET AUTOTRACE ON
EXPLAIN
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.
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.
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.
To grant the PLUSTRACE role to the HR user, run the commands in Example
11-2 from a SQL*Plus session.