Sub topic 10
Getting the actual plan with DISPLAY_CURSOR
This chapter assumes you can read a plan produced by EXPLAIN PLAN and DBMS_XPLAN.DISPLAY. That means you know the Id, Operation and Name columns, the indentation rules, and the difference between access and filter predicates. You also need SELECT privilege on V$SQL, V$SQL_PLAN, V$SESSION and V$SQL_PLAN_STATISTICS_ALL, or the SELECT_CATALOG_ROLE role, which covers them.
Why a second display function is needed
EXPLAIN PLAN asks the optimizer what it would do if it parsed the statement now. It does not run the statement. Bind variable values are not peeked, and the session settings or statistics may differ from those in effect when the statement really ran. The plan you see can therefore differ from the plan that was executed.
DBMS_XPLAN.DISPLAY_CURSOR reads the plan from the cursor cache, which is the shared pool area that holds parsed statements. That plan is the one Oracle used. If you ask for it, the function can also show row counts, buffer reads and times measured during execution.
| Function | Source of the plan | Runs the statement? | Shows actual numbers? |
|---|---|---|---|
| `DISPLAY` | `PLAN_TABLE`, filled by `EXPLAIN PLAN` | No | No |
| `DISPLAY_CURSOR` | Cursor cache (`V$SQL_PLAN`) | The statement must already have run | Yes, if statistics were collected |
| `DISPLAY_AWR` | AWR snapshots | The statement ran earlier | Limited to what AWR stored |
The simplest call: the last statement in your session
Run the statement, then call DISPLAY_CURSOR with no sql_id. It then reports the last statement your session executed.
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'));Two details matter here.
- The
GATHER_PLAN_STATISTICShint tells Oracle to time and count each row source while it runs. Without it, Oracle collects no per-operation statistics, andALLSTATShas nothing to show. - "Last statement" means the last statement of any kind in your session. In SQLPlus, run
SET SERVEROUTPUT OFFfirst. With it on, SQLPlus issues aDBMS_OUTPUTcall after each statement, and that call becomes the "last statement". You then get the plan of the wrong statement, or a message that the cursor was not found.
Reading the output
The output looks like the following. The numbers are illustrative, and yours will differ.
---------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
---------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 15 |00:00:00.01 | 12 |
| 1 | NESTED LOOPS | | 1 | 15 | 15 |00:00:00.01 | 12 |
| 2 | NESTED LOOPS | | 1 | 15 | 15 |00:00:00.01 | 8 |
|* 3 | TABLE ACCESS FULL | EMPLOYEES | 1 | 15 | 15 |00:00:00.01 | 6 |
|* 4 | INDEX UNIQUE SCAN | DEPT_ID_PK | 15 | 1 | 15 |00:00:00.01 | 2 |
| 5 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 15 | 1 | 15 |00:00:00.01 | 4 |
---------------------------------------------------------------------------------------------------------The columns that DISPLAY did not give you are these:
- Starts: how many times that operation began. For the inner side of a nested loop, Starts equals the number of rows the outer side produced. In the sample, step 4 starts 15 times, once per employee row.
- E-Rows: the optimizer's estimate of rows produced by one start of the operation.
- A-Rows: the actual rows produced, summed over all starts.
- A-Time: the actual elapsed time for the operation, including its children.
- Buffers: logical reads (buffer cache gets) performed by the operation, including its children.
The asterisks next to Ids 3 and 4 point to the Predicate Information section below the table, which you read exactly as in the previous chapters.
Why E-Rows and A-Rows are not directly comparable
E-Rows is a per-start figure, and A-Rows is a total. For an operation with Starts = 15, an E-Rows of 1 means the optimizer expects 15 rows in total, so compare A-Rows with E-Rows multiplied by Starts. Comparing the two columns as printed makes every inner nested-loop step look like a misestimate.
What the format string controls
ALLSTATS LAST is shorthand for two things combined.
ALLSTATSadds the runtime columns (IOSTATSandMEMSTATStogether). It reports values accumulated over all executions of the cursor.LASTrestricts those values to the most recent execution.
If you omit LAST, A-Rows and Buffers accumulate across every run of the cursor. After five executions, A-Rows is about five times the per-run figure, and that misleads you. Use ALLSTATS LAST when you want to see what one run did.
You can add modifiers in the same string, such as 'ALLSTATS LAST +PEEKED_BINDS' or 'ALLSTATS LAST -PROJECTION'. A + adds a section and a - removes one.
Collecting runtime statistics without a hint
You cannot always edit the statement, for example when it comes from an application. In that case, set the statistics level for your own session:
ALTER SESSION SET statistics_level = ALL;
SELECT e.last_name, d.department_name
FROM hr.employees e
JOIN hr.departments d ON d.department_id = e.department_id;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST'));STATISTICS_LEVEL = ALL makes every statement in the session collect row-source statistics. The extra timing calls add measurable overhead, and the timings themselves can be inflated for operations that run many times. Use the setting only while you diagnose, and reset it afterwards with ALTER SESSION SET statistics_level = TYPICAL;.
The hint and the session setting have the same limitation. The runtime figures describe a run with timing instrumentation switched on, so an operation that starts very many times, such as an inner nested-loop step, can look slower than it is in production.
Finding a cursor in another session
The no-argument call works only for your own last statement. To inspect a statement run by someone else, or one you ran earlier, find its sql_id and child number in V$SQL.
SELECT sql_id, child_number, sql_text
FROM v$sql
WHERE sql_text LIKE '%GATHER_PLAN_STATISTICS%'
AND sql_text NOT LIKE '%v$sql%';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(
sql_id => '&sql_id',
cursor_child_no => 0,
format => 'ALLSTATS LAST'));- The
sql_ididentifies the statement text. A statement may have several child cursors, each with its own plan, because of different bind types, session settings or adaptive behavior.cursor_child_noselects one of them. - If you leave
cursor_child_noout, Oracle shows every child of thatsql_id. This is a useful way to notice that one statement has two different plans. - The second condition in the first query keeps the lookup query from finding itself.
- For another session's statement,
ALLSTATS LASTshows runtime figures only if that statement was run with statistics collection enabled. If it was not, the runtime columns are missing and you get a plan with estimates only.
When the statistics columns are missing
If you run ALLSTATS LAST against a cursor that collected no row-source statistics, the plan still prints, but without the A-Rows, A-Time and Buffers columns. The output carries a note saying that statistics were not gathered, with a hint to use GATHER_PLAN_STATISTICS or STATISTICS_LEVEL = ALL. Treat that note as a prompt to re-run the statement with collection on, not as an error in the plan.
Where this fits in a diagnosis
Common mistakes
- Calling
DISPLAY_CURSORafter another statement. Any intervening statement changes what "last" means. Run the query and the display call back to back. - Using
ALLSTATSwithoutLAST. The numbers are cumulative over all executions. - Forgetting the hint, or typing it wrongly. A hint with a typo is silently ignored as an ordinary comment, and the statistics columns are then absent.
- Comparing E-Rows and A-Rows without Starts. This produces false misestimates on the inner side of nested loops.
- Expecting the plan to match
EXPLAIN PLAN. Bind peeking, adaptive plans and different session parameters can legitimately produce a different plan. That is the reason to useDISPLAY_CURSORhere.
Exercise
You run a query with the GATHERPLANSTATISTICS hint and call DBMSXPLAN.DISPLAYCURSOR(format => 'ALLSTATS LAST'). One row in the output shows Starts = 200, E-Rows = 1 and A-Rows = 200. Is this a cardinality misestimate? Explain.
Show answer
No. E-Rows is a per-start estimate, while A-Rows is the total across all starts. With 200 starts, the optimizer expected about 200 rows in total (1 x 200), and 200 were produced, so the estimate was accurate.
Exercise
A colleague says the query was slow at 02:00 in their session. You call DISPLAYCURSOR with the right sqlid and get no rows back. Give two reasons, and say what you would try next.
Show answer
The cursor may have aged out of the shared pool or been invalidated, for example by a statistics gather or DDL. The statement may also have run in an instance other than the one you are querying. Next, check whether the plan was captured by AWR with DBMSXPLAN.DISPLAYAWR (this needs the Diagnostics Pack license), or re-run the statement with GATHERPLANSTATISTICS and capture its plan again.