Sub topic 2
How a B-tree index is organized
This chapter assumes you can write basic SQL and create tables in Oracle. It also assumes you know a ROWID identifies a row's physical location, and that the optimizer picks an execution plan. By the end you will be able to describe the block structure of a B-tree index. You will also be able to explain why lookups stay fast as a table grows, and why some queries cannot use the index.
The shape of the tree
Oracle's default index type is the B-tree index. It stores a sorted copy of the indexed column values, each paired with the ROWID of the row it came from. The index lives in its own segment, made of the same database blocks (often 8 KB) that tables use. It has three kinds of blocks:
- Root block. There is exactly one. Every search starts here.
- Branch blocks. These sit between the root and the leaves. Each entry holds enough of a key value to decide which child block to follow, plus the address of that child.
- Leaf blocks. These hold the index entries themselves: the key value or values and the ROWID of the matching row.
The tree is balanced. Every leaf block is the same number of steps from the root, so every lookup does the same amount of work, whichever key you search for. Oracle keeps the tree balanced as rows are inserted and deleted. You never rebalance it by hand.
The horizontal links between leaf blocks matter. Leaf blocks form a doubly linked list in key order. After Oracle finds the first matching entry, it can read forward (or backward) along the leaves without going back up the tree. This is what makes range predicates such as order_date >= DATE '2026-01-01' and ORDER BY on the indexed column efficient.
What a leaf entry contains
A leaf entry has a small header, the key column values, and a ROWID. Within a leaf block, entries are kept in sorted order.
- In a unique index, the key values alone identify one row. The ROWID is stored as the pointer to the table row.
- In a non-unique index, Oracle treats the ROWID as part of the sort key. Duplicate key values are therefore stored next to each other, ordered by ROWID.
The table row is not in the index. After the index finds the ROWID, Oracle reads the table block to fetch columns that the index does not hold. If a query needs only indexed columns, Oracle can skip that step. The next chapters on access paths cover this.
How a search walks the tree
For WHERE order_id = 1001 on an indexed column, Oracle does the following:
- Read the root block. Compare 1001 with the entries and pick the child that covers it.
- Read the branch block (repeat once per branch level).
- Read the leaf block and look for key 1001.
- Take the ROWID from the entry and read the table block that holds the row.
The number of blocks read for steps 1 to 3 equals the height of the tree. Height grows very slowly. A branch block holds hundreds of child pointers, so a three- or four-level index can cover millions or hundreds of millions of rows. Doubling the table adds a level only rarely. This is why an index lookup stays close to constant cost while a full table scan grows with table size.
Looking at the height yourself
Oracle records the structure in optimizer statistics. This script builds the sample table from the spine, adds a composite index, loads rows, and then reads the index statistics.
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER NOT NULL,
status VARCHAR2(10) NOT NULL,
order_date DATE NOT NULL,
email VARCHAR2(100)
);
CREATE INDEX orders_cust_date_ix ON orders (customer_id, order_date);
INSERT INTO orders (order_id, customer_id, status, order_date, email)
SELECT level,
MOD(level, 5000),
CASE MOD(level, 3) WHEN 0 THEN 'OPEN' WHEN 1 THEN 'SHIPPED' ELSE 'CLOSED' END,
DATE '2026-01-01' + MOD(level, 300),
CASE WHEN MOD(level, 10) = 0 THEN NULL ELSE 'user' || level || '@example.com' END
FROM dual
CONNECT BY level <= 200000;
COMMIT;
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(USER, 'ORDERS', cascade => TRUE);
END;
/
SELECT index_name, blevel, leaf_blocks, distinct_keys, num_rows
FROM user_indexes
WHERE table_name = 'ORDERS';BLEVEL is the number of branch levels above the leaf level. A BLEVEL of 0 means the root block is also the only leaf. The height of the tree is BLEVEL + 1. With 200,000 rows you will probably see a BLEVEL of 1 or 2 for these indexes. Exact numbers depend on block size and the data. LEAF_BLOCKS shows how much space the leaf level takes, and DISTINCT_KEYS shows how many different key values the index holds.
The primary key created an index automatically. Its name is system-generated (SYS_C...), so it appears in this query with an unfamiliar name.
How the tree stays balanced: block splits
Leaf blocks fill up. When a new entry must go into a full leaf block, Oracle splits the block:
- It allocates a new leaf block.
- It moves part of the entries to the new block.
- It adds an entry for the new block to the parent branch block.
If the parent is full too, it splits as well, and the split can travel up to the root. A root split is the only way the tree gets taller, and it adds a level for every leaf at once. That keeps the tree balanced.
Oracle uses two split styles:
- 50/50 split. For an insert into the middle of the key range, entries are divided about evenly. Both blocks then have free space for later inserts.
- 90/10 split. When the new key is the highest in the index, as with an ever-increasing sequence or a timestamp, Oracle leaves the old block almost full and puts the new entry alone in the new block. This avoids half-empty leaves on sequential loads.
Splits are part of the cost of an index, and they happen inside your insert. The cost depends on the key pattern. Random keys, such as hashed values, scatter inserts across all leaf blocks and cause many 50/50 splits. Sequential keys concentrate inserts on the rightmost leaf block. That is cheap for space, but under heavy concurrent inserts many sessions compete for the same block.
Deletes and updates
When you delete a row, Oracle marks its index entry as deleted but leaves the entry in the leaf block. A later insert into that block can reuse the space. Blocks that become completely empty return to the free list. The tree does not shrink and leaf blocks are not merged on their own, so a table that has lost most of its rows can keep a larger index than it needs.
An update of an indexed column is a delete of the old entry plus an insert of a new one, possibly in a different leaf block. Updating a column that is not indexed does not touch the index at all. This is the main reason extra indexes slow down write-heavy tables. Each insert and delete changes every index on the table, and each update of an indexed column changes that index.
NULLs and single-column B-tree indexes
A B-tree index does not store an entry for a row where all the indexed columns are NULL. For a single-column index that has a consequence:
CREATE INDEX orders_email_ix ON orders (email);
SELECT order_id FROM orders WHERE email IS NULL;The IS NULL query cannot use orders_email_ix, because rows with a NULL email have no entries in it. Oracle must scan the table. In a composite index, a row is stored as long as at least one key column is non-NULL. Adding a column that is always non-NULL, such as a constant or a NOT NULL column, makes NULL searches on the other column possible.
What the structure means for queries
The sorted, linked leaf level explains most of the behavior you will see in execution plans:
- Equality on the full key goes straight down the tree to one entry (or a few, if non-unique).
- Range conditions find the first entry, then read along the leaf chain until the range ends.
- Sorted output on the indexed columns can come straight from the leaf order, so no separate sort is needed.
- Conditions on a non-leading part of a key have no sorted starting point in the tree. This is why column order in a composite index matters, which a later chapter covers.
- Functions applied to the indexed column, such as
UPPER(email), break the match between the stored values and the search value. That is the problem function-based indexes solve later in this tutorial.
Exercise
A table has 50 million rows and a B-tree index on customer_id with BLEVEL 3. How many index blocks does a unique lookup of one key read before it has the ROWID, and why does the number stay about the same if the table doubles in size?
Show answer
It reads 4 index blocks: the root, two branch blocks (BLEVEL 3 means three branch levels above the leaves, counting the root), and one leaf block. The height is BLEVEL + 1 = 4. Each branch block holds hundreds of child pointers, so doubling the rows adds entries to the leaf level and only rarely adds a level. The tree grows taller only when a root split occurs, so the lookup cost stays nearly constant.
Exercise
Why can SELECT order_id FROM orders WHERE email IS NULL not use a single-column B-tree index on email?
Show answer
A B-tree index does not store entries for rows where all indexed columns are NULL. Rows with a NULL email are therefore missing from the index, and Oracle must read the table to find them.