B-Tree Indexes: How Databases Actually Store and Search Indexes
How B-tree and B+tree indexes work inside MySQL InnoDB and PostgreSQL: pages, fan-out, tree height, range scans, page splits and why lookups stay fast.
Published November 27, 2026
Almost every default index in MySQL, PostgreSQL, SQL Server, Oracle and SQLite is a B-tree (more precisely a B+tree). Its design is the reason a lookup in a billion-row table takes only a few disk reads. Understanding its shape explains index sizes, why random UUID primary keys hurt, and why range queries are cheap.
The concept
A B+tree is made of fixed-size pages (16 KB in InnoDB, 8 KB in PostgreSQL). Inner pages hold sorted keys and pointers to child pages; leaf pages hold the actual index entries. All leaves are at the same depth, and each leaf links to its neighbours, forming a sorted linked list across the whole index.
The key property is fan-out. Because each inner page holds hundreds of keys, the tree is extremely wide and shallow. With a fan-out of about 500, three levels address 125 million entries and four levels about 60 billion. The root and inner pages are small enough to stay cached in memory, so a lookup often costs just one or two actual disk reads.
Range queries use the leaf links: seek to the first matching key, then walk right along the leaves until the range ends, without going back up the tree. Inserts go into the correct leaf; when a leaf is full it splits in two, and the split may propagate upward. That is why always-increasing keys (auto-increment, time-ordered IDs) insert efficiently at the right edge while random keys cause splits all over the tree.
Anatomy of a B+tree
Why trees stay shallow
Clustered vs secondary B-trees
In MySQL InnoDB the table itself is a B+tree keyed by the primary key, with full rows in the leaves (a clustered index). Secondary indexes store the indexed columns plus the primary key, so a lookup through a secondary index finds the primary key and then does a second B-tree descent into the clustered index. PostgreSQL stores rows in an unordered heap; every index, including the primary key, stores a pointer (ctid) to the row's physical location.
Why key choice matters for inserts
| Key pattern | Insert location | Effect |
|---|---|---|
| Auto-increment / BIGSERIAL | Always the rightmost leaf | Few splits, compact pages, cache-friendly |
| Time-ordered UUIDv7 / ULID | Near the right edge | Almost as good as auto-increment |
| Random UUIDv4 | Anywhere | Frequent page splits, ~50-70% full pages, more I/O |
| Natural keys (email) | Anywhere | Like random; fine for secondary indexes |
Practical examples
See index size and height in PostgreSQL
The pageinspect and pgstattuple extensions expose B-tree internals.
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT pg_size_pretty(pg_relation_size('idx_users_email'));
SELECT tree_level, avg_leaf_density FROM pgstatindex('idx_users_email');InnoDB index statistics
Approximate size of each index on a table.
SELECT index_name, ROUND(stat_value * @@innodb_page_size / 1024 / 1024, 1) AS size_mb
FROM mysql.innodb_index_stats
WHERE database_name = 'app' AND table_name = 'users' AND stat_name = 'size';Prefer time-ordered UUIDs for primary keys
UUIDv7 keeps uniqueness without random insert locations.
-- PostgreSQL 18 has uuidv7(); earlier versions can use an extension or generate in the app
CREATE TABLE events (id uuid PRIMARY KEY DEFAULT uuidv7(), payload jsonb);Common mistakes
- Using random UUIDv4 as the clustered primary key on write-heavy InnoDB tables.
- Assuming a binary tree: B-trees are wide and shallow, so lookups are a few page reads, not log2(n).
- Forgetting that very wide index keys (long VARCHARs) reduce fan-out and increase height.
- Ignoring bloat: heavy updates and deletes leave half-empty pages that need REINDEX or OPTIMIZE.
Key takeaways
- B+trees store sorted keys in pages; leaves are linked for range scans.
- High fan-out keeps trees three or four levels deep even for billions of rows.
- InnoDB clusters rows by primary key; PostgreSQL indexes point into a heap.
- Sequential or time-ordered keys insert efficiently; random keys cause page splits.
FAQ
What is the difference between a B-tree and a B+tree?
In a B+tree all data entries live in the leaves and leaves are linked; inner nodes only route. Databases use B+trees because range scans and dense inner pages are better for disks.
Why do databases not use hash indexes by default?
Hash indexes only support equality. B-trees support equality, ranges, prefix matches and ordering, so they are the better general default.
How often should I rebuild indexes?
Rarely. Rebuild when monitoring shows significant bloat after mass deletes or updates. PostgreSQL REINDEX CONCURRENTLY does it without blocking writes.