Sub topic 4
Index Scans and Full Table Scans
Prerequisites
This chapter assumes that you know basic SELECT and WHERE clauses, have created Oracle tables, and understand that the optimizer chooses how to run a SQL statement. An index is a separate database structure that stores column values and references to table rows. A ROWID is Oracle’s address for a physical table row.
Two main access paths
An access path is the method Oracle uses to find rows.
An index scan starts with an index. Oracle searches the index for matching key values, obtains the matching ROWID values, and then fetches the table rows when the query needs columns that are not stored in the index.
A full table scan reads the table’s blocks and checks rows against the query conditions. A full scan can sound inefficient, but it may be the best choice when the query needs a large part of the table. Reading the table sequentially can cost less than visiting many scattered rows through an index.
| Access path | Often fits | Main cost |
|---|---|---|
| Index range scan | A selective range or equality condition | Index lookups and table row fetches |
| Full table scan | A large result set or small table | Reading many table blocks |
Selectivity describes how small the matching result is compared with the whole table. A condition matching ten rows out of one million is highly selective. A condition matching half the table is not selective.
The optimizer estimates selectivity from statistics. It considers the estimated number of matching rows, the cost of reading table blocks, available indexes, and other query details. The optimizer does not choose an index merely because one exists.
Seeing the access path
Use EXPLAIN PLAN to ask Oracle to store an estimated plan. Then display the plan with DBMS_XPLAN.DISPLAY.
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);Look at the Operation column. Common operations include:
INDEX RANGE SCAN: Oracle finds a range of values in a B-tree index.INDEX UNIQUE SCAN: Oracle finds at most one index entry, usually through a unique key.TABLE ACCESS BY INDEX ROWID: Oracle uses row addresses from an index to fetch table rows.TABLE ACCESS FULL: Oracle reads the table directly.
A plan might contain this sequence:
TABLE ACCESS BY INDEX ROWID ORDERS
INDEX RANGE SCAN ORDERS_CUST_DATE_IXThe indentation shows the relationship between operations. Oracle first scans the index, then uses the matching row addresses to access ORDERS.
A full scan might appear as:
TABLE ACCESS FULL ORDERSEXPLAIN PLAN shows an estimate, not proof of what happened during a particular execution. Estimates can be wrong when statistics are stale, data is unevenly distributed, or the statement uses values that are difficult to estimate. For important performance work, compare the plan with runtime statistics from the actual execution.
A simple decision model
For a small table, a full scan may be cheaper because reading the entire table takes little time. For a large table with a highly selective predicate, an index can avoid reading most table blocks.
The query must also use an indexed expression in a useful way. For example, a condition such as customer_id = 42 can use an index beginning with customer_id. A function applied to a column may require a function-based index, which is covered in a later chapter.
Exercise
Why might Oracle choose TABLE ACCESS FULL even when a suitable index exists?
Show answer
The optimizer may estimate that many rows match, making sequentially reading the table cheaper than performing many index lookups and table row fetches.