Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Why do relational databases use B+ trees for indexes instead of binary search trees or hash tables? Estimate the height of a B+ tree index on 1 billion rows with 8 KB pages, and explain why range queries are efficient.

Databases use B+ trees because each node is a whole page holding hundreds of keys, so the tree is very shallow and every level costs one page read. With a fanout of 300 to 500, a billion keys fit in about four levels, and since the top levels stay in memory a lookup needs one or two disk reads at most. All entries sit in leaf pages linked in key order, so a range query finds its first key and then reads neighbouring leaves; binary trees are far deeper and hash indexes cannot do ranges or ordering.

Why this question is asked

Indexes are the main tool we have for making queries fast, and almost every index in PostgreSQL and MySQL is a B+ tree. Knowing its shape lets us estimate costs in our head: how many reads a lookup takes, why range queries and ORDER BY can use an index, and why some inserts are more expensive than others. Interviewers like the height estimate because it shows whether we understand fanout. In this answer, we will recap the structure of a B+ tree, work out the height for a billion rows, see why range scans are cheap, compare it with binary trees and hash indexes, and look at what page splits cost.

1A Quick Recap of the B+ Tree

A B+ tree is a balanced search tree whose nodes are pages. Inner pages hold only separator keys and pointers to child pages: everything smaller than the first separator is in the first child, and so on. Leaf pages hold the actual index entries, each a key with a pointer to the row (in PostgreSQL a tuple id, the row's page and slot in the heap) or, for InnoDB's primary key, the row itself. Every leaf is at the same depth, which is what balanced means, and the leaves are linked to their neighbours in key order.

Because a node is a full page, 8 KB in PostgreSQL or 16 KB in InnoDB, it can hold a lot of keys. The number of children per inner page is called the fanout, and it is the number that decides everything else.

2Estimating the Height for One Billion Rows

Let us estimate the fanout for a bigint key in an 8 KB page. Each inner entry holds the 8-byte key plus a child pointer and some per-entry overhead, about 16 to 24 bytes in total. 8192 bytes divided by about 20 gives roughly 400 entries. Real pages are not completely full, so a fanout of 300 to 500 is a good working range.

Java
levels keys reachable with fanout 400
1 400 (the root)
2 400^2 = 160,000
3 400^3 = 64,000,000
4 400^4 = 25,600,000,000 -> 1,000,000,000 keys fit in 4 levels
with fanout 300: 300^3 = 27 million (too few), 300^4 = 8.1 billion -> still 4 levels
binary tree: log2(1,000,000,000) = about 30 levels
Step throughA four-level B+ tree on one billion keys
  1. Root page1 page, about 400 separators, always cached
  2. Level 2about 400 pages (3 MB), always cached
  3. Level 3about 160,000 pages (1.3 GB), usually cached
  4. Leaf levelmillions of pages, linked in key order, holds every entry

4 layers, from the top down. Step through them one at a time.

Each level is one page read; fanout keeps the tree shallow.

So a lookup in a billion-row index reads four pages: root, two inner levels and a leaf. Now notice how much memory the upper levels need. The root is one page, level two is about 400 pages, which is 3.2 MB, and level three about 160,000 pages, roughly 1.3 GB. The first two levels are always cached, and on a server with a reasonable buffer pool the third usually is too. A cold lookup therefore costs one or two real disk reads: the leaf, and perhaps the page above it, plus the data page if the row lives elsewhere.

3Why Range Queries Are Efficient

Consider WHERE created_at BETWEEN '2026-03-01' AND '2026-03-07'. The database descends the tree once to find the first leaf entry for 1 March, which costs the usual four page reads. From there it does not go back up. It reads entries in order along the leaf, then follows the link to the next leaf, and continues until it passes 7 March.

Because the leaves are in key order, a range of 50,000 entries might occupy only about 150 consecutive leaf pages, and each page read delivers hundreds of matching keys. The same property lets ORDER BY created_at LIMIT 20 stop after reading the first leaf, and lets the database produce sorted results without a separate sort step.

SQL
EXPLAIN SELECT id FROM orders WHERE created_at BETWEEN '2026-03-01' AND '2026-03-07' ORDER BY created_at;
-- Index Only Scan using orders_created_at_idx on orders
-- Index Cond: ((created_at >= '2026-03-01') AND (created_at <= '2026-03-07'))
-- no Sort node: the leaves already return rows in created_at order

4Why Not a Binary Tree or a Hash Table?

A binary search tree has two children per node, so a billion keys need about 30 levels. If each node were a separate allocation on disk, a lookup could touch 30 different pages. That is fine in memory, where Java's TreeMap lives, but terrible on storage, where each page read can cost a fraction of a millisecond or more. The B+ tree is essentially a binary tree reshaped so that one disk read makes a 400-way decision instead of a 2-way one.

A hash index maps a key straight to a bucket, so an equality lookup can be even cheaper than in a B+ tree. But hashing destroys order: neighbouring keys land in unrelated buckets. So a hash index cannot answer ranges, BETWEEN, prefix searches with LIKE 'abc%', ORDER BY or MIN and MAX. PostgreSQL does offer hash indexes for pure equality on large values, but B-tree is the default because it handles every one of those cases well.

Index structures for a billion keys
StructurePage reads per lookupRange and ORDER BYNotes
binary search treeabout 30yesdeep, made for memory
hash indexabout 1 to 2noequality only
B+ tree, fanout 4004, top levels cachedyes, linked leavesdefault in PostgreSQL and InnoDB

5What Inserts Cost: Page Splits

When we insert a key, the database finds the leaf where it belongs and adds the entry. If the leaf is full, it splits: it allocates a new page, moves about half of the entries there, and adds a separator for the new page to the parent. Occasionally the parent is full too and splits as well; if the root splits, the tree grows one level taller. This is how the tree stays balanced without ever being rebuilt.

Splits cost extra writes, and they leave pages partly empty. When keys arrive in increasing order, as with a sequence, databases optimise for it: the new key always goes to the rightmost leaf, and that page stays hot in memory. Random keys, such as UUIDv4, cause splits all over the tree, which is why they make large indexes slower and bigger.

6Follow-up questions

What is the difference between a B tree and a B+ tree?

In a classic B tree, inner nodes also store full entries, so a lookup can stop at an inner node, but each inner node holds fewer separators and the tree is a little deeper. In a B+ tree, inner nodes hold only separators, all entries live in the leaves, and the leaves are linked in order. That gives a higher fanout and makes range scans a simple walk along the leaves, which is why databases use B+ trees even when their documentation simply says B-tree.

Does a wider key change the height?

Yes, through fanout. A 16-byte UUID or a 40-byte text key fits fewer entries per page, perhaps 200 instead of 400, so the tree may need one more level for the same number of rows, and the index takes more pages and memory overall.

Why can an index scan be slower than a sequential scan?

If the query matches a large fraction of the table, the index finds entries quickly but then jumps to a different data page for almost every row, which can mean many random reads. Reading the whole table sequentially is then cheaper, and the planner chooses it.

7Conclusion

In this answer, we saw that a B+ tree stores hundreds of keys per page, so a billion keys fit in about four levels, and because the top levels stay cached a lookup costs one or two disk reads. All entries live in leaves linked in key order, so a range query descends once and then walks neighbouring leaves, which also serves ORDER BY. Binary trees are about 30 levels deep and hash indexes cannot do ranges or ordering. Inserts occasionally split pages, which keeps the tree balanced at the cost of some extra writes.

What to say in the interviewAbout 100 seconds aloud

Open with

The key property of a B+ tree is fanout. Each node is a whole page, 8 KB in PostgreSQL, and an inner page holds a few hundred keys with child pointers, so each level narrows the search by a factor of 300 to 500 for one page read.

Then, if they want more

For a billion keys with a fanout of 400: three levels reach 64 million and four reach about 25 billion, so the tree is four levels deep; even with fanout 300 it is four.

The root and the 400 or so second-level pages are a few megabytes, always cached, and the third level is around a gigabyte, usually cached, so a cold lookup costs one or two real disk reads. All entries are in the leaf level, and the leaves are linked in key order.

So a range query descends once to the first matching key and then reads along the leaves, getting hundreds of keys per page, and the same order serves ORDER BY and LIMIT without a sort. A binary search tree would need about 30 levels, which is fine in memory but means up to 30 page reads on disk.

A hash index is good for equality, but it has no order, so it cannot do ranges, prefix matches or sorting.

The cost of a B+ tree is on writes: a full leaf splits in two and pushes a separator to the parent, which keeps the tree balanced but costs extra writes and leaves pages partly empty, especially with random keys.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why do relational databases use B+ trees for indexes instead of binary search trees or...": https://scrivio-interview-library.netlify.app/databases/storage-internals/dc7531ad19/