Sub topic 1
How a B-tree Index Is Searched
Prerequisites
This chapter assumes that you know basic SELECT, WHERE, JOIN, and ORDER BY syntax. You should also know common Oracle data types and have access to SQL*Plus, SQL Developer, or a similar client.
What a B-tree index stores
A B-tree index is a separate database structure that keeps indexed values in sorted order. Each entry usually contains:
- The indexed column value.
- A row locator, usually a rowid, that identifies the table row.
For example, this statement creates an index on order_date:
CREATE INDEX orders_order_date_ix
ON orders (order_date);The table remains stored in its table blocks. The index stores an ordered copy of order_date values and pointers to the matching table rows. Oracle can search the smaller, ordered index instead of examining every table row.
The term B-tree means balanced tree. The tree has levels:
- The root block is the starting point.
- Branch blocks direct the search toward the correct value range.
- Leaf blocks contain indexed values and row locators.
The leaf blocks are linked in order. This lets Oracle find one value quickly or read a continuous range of values efficiently.
Equality and range searches
For an equality condition, Oracle follows the tree to the matching leaf entry:
SELECT order_id, order_date
FROM orders
WHERE order_date = DATE '2026-01-15';For a range condition, Oracle finds the first matching leaf entry and then follows the linked leaf blocks until the range ends:
SELECT order_id, order_date
FROM orders
WHERE order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';This is called an index range scan. The word range does not require a broad range. Oracle can use the same access method for a narrow range or many matching values.
After finding index entries, Oracle often visits the table blocks identified by their rowids. This second step is called table access by rowid. If the query needs only columns available in the index, Oracle may avoid those table visits, but that design is considered separately from the basic search.
When Oracle chooses a full table scan
An index is not automatically faster. Oracle estimates the cost of different access paths using the expected number of matching rows, table size, block layout, and optimizer statistics.
An index is often useful when a predicate selects a small part of a table. If a query returns a large percentage of rows, reading the table sequentially can cost less than jumping from the index to many table blocks.
| Predicate | Typical index access |
|---|---|
| `order_id = 1001` | Index unique scan or range scan |
| `order_date >= DATE '2026-01-01'` | Index range scan |
| No selective predicate | Full table scan may be cheaper |
Exercise
Why can an index range scan be efficient for a date range?
Show answer
Oracle finds the first matching leaf entry through the tree, then reads adjacent ordered leaf entries until the range ends instead of searching every table row.