Sub topic 11
Actual Rows, Buffers, and Time with ALLSTATS LAST
Prerequisites
Assume you know basic SELECT statements, joins, indexes, and Oracle plan trees. You also need permission to use DBMS_XPLAN.DISPLAY_CURSOR. This chapter uses the HR sample schema.
An estimated plan predicts how Oracle will execute a statement. To investigate a slow statement, you also need the plan statistics collected while the statement runs. These statistics show what each operation actually did.
Collecting runtime statistics
Add the GATHER_PLAN_STATISTICS hint to the statement you want to inspect. The hint tells Oracle to collect row-source statistics during execution. Then call DBMS_XPLAN.DISPLAY_CURSOR with the ALLSTATS LAST format.
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'));DISPLAY_CURSOR reads the plan for a cursor in the shared pool. With no sql_id, it displays the last statement executed in the current session. Run the query immediately before the DBMS_XPLAN call. In SQL*Plus, turn server output off if necessary so another statement does not become the session's last cursor.
The output includes the normal plan tree and runtime columns:
| Column | Meaning |
|---|---|
| E-Rows | Estimated rows for the operation |
| A-Rows | Actual rows produced by the operation |
| Starts | Number of times the operation started |
| A-Time | Elapsed time spent in the operation |
| Buffers | Logical reads made by the operation |
Comparing estimates with actuals
The most useful comparison is E-Rows versus A-Rows. A large difference is called a cardinality misestimate. Cardinality means the number of rows Oracle expects an operation to produce.
For example, if an operation shows E-Rows of 10 but A-Rows of 10,000, Oracle underestimated the result by 1,000 times. That error can cause it to choose a nested loops join when a hash join would have been better.
A-Rows is cumulative across all starts. If Starts is 20 and A-Rows is 200, the operation produced about 10 rows per start. This matters for operations inside nested loops.
Buffers counts logical reads from Oracle's buffer cache. A high value often indicates that an operation examined many blocks. It is usually more useful for diagnosing database work than elapsed time alone, because elapsed time can change with system load.
A-Time is the actual elapsed time recorded for the operation. Time at a parent operation can include time spent in its child operations, so do not simply add every displayed time.
Reading the runtime tree
The plan still executes as a tree. Oracle usually starts with the deepest child operation and passes rows to its parent. Runtime statistics let you ask three questions at each important node:
- How many times did it start?
- How many rows did it produce compared with the estimate?
- How much work did it perform, measured by buffers and time?
A large A-Rows error near the bottom of the tree can affect every parent operation. Investigate the first operation where estimates diverge sharply. Check its predicates, column statistics, data distribution, and bind values before changing indexes or join methods.
Collecting statistics without a hint
For a diagnostic session, you can collect statistics for every statement:
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'));This setting increases overhead, so restore the normal session setting after testing if needed. The hint is usually safer when you want to inspect one statement.
Exercise
An operation has E-Rows = 5, A-Rows = 500, Starts = 20, and Buffers = 12,000. What should you investigate first?
Show answer
Investigate the cardinality estimate and the operation's predicates first. Oracle underestimated the rows by 100 times, and repeated starts may have multiplied its logical reads.