Sub topic 2
How a B-tree Index Is Organized
Assume you know basic SELECT and WHERE clauses, can create Oracle tables, and understand that a row has a ROWID. This chapter focuses on the internal organization of an Oracle B-tree index. It does not cover every index type or every optimizer rule.
The purpose of a B-tree index
A B-tree index stores column values in sorted order together with references to table rows. Oracle can use this order to find matching rows without examining every row in the table.
Suppose an orders table contains one million rows and you query:
SELECT order_id
FROM orders
WHERE customer_id = 42;Without a useful index, Oracle may read many table blocks in a full table scan. With an index on customer_id, Oracle can find the entries for 42, obtain their row references, and visit the matching table rows.
An index reduces some read work, but it has costs:
- It uses storage.
- Inserts must add entries in the correct location.
- Updates to indexed columns can change index entries.
- Deletes must remove entries.
- Extra work is needed to keep the index synchronized with the table.
Root, branch, and leaf blocks
A B-tree is a balanced tree stored in database blocks. “Balanced” means that every search reaches a leaf level through the same number of tree levels. The tree does not become a long, uneven chain as rows are added.
An Oracle B-tree index usually contains these block types:
- The root block is the starting point for a search.
- Branch blocks guide the search toward the correct range of values.
- Leaf blocks contain sorted index entries and row references.
Branch entries contain separator keys and pointers to lower-level blocks. Leaf entries contain the indexed key and a row reference, commonly a physical ROWID for a heap-organized table.
Leaf blocks are linked in key order. This lets Oracle continue through adjacent leaf entries for a range condition such as:
WHERE order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01'Oracle can locate the first matching key, then scan forward through the leaf block chain.
Creating a B-tree index
A normal CREATE INDEX statement creates a B-tree index unless another index type is specified.
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER NOT NULL,
order_date DATE NOT NULL
);
CREATE INDEX orders_customer_ix
ON orders (customer_id);The primary key creates a unique index automatically if Oracle needs one to enforce the constraint. A manually declared unique index also prevents duplicate indexed keys:
CREATE UNIQUE INDEX orders_order_id_uix
ON orders (order_id);The index stores keys, not complete copies of table rows. Oracle usually performs two steps: it finds an index entry, then uses its row reference to fetch columns from the table. If the query needs only indexed columns, Oracle may be able to answer it from the index alone.
| Index part | Main contents | Purpose |
|---|---|---|
| Root | Pointers and key boundaries | Starts the search |
| Branch | Pointers and separator keys | Chooses a lower-level block |
| Leaf | Sorted keys and row references | Finds matching table rows |
Important storage behavior
Oracle does not normally store an entry when every indexed column is NULL. A single-column B-tree index therefore cannot help find rows with WHERE column IS NULL. Composite indexes have more detailed null behavior because an entry can exist when at least one indexed column is non-null.
When many entries must fit into one block, Oracle splits a block and maintains the tree structure. These splits are normal maintenance operations, but frequent random inserts can increase index work and storage use.
Exercise
In a B-tree index, which blocks contain the indexed keys and row references, and what do branch blocks do?
Show answer
Leaf blocks contain the sorted keys and row references. Branch blocks contain separator information and pointers that guide a search toward the correct leaf block.