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

[ 40 | 80 ][ 10 | 25 ][ 50 | 65 ][ 90 | 120 ]1 5 810 17 2225 31 3840 44 4750 58 6165 70 7780 85 8890 99 110120 130
Searching for 58: compare at the root, descend to the middle child, land on the leaf. Leaves are linked left to right.

Why trees stay shallow

1 level500 entries2 levels250 thousand3 levels125 million4 levels62.5 billion
Entries reachable by tree height with a fan-out of 500. Bar length is logarithmic: each level multiplies capacity by 500.

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 patternInsert locationEffect
Auto-increment / BIGSERIALAlways the rightmost leafFew splits, compact pages, cache-friendly
Time-ordered UUIDv7 / ULIDNear the right edgeAlmost as good as auto-increment
Random UUIDv4AnywhereFrequent page splits, ~50-70% full pages, more I/O
Natural keys (email)AnywhereLike 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.

More Databases articles