Sub topic 10
Getting the Actual Plan with `DBMS_XPLAN.DISPLAY_CURSOR`
This chapter assumes you can read basic SQL execution plans and understand tables, indexes, joins, and WHERE clauses. You also need permission to use DBMS_XPLAN and query the dynamic performance views, such as V$SQL.
Why `DISPLAY_CURSOR` Matters
EXPLAIN PLAN shows an estimated plan that Oracle might use. It does not execute the statement. The optimizer can choose a different plan when the statement actually runs.
DBMS_XPLAN.DISPLAY_CURSOR shows the plan stored in the cursor cache for a statement that has run. A cursor is Oracle's parsed and executable representation of SQL. With runtime statistics enabled, the output also shows what happened during execution.
The important distinction is:
| Tool | Shows | Executes the SQL? |
|---|---|---|
| `DBMS_XPLAN.DISPLAY` | Estimated plan in `PLAN_TABLE` | No |
| `DBMS_XPLAN.DISPLAY_CURSOR` | Plan used by a cached cursor | The statement must already have run |
Capture the Last Statement in Your Session
Add the GATHER_PLAN_STATISTICS hint to the query. This asks Oracle to collect row-source statistics for that 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'));The LAST option requests statistics for the most recent execution of each operation. The output can include:
E-Rows: estimated rowsA-Rows: actual rows producedStarts: number of times an operation startedBuffers: logical readsA-Time: elapsed time spent in the operation
DISPLAY_CURSOR with no sql_id uses the last SQL statement executed in your current session. In SQL*Plus or SQLcl, avoid running another statement between the query and DISPLAY_CURSOR. For example, a command that prints server output can become the statement Oracle considers last.
Display a Specific Cached Cursor
A statement can have more than one cursor. Different bind values, optimizer settings, or object changes can produce separate child cursors. Use V$SQL to find the SQL_ID and child number.
SELECT sql_id,
child_number,
executions,
sql_text
FROM v$sql
WHERE sql_text LIKE '%FROM hr.employees e%'
AND sql_text NOT LIKE '%v$sql%';Then pass the values to DISPLAY_CURSOR:
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(
sql_id => '4j3m8nq2v7k1p',
cursor_child_no => 0,
format => 'ALLSTATS LAST'));The cursor must still be in the shared pool. If Oracle has aged it out, DISPLAY_CURSOR cannot retrieve it.
Read the Runtime Columns
Compare estimated and actual rows at each operation. A large difference can cause Oracle to choose a poor join method or access path.
For example, E-Rows = 10 and A-Rows = 10 indicate a close estimate. E-Rows = 10 and A-Rows = 50000 indicate a cardinality misestimate. Check the operation where the difference first appears, not only the final result.
ALLSTATS LAST reports statistics for the last execution captured. It does not automatically represent every execution of the cursor.
Exercise
Exercise
Which option should you use to display the plan and actual row statistics for the last execution of a statement in your session?
Show answer
Run the statement with the GATHERPLANSTATISTICS hint, then call DBMSXPLAN.DISPLAYCURSOR with format => 'ALLSTATS LAST'.