Overview

Oracle can show the plan it would use (EXPLAIN PLAN) or the plan it actually used with real row counts and buffer gets (DBMS_XPLAN.DISPLAY_CURSOR). Always prefer the actual plan from the cursor cache; the estimated plan can differ because of bind peeking (oracle-optimizer-statistics).

Core concepts

How to get the actual plan:

SELECT /*+ gather_plan_statistics */ COUNT(*) FROM orders WHERE customer_id = 7;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL, NULL, 'ALLSTATS LAST +COST'));

Or ALTER SESSION SET statistics_level = ALL. Needs SELECT on V$SESSION, V$SQL, V$SQL_PLAN, V$SQL_PLAN_STATISTICS_ALL (via SELECT_CATALOG_ROLE).

Columns in the output:

ColumnMeaning
StartsHow many times this step ran
E-RowsOptimizer estimated rows per start
A-RowsActual rows (total)
A-TimeActual elapsed time
BuffersLogical reads (consistent gets); Reads physical
OMem/1Mem/Used-TmpMemory and temp usage of sorts and hashes

Reading rules: the plan is a tree, executed innermost and first-listed child first (the most indented line runs first). Compare E-Rows with A-Rows, then look at where Buffers accumulate. Predicate information distinguishes access (used to navigate an index) from filter (applied after reading rows).

Common operations: TABLE ACCESS FULL, INDEX RANGE SCAN, INDEX UNIQUE SCAN, INDEX FAST FULL SCAN, TABLE ACCESS BY INDEX ROWID BATCHED, NESTED LOOPS, HASH JOIN, SORT AGGREGATE, PARTITION RANGE ... (oracle-partitioning).

Worked example

SELECT COUNT(*) FROM orders WHERE customer_id = 7 on 500,000 rows (47 matching):

| Id | Operation          | Name   | Starts | E-Rows | Cost | A-Rows | A-Time   | Buffers |
|  0 | SELECT STATEMENT   |        |      1 |        | 2602 |      1 | 00:00.02 |    9443 |
|  1 |  SORT AGGREGATE    |        |      1 |      1 |      |      1 | 00:00.02 |    9443 |
|* 2 |   TABLE ACCESS FULL| ORDERS |      1 |     50 | 2602 |     47 | 00:00.02 |    9443 |

After CREATE INDEX orders_cust ON orders(customer_id) and new statistics, the same data with SUM(amount):

|  2 |  TABLE ACCESS BY INDEX ROWID BATCHED | ORDERS      | 1 | 50 |  53 | 47 | 00:00.01 | 50 |
|* 3 |   INDEX RANGE SCAN                   | ORDERS_CUST | 1 | 50 |   3 | 47 | 00:00.01 |  3 |

9,443 buffers fall to 50. Estimate 50 against actual 47 shows good statistics. Oracle’s hints (/*+ INDEX(o orders_cust) */) can force choices, but they freeze a decision; fix statistics and indexes first.

Pitfalls and production notes

  • EXPLAIN PLAN plus DBMS_XPLAN.DISPLAY does not peek binds or show actual rows. Use cursor-based plans, or DISPLAY_AWR for older ones (oracle-awr-ash).
  • Plans for a child cursor may differ; pass sql_id and child_number explicitly when several exist.
  • Do not judge by cost alone; use buffers and time.
  • Real-Time SQL Monitoring (DBMS_SQLTUNE.REPORT_SQL_MONITOR) is great for long statements but belongs to the Tuning Pack license in Enterprise Edition.
  • SET AUTOTRACE TRACEONLY STATISTICS in SQL*Plus is a quick way to see consistent gets.

Practice questions

  1. What does A-Rows differing 100x from E-Rows suggest?
  2. What is the difference between access and filter predicates?
  3. Why is EXPLAIN PLAN sometimes wrong for a statement that uses bind variables?

References

  • Oracle Database SQL Tuning Guide, Explaining and Displaying Execution Plans

oracle-optimizer-statistics · oracle-indexes · oracle-awr-ash · sqlserver-execution-plans · pg-explain-analyze