Sub topic 1
How a B-tree index is searched
Prerequisites
This chapter assumes you can:
- Write
SELECT,WHERE,JOINandORDER BYstatements. - Recognize Oracle data types such as
NUMBER,VARCHAR2andDATE. - Run statements in SQL*Plus, SQL Developer or a similar client.
- Explain in general terms what a database index is.
Later chapters build on this one. They cover predicates that block an index, composite indexes and execution plans. Here you learn the structure being searched, so those rules make sense later.
What a B-tree index contains
Oracle's default index type is a B-tree index. Each entry holds the indexed column values and the ROWID of the matching table row. A ROWID is the physical address of a row: which data file, block and row slot it lives in.
The entries are kept sorted by the indexed values, and they are stored in three kinds of blocks:
- Root block. The single entry point. It holds ranges of key values and pointers to the next level.
- Branch blocks. Intermediate levels that narrow the range further. Small indexes may have none.
- Leaf blocks. The bottom level. They hold the sorted entries with their
ROWIDs. Each leaf block is linked to the previous and next leaf block.
The tree is balanced. Every leaf is the same number of levels below the root, so a lookup always reads the same number of index blocks. Even for millions of rows, that depth is usually only three or four levels.
The two phases of a search
A search for an exact value starts at the root. Oracle compares the value with the key ranges in each block and follows one pointer down per level until it reaches a leaf block. That descent costs one block read per level.
If the predicate matches a range of values, such as BETWEEN or >=, Oracle then follows the leaf-to-leaf links. It reads entries in order until it passes the end of the range. It does not return to the root.
For each matching entry, Oracle usually also visits the table block that the ROWID points to. This step is the main cost of an index search. The index finds rows quickly, but fetching each row is a separate block access, and that cost grows with the number of matching rows.
Access paths you will see
Oracle names each way of reading an index in the execution plan. These are the ones you meet most often:
| Plan operation | What it does | Typical trigger |
|---|---|---|
| `INDEX UNIQUE SCAN` | Descends the tree and returns at most one entry | Equality on all columns of a unique or primary key index |
| `INDEX RANGE SCAN` | Descends once, then reads consecutive leaf entries | Equality on a non-unique index, or a range such as `>=` and `<` |
| `INDEX FULL SCAN` | Reads every leaf entry in sorted order | A query that needs the index order or can use only the index |
| `INDEX FAST FULL SCAN` | Reads all index blocks in disk order, with multiblock reads and no ordering | A query that needs only indexed columns and no sort |
| `TABLE ACCESS BY INDEX ROWID` | Fetches the table row for each `ROWID` from the index | Any index search that needs non-indexed columns |
| `TABLE ACCESS FULL` | Reads every block of the table | No usable index, or the optimizer expects to read most rows |
The word "scan" in INDEX RANGE SCAN can mislead you. Only a small slice of the leaf level is read. Slow queries are usually caused by a range that is wider than you thought, by many table block fetches, or by both.
Seeing it on a real table
The following script builds a table with 100,000 rows, adds an index and gathers statistics. Statistics tell the optimizer how many rows exist and how values are distributed.
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER NOT NULL,
order_date DATE NOT NULL,
status VARCHAR2(10) NOT NULL
);
INSERT INTO orders
SELECT level,
MOD(level, 1000) + 1,
DATE '2026-01-01' + MOD(level, 365),
'SHIPPED'
FROM dual
CONNECT BY level <= 100000;
COMMIT;
CREATE INDEX orders_cust_ix ON orders (customer_id);
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(USER, 'ORDERS');
END;
/
EXPLAIN PLAN FOR
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Each customer owns about 100 of the 100,000 rows, or 0.1%. The plan normally has this shape. Exact operation names and costs vary by Oracle version.
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| ORDERS |
|* 2 | INDEX RANGE SCAN | ORDERS_CUST_IX |
Predicate Information:
2 - access("CUSTOMER_ID"=42)Read the plan from the innermost, most indented step outward. Step 2 finds the leaf entries for customer 42. Step 1 uses their ROWIDs to fetch the rows. The access line shows that the condition was used to navigate the tree. Chapter 2 covers plan reading in detail.
Why the optimizer sometimes prefers a full table scan
An index is not always faster. Oracle's cost-based optimizer estimates the cost of each access path and picks the cheapest. Two things matter most.
Selectivity. This is the fraction of rows a predicate returns. A predicate for one customer out of 1,000 is highly selective. A predicate on status = 'SHIPPED' returns every row, so it is not selective at all. If Oracle expects to read a large share of the table, a full scan wins. A full scan uses large multiblock reads, while an index search fetches table blocks one row at a time.
Clustering factor. This is a number stored with the index statistics. It measures how closely the order of the rows in the table matches the order of the index. If rows with neighboring keys sit in the same table blocks, the clustering factor is low, and one table block read serves many index entries. If they are scattered, nearly every ROWID points to a different block, and the index becomes expensive even for a modest number of rows.
There is no fixed percentage at which Oracle switches. The break-even point depends on block size, clustering and the estimated row count, so do not rely on a rule such as "indexes stop helping above 5%".
Common mistakes
- Assuming an index is always used if it exists. The optimizer chooses by cost. A low-selectivity predicate or stale statistics can lead to a full scan, and often that choice is correct.
- Forcing an index with hints before checking the plan. A hint hides the underlying problem, such as a predicate that cannot use the index. It also leaves a query that is slow once the data changes.
- Reading
INDEX RANGE SCANas proof of a fast query. A range scan over millions of entries, followed by millions of table fetches, can be slower than a full scan. - Judging an index by one test with a handful of rows. With tiny tables, a full scan is cheap and the optimizer rightly picks it. Test with realistic data volumes and current statistics.
Key points
- A B-tree index is a balanced, sorted structure of root, branch and leaf blocks, with
ROWIDs in the leaves. - Exact lookups descend the tree once. Range lookups descend once and then follow the leaf links.
- Fetching table rows by
ROWIDis usually the larger part of an index search's cost. - The optimizer chooses between an index and a full scan using selectivity, clustering factor and statistics.
Exercise
A table has 1,000,000 rows and a non-unique index on status, where 90% of the rows have status = 'SHIPPED'. For WHERE status = 'SHIPPED', why might Oracle choose TABLE ACCESS FULL over INDEX RANGE SCAN, and why is that choice not a bug?
Show answer
The predicate is not selective: it matches about 900,000 rows. An index search would read many leaf entries and then fetch the table block for each ROWID, often one block at a time. A full scan reads every table block once using efficient multiblock reads. For this predicate the full scan has the lower estimated cost, so the optimizer is right to choose it.
Exercise
In the plan for WHERE customer_id = 42, which step performs the descent from the root block to the leaf block, and which step reads the table?
Show answer
INDEX RANGE SCAN on ORDERS_CUST_IX descends the tree and reads the leaf entries for customer 42. TABLE ACCESS BY INDEX ROWID then uses the returned ROWIDs to read the matching rows from the table.