How To Fetch Explain Plan


Displaying an execution plan is made easy if you use the DBMS_XPLAN package.

This packages provides several PL/SQL procedures to display the plan from different sources:

• EXPLAIN PLAN command
• Automatic Workload Repository (AWR)
• SQL Tuning Set (STS)
• SQL Plan Baseline (SPM)

The following examples illustrate how to generate and display an execution plan for our original SQL statement using the different functions provided in the dbms_xplan package.


Example 1: Uses the EXPLAIN PLAN command and the DBMS_XPLAN.DISPLAY function.



The arguments are for DBMS_XPLAN.DISPLAY are:
• Plan table name (default ‘PLAN_TABLE’)
• Statement_id (default NULL)
• Format (default ‘TYPICAL’)
More details can be found in $ORACLE_HOME/rdbms/admin/dbmsxpln.sql.


Example 2: Generating and displaying the execution plan for the last SQL statement executed in a session:


The arguments used by DBMS_XPLAN.DISPLAY_CURSOR are:
• SQL ID (default NULL, which means the last SQL statement executed in this session)
• Child number (default 0)
• Format (default ‘TYPICAL’)
The details are in $ORACLE_HOME/rdbms/admin/dbmsxpln.sql.


Example 3: Displaying the execution plan for any other statement requires the SQL ID to be provided, either directly or indirectly:




Example 4: Displaying an execution plan corresponding to a SQL Plan Baseline.


If the above statement has been executed more than once, a SQL Plan Baseline will be created for it and you can verified this using the follows query:


The execution plan for the SQL Plan Baseline created above can be displayed either directly or indirectly:




The output of either of these two statements is:



The format argument is highly customizable and allows you to see as little (high-level) or as much (low-level) details as you need / want in the plan output. The high-level options are:

1. Basic
The plan includes the operation, options, and the object name (table, index, MV, etc)

2. Typical
It includes the information shown in BASIC plus additional optimizer-related internal information such as cost, size, cardinality, etc.

These information are shown for every operation in the plan and represents what the optimizer thinks is the operation cost, the number of rows produced, etc.

It also shows the predicates evaluation by the operation. There are two types of predicates: ACCESS and FILTER.

The ACCESS predicates for an index are used to fetch the relevant blocks because they apply to the search columns.

The FILTER predicates are evaluated after the blocks have been fetched.

3. All
It includes the information shown in TYPICAL plus the lists of expressions (columns) produced by every operation, the hint alias and query block names where the operation belongs.

The last two pieces of information can be used as arguments to add hints to the statement.

The low-level options allow the inclusion or exclusion of find details, such as predicates and cost.


For example:


Note Section
In addition to the plan, the package displays notes in the NOTE section, such as that dynamic sampling was used during query optimization or that star transformation was applied to the query.

For example, if the table SALES did not have statistics then the optimizer will use dynamic sampling and the plan display will report it as follows (see s’+note’ detail in the query):


Bind Peeking
The query optimizer takes into account the values of bind variable values when generation an execution plan.

It does what is generally called bind peeking. See the first post in this blog about the concept of bind peeking and its impact on the plans and the performance of SQL statements.

As stated earlier the plan shown in V$SQL_PLAN takes into account the values of bind variables while the one shown from using EXPLAIN PLAN does not.

The DBMS_XPLAN package allows the display of the bind variable values used to generate a particular cursor/plan.

This is done by adding ‘+peeked_binds’ to the format argument when using display_cursor().

This is illustrated with the following example:

Avinav Chadha
Author: Avinav Chadha

I have been supporting and managing oracle databases for almost 4 years now.

Leave a Reply

Your email address will not be published. Required fields are marked *