Sub topic 11
Actual rows, buffers and time with ALLSTATS LAST
This chapter assumes you can already generate a plan with EXPLAIN PLAN and DBMS_XPLAN.DISPLAY, read the operation tree, and run DBMS_XPLAN.DISPLAY_CURSOR. It also assumes you can select from V$SQL and V$SESSION. The goal here is to read the runtime columns that ALLSTATS LAST adds, and to use them to tell what the statement actually did from what the optimizer expected.
Why estimates are not enough
An estimated plan shows what the optimizer expects. Its row counts come from statistics, and a wrong count can lead to a wrong join method or access path. A plan that looks reasonable on paper can still be slow because a filter returned 400,000 rows where the optimizer expected 40.
ALLSTATS LAST prints the estimates next to measurements taken while the statement ran. The format has two parts:
ALLSTATSis shorthand forIOSTATS MEMSTATS. It adds the I/O and memory counters for each plan line.LASTlimits those counters to the most recent execution of the cursor. Without it, the counters are cumulative across all executions of the same child cursor, which makes a single test run hard to read.
Collecting the statistics
Oracle collects row-source statistics only when asked. There are two common ways to ask for them.
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 hint affects one statement. Alternatively, ALTER SESSION SET statistics_level = ALL; turns collection on for every statement in the session. That is convenient while diagnosing, but it adds overhead, so reset it afterwards.
Two practical points apply:
- Call
DISPLAY_CURSORimmediately after the query, in the same session, with nosql_id. In SQL*Plus, runSET SERVEROUTPUT OFFfirst. Otherwise the last statement is a hiddenDBMS_OUTPUTcall and you see the wrong plan. - If the statement ran without statistics collection, the runtime columns are missing or empty, and a warning may appear. Re-run the statement with the hint.
The runtime columns
A typical result looks like this. The numbers are illustrative, and your values will differ.
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 15 |00:00:00.01 | 14 |
|* 1 | HASH JOIN | | 1 | 15 | 15 |00:00:00.01 | 14 |
| 2 | TABLE ACCESS FULL| DEPARTMENTS | 1 | 27 | 27 |00:00:00.01 | 7 |
|* 3 | TABLE ACCESS FULL| EMPLOYEES | 1 | 15 | 15 |00:00:00.01 | 7 |
-----------------------------------------------------------------------------------------------The columns mean the following.
| Column | Meaning | Scope |
|---|---|---|
| Starts | How many times this operation began executing | Count for the operation |
| E-Rows | Rows the optimizer expected for one start | Per start |
| A-Rows | Rows the operation actually returned | Total over all starts |
| A-Time | Elapsed time, formatted as hh:mm:ss.ff | Cumulative, includes child operations |
| Buffers | Logical reads (consistent and current gets) in the buffer cache | Cumulative, includes child operations |
| Reads | Physical block reads | Cumulative, includes child operations |
| Writes | Physical block writes, for example spills to temporary space | Cumulative, includes child operations |
| OMem, 1Mem, Used-Mem | Memory needed for an optimal pass, a one-pass operation, and the amount used | Work areas such as hash joins and sorts |
The Reads and Writes columns appear only when the counts are not zero. Used-Mem followed by a figure in parentheses, such as (1), shows how many passes the work area needed. A Writes value on a hash join or sort means that work area spilled to temporary space.
Comparing E-Rows with A-Rows correctly
E-Rows is a per-start figure and A-Rows is a total. Compare them with this in mind:
E-Rows x Starts versus A-RowsWhen Starts is 1, as in the plan above, the two columns compare directly. When an operation is the inner side of a nested loops join, Starts equals the number of rows the outer side produced. Here is a second example that forces that shape.
SELECT /*+ GATHER_PLAN_STATISTICS LEADING(e d) USE_NL(d) */
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 relevant part of the output, again illustrative:
| Id | Operation | Name | Starts | E-Rows | A-Rows | Buffers |
| 1 | NESTED LOOPS | | 1 | 15 | 15 | 32 |
|* 2 | TABLE ACCESS FULL | EMPLOYEES | 1 | 15 | 15 | 7 |
| 3 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 15 | 1 | 15 | 25 |
|* 4 | INDEX UNIQUE SCAN | DEPT_ID_PK | 15 | 1 | 15 | 10 |Line 4 shows Starts of 15, E-Rows of 1, and A-Rows of 15. The expectation of 1 row per start multiplied by 15 starts is 15, so the estimate was correct. Reading E-Rows 1 against A-Rows 15 without looking at Starts would make a healthy plan look wrong.
Reading Buffers and A-Time without double counting
Buffers and A-Time include the work of child operations. To find what a single line cost, subtract the values of its children from its own. In the hash join plan above, line 1 reports 14 buffers, and its children report 7 and 7. The join itself did no additional buffer reads. A parent line with a large Buffers value only tells you the cost is somewhere beneath it.
Consider these points when you interpret the figures:
Buffersis a count of logical reads, so it is stable across runs and is a good metric for comparing two versions of a query. Elapsed time varies with caching and load.- A large
Readsfigure on the first run that disappears on the second run points to a cold cache and not to a plan problem. - With
statistics_level = ALLor the hint, timing instrumentation adds overhead on operations that start very often. A line with millions of starts may show an inflatedA-Time. Judge such lines mostly byBuffersandStarts. - A very high
Startscount on an inner operation is itself a finding. It means the join drives a large number of lookups.
The reading order
The diagram shows how to work through the columns once you have an ALLSTATS LAST plan.
Where the first divergence tells you the cause
Estimates compound as they move up the tree, so one early error produces wrong numbers above it. For that reason, find the divergence lowest in the plan and start there. A few common causes:
- Stale or missing statistics on a table, so a filter estimate is off.
- Correlated columns. The optimizer multiplies the selectivities of
city = 'X'andcountry = 'Y'as if they were independent. - Skewed data without a histogram, so every value is assumed to have the same row count.
- Predicates on expressions or functions, which hide the column from its statistics.
The next chapter covers these cases in detail. Here you only need to locate the first line where the numbers separate.
Gotchas
- The
LASTkeyword works only if the cursor was run with statistics collection. If another session executed the same cursor last,LASTshows that run. Test in your own session where you can. - If the cursor was invalidated or aged out of the shared pool,
DISPLAY_CURSORfinds nothing. - Values in
A-Rowsfor a query that stops early, such as one fetched withFETCH FIRST, may be lower than the full row count. A client that fetches only part of the result set also stops the plan early. - Cumulative figures without
LASTmix runs with different bind values. Always check the format string before drawing conclusions.
Exercise
A plan line for an index lookup on the inner side of a nested loops join shows Starts = 2000, E-Rows = 1, A-Rows = 2000. Is the optimizer's estimate for this line wrong? What would you check next, and which columns would you use?
Show answer
No. The estimate is per start, so the expected total is E-Rows x Starts = 1 x 2000 = 2000, which matches A-Rows. The estimate is accurate for this line. The next question is whether 2000 lookups is acceptable. Check Buffers on the line and its parent, minus the child values, to see the cost of those 2000 starts, and check the outer side's A-Rows to confirm it really produced 2000 rows. If the cost is high, consider whether a hash join would suit that volume better.
Exercise
You run a query with the GATHERPLANSTATISTICS hint, then call DBMSXPLAN.DISPLAYCURSOR(format => 'ALLSTATS LAST') and see the plan of a DBMS_OUTPUT call instead of your query. What caused it and how do you fix it?
Show answer
In SQL*Plus with SERVEROUTPUT ON, a hidden DBMSOUTPUT call runs after each statement, so the most recent statement in the session is that call. Run SET SERVEROUTPUT OFF before the query, or pass the sqlid and cursorchildno found in V$SQL.