Sub topic 9
Why EXPLAIN PLAN May Not Show the Real Plan
This chapter assumes that you know basic SELECT, joins, WHERE clauses, tables, indexes, and primary keys. You also need access to an Oracle schema and permission to inspect statements in the cursor cache.
What EXPLAIN PLAN Shows
EXPLAIN PLAN asks the optimizer to estimate how Oracle would execute a statement. It stores that estimated plan in a table named PLAN_TABLE. It does not run the statement.
EXPLAIN PLAN FOR
SELECT e.last_name, d.department_name
FROM hr.employees e
JOIN hr.departments d
ON d.department_id = e.department_id
WHERE e.salary > 10000;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);The displayed plan is useful for learning the planned access paths, join methods, estimated row counts, and predicates. However, it is a prediction made at the time of the EXPLAIN PLAN call.
Why the Estimate Can Differ
Oracle can choose a different plan when the application actually executes the statement. Common reasons include:
- Table and index statistics changed between the two operations.
- The session used different optimizer settings.
- The statement contained bind variables, and the real values affected selectivity.
- Oracle created multiple child cursors for different bind values or environments.
- A SQL plan baseline, SQL profile, or SQL patch influenced the executed cursor.
- Adaptive cursor sharing selected a different plan for a particular bind value.
- The statement was executed under a different schema, privilege set, or object name resolution.
- The query text was not exactly the same, including hints or whitespace that changed the cursor identity.
EXPLAIN PLAN also does not show execution effects such as actual row counts, physical reads, buffer gets, or elapsed time.
Use DISPLAY_CURSOR for the Executed Plan
DBMS_XPLAN.DISPLAY_CURSOR reads the plan from the cursor cache. The following hint tells Oracle to collect row-source statistics for this execution:
SELECT /*+ GATHER_PLAN_STATISTICS */
e.last_name,
d.department_name
FROM hr.employees e
JOIN hr.departments d
ON d.department_id = e.department_id
WHERE e.salary > 10000;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(
format => 'ALLSTATS LAST'));With no sql_id, DISPLAY_CURSOR normally displays the last statement executed in the current session. Run the query first, then run the display call. Otherwise, the display call might show a different last statement.
The ALLSTATS LAST format adds statistics for the last execution. These statistics help you compare the optimizer's estimates with what happened.
| Evidence | Meaning |
|---|---|
| E-Rows | Rows the optimizer estimated |
| A-Rows | Rows actually produced |
| Buffers | Logical reads used by an operation |
| A-Time | Time spent by an operation |
DISPLAY_CURSOR requires suitable privileges, which vary by Oracle version and installation. The cursor must also still be available in the shared pool.
A Simple Comparison
If E-Rows and A-Rows differ greatly at an operation, the optimizer misestimated the number of rows there. That difference can explain why Oracle selected an unsuitable join method or access path. Investigate statistics, data distribution, predicates, and bind values before changing indexes or adding hints.
When EXPLAIN PLAN Is Still Useful
Use EXPLAIN PLAN when the statement is dangerous or expensive and you do not want to run it. Use DISPLAY_CURSOR when you need evidence from a real execution. For a reliable diagnosis, compare the estimated plan with the executed plan under the same session conditions.
Exercise
Why can EXPLAIN PLAN and DISPLAY_CURSOR show different plans for apparently the same SQL?
Show answer
EXPLAIN PLAN creates a new estimate, while DISPLAY_CURSOR reads the plan selected for an actual cursor. Statistics, bind values, session settings, and SQL management features can differ.