Sub topic 4
Index scans and full table scans
This chapter assumes you can write basic SQL (SELECT, WHERE, JOIN, ORDER BY), have created tables and indexes in Oracle, know what a ROWID is, and know that the optimizer picks an execution plan. It covers the access paths Oracle uses to read a table, how the optimizer chooses among them, and the errors that make a good index go unused.
What an access path is
An access path is the method Oracle uses to find the rows a query needs. For one table, the optimizer typically considers these:
- Full table scan: read every block of the table up to the high-water mark.
- Index unique scan: walk the index to find at most one entry, then fetch one row.
- Index range scan: walk the index to a starting entry, then follow the leaf blocks while entries still match.
- Index full scan: read every leaf entry of the index in key order.
- Index fast full scan: read all index blocks with multiblock reads, in no particular order.
- Index skip scan: probe an index even though its leading column is not in the predicate.
The optimizer estimates the cost of each candidate using table and index statistics, then picks the cheapest. A full table scan is not a failure. It is often the correct choice.
Full table scans
A full table scan reads the table's blocks sequentially. Oracle reads many blocks per I/O call, controlled by DB_FILE_MULTIBLOCK_READ_COUNT. The scan reads every block below the high-water mark, including blocks that are now empty after deletes. A table that once held millions of rows and now holds a few hundred can still be slow to scan.
A full scan is usually best when:
- The query has no WHERE clause, or the predicate matches a large share of the rows.
- The table is small. Reading a few blocks is cheaper than walking an index and then fetching blocks.
- No usable index exists for the predicate.
- Statistics suggest an index would do more I/O than a scan.
Index range scans
An index range scan is the common path for a non-unique index, or for a unique index when the predicate is a range such as >= or BETWEEN. Oracle descends from the root block through branch blocks to the first matching leaf entry. It then reads along the linked leaf blocks until entries no longer match. Each matching entry holds a ROWID, and Oracle uses it to fetch the row from the table.
That last step is the hidden cost. Each ROWID lookup is a single-block read into the table, and the matching rows may be scattered across many blocks. The optimizer measures this scatter with the index's clustering factor. A low clustering factor (close to the number of table blocks) means rows with adjacent keys sit in adjacent blocks, so a range scan is cheap. A high one (close to the number of rows) means each ROWID probably points to a different block, so a range scan costs about one block read per row.
Index unique scans
A unique scan applies when an equality predicate covers every column of a unique or primary key index. Oracle finds at most one entry and does one table lookup. In the sample table, this query uses the primary key index:
SELECT status
FROM orders
WHERE order_id = 1001;The plan shows INDEX UNIQUE SCAN followed by TABLE ACCESS BY INDEX ROWID.
Covering indexes: no table access
If the index holds every column the query needs, Oracle can answer from the index alone and skip the table. The composite index on (customer_id, order_date) can cover this query:
SELECT customer_id, order_date
FROM orders
WHERE customer_id = 42;The plan shows only an index access step, with no TABLE ACCESS BY INDEX ROWID. Compare this with SELECT order_id ...: the primary key index holds order_id, but not customer_id, so the two indexes are different structures. Oracle does not combine them into one covering structure. It uses whichever one fits the predicate.
Full and fast full index scans
An index full scan reads the leaf blocks in key order using single-block reads. Oracle can use it to avoid a sort when the query has an ORDER BY on the index's leading columns.
An index fast full scan reads all index blocks in physical order with multiblock reads, as a full table scan does. It cannot return rows in key order. The optimizer considers it when the index holds all needed columns and the index is much smaller than the table. One rule matters here: B-tree indexes do not store entries where every indexed column is NULL. If a query could return such rows, Oracle cannot answer it from the index alone, unless a column in the index is declared NOT NULL or the predicate excludes NULLs.
Index skip scans
If a predicate filters only the second column of a composite index, Oracle may still use the index by treating each distinct value of the leading column as its own small index. This is a skip scan. It works well only when the leading column has few distinct values. With many distinct leading values, the optimizer usually prefers a full scan. The next chapters return to column order in composite indexes.
How the optimizer chooses
The decision depends on estimated cost, and the main inputs are:
- Selectivity: the fraction of rows the predicate returns. Low fractions favor an index.
- Clustering factor: how scattered those rows are across table blocks.
- Table and index size from statistics.
- Multiblock read cost compared with single-block read cost.
There is no fixed percentage at which Oracle switches from index to scan. A query returning 1% of rows from a badly clustered index can be better served by a full scan, and 20% from a well-clustered one can be better served by the index.
Seeing the choice
Use the plan from EXPLAIN PLAN to check the access path. Chapter 6 covers reading it in detail, but the key operations are these:
| Plan operation | Meaning |
|---|---|
| `TABLE ACCESS FULL` | Full table scan |
| `INDEX UNIQUE SCAN` | Unique scan, at most one entry |
| `INDEX RANGE SCAN` | Range scan over leaf entries |
| `INDEX FULL SCAN` | All leaf entries, in key order |
| `INDEX FAST FULL SCAN` | All index blocks, unordered |
| `INDEX SKIP SCAN` | Probes per distinct leading value |
| `TABLE ACCESS BY INDEX ROWID` | Row fetch using a ROWID from an index |
Run this against the sample table:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);With current statistics and a selective predicate, you should see INDEX RANGE SCAN on ORDERS_CUST_DATE_IX, then TABLE ACCESS BY INDEX ROWID. If the table is tiny, or no statistics exist yet, the plan may show TABLE ACCESS FULL instead. That is the optimizer making a cost decision, not an error.
Why an index goes unused
When a plan shows a full scan and you expected an index, check these causes first:
- A function wraps the indexed column.
WHERE TRUNC(order_date) = DATE '2026-01-01'cannot use a plain index onorder_date, because the index stores raw values, notTRUNCresults. Rewrite the condition as a range (order_date >= DATE '2026-01-01' AND order_date < DATE '2026-01-02') or create a function-based index, covered later. - A implicit type conversion. If
emailwere compared to a number, or aVARCHAR2column to a number literal, Oracle converts the column side, which hides it from the index. - A leading wildcard.
LIKE '%abc'has no starting point to descend to.LIKE 'abc%'can use a range scan. - A missing leading column. A composite index on
(customer_id, order_date)serves a predicate oncustomer_idalone well, but a predicate onorder_datealone gets at best a skip scan. - Nullable columns with
IS NULL. Entries with all-NULL keys are not in a B-tree index, soWHERE email IS NULLcannot use an index onemailalone. - Stale or missing statistics. The optimizer's estimates may be far from reality.
- Low selectivity. The predicate matches so many rows that the scan really is cheaper.
Edge cases
- Bind variables with skewed data. The same SQL can need an index for a rare value and a scan for a common one. Oracle can use adaptive cursor sharing to produce different plans for different bind values.
ORDER BYandFIRST_ROWS. An index range scan on the sort column can return the first rows quickly without sorting. The optimizer weighs this when the query asks for only the first few rows.- Large range scans with poor clustering. A full scan with multiblock reads can beat millions of single-block reads, even at a modest percentage of rows.
- Small tables. Oracle may scan a small table even when an index exists, because both read few blocks.
Exercise
A query SELECT order_id FROM orders WHERE order_date >= DATE '2026-01-01' runs against a table with the index (customer_id, order_date), and the plan shows TABLE ACCESS FULL. Give two reasons the optimizer might not use that index, and one change that could help.
Show answer
The predicate omits the leading column customer_id, so the optimizer can use at most a skip scan, which pays off only when customer_id has few distinct values. Also, if the date range matches a large share of the rows, a full scan with multiblock reads can be cheaper than many ROWID lookups. A change that could help is a separate index on order_date, or reordering the index if most queries filter on order_date first. Check the plan again after the change.
Exercise
What is the clustering factor, and why can a high value make an index range scan expensive?
Show answer
The clustering factor estimates how many table block reads are needed to fetch all rows in index order. A value near the number of table blocks means adjacent keys are in the same blocks. A value near the number of rows means each ROWID probably points to a different block, so a range scan costs about one single-block read per row. The optimizer may then choose a full table scan instead.