Quick revision
The short answer
Databases use B+ trees because their data lives in fixed-size pages on disk, and a B+ tree node is a whole page holding hundreds of keys, so a billion rows fit in a tree only 3 or 4 levels deep and a lookup costs a handful of page reads, most of them already cached. A binary tree would be about 30 levels deep with one random read per level, which is fine in RAM and terrible on disk. B+ tree leaves are also linked in key order, which makes range scans efficient, and for a Java developer this explains why composite index column order matters and why random UUID keys hurt write performance.
What to remember
In this answer, we saw that databases use B+ trees because they read data in pages, and a page-sized node with hundreds of keys gives a tree only 3 or 4 levels deep for a billion rows, so a lookup reads very few pages, while a binary tree's 30 levels suit RAM but not disk. Linked, sorted leaves make range scans, ORDER BY and keyset pagination cheap. For Java developers, this explains why a composite index serves queries on its leftmost columns first and why random UUID keys cause page splits and poor locality compared with sequential or time-ordered keys.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Databases use B+ trees because their data lives in fixed-size pages on disk, and a B+ tree node is a whole page holding hundreds of keys, so a billion rows fit in a tree only 3 or 4 levels deep and a lookup costs a handful of page reads, most of them already cached. A binary tree would be about 30 levels deep with one random read per level, which is fine in RAM and terrible on disk. B+ tree leaves are also linked in key order, which makes range scans efficient, and for a Java developer this explains why composite index column order matters and why random UUID keys hurt write performance.
Why this question is asked
As Java developers we write queries through JPA, Spring Data or plain JDBC and rarely look at what the database does with them, until a query that took 2 milliseconds takes 2 seconds. Most of those surprises are index surprises, and indexes are B+ trees. Interviewers ask this question to see whether we understand why databases chose a different tree from the one in TreeMap, because that reasoning predicts which queries an index can serve. In this answer, we will see why disks change the trade-off, how a B+ tree is laid out, why linked leaves make ranges cheap, what this means for composite indexes, and why random keys cause trouble.
1Why Disks Change the Trade-Off
In memory, following a pointer to the next node costs around 100 nanoseconds even on a cache miss. A binary tree such as TreeMap's red-black tree has height about log2(n), roughly 30 for a billion keys, so a lookup is around 30 cheap hops. That is a good design for RAM.
A database stores data in pages, fixed-size blocks that it reads and writes as a unit: 8 KB in PostgreSQL and 16 KB by default in MySQL's InnoDB. Reading a page that is not in memory means a disk read, roughly 0.1 milliseconds on an SSD and several milliseconds on a spinning disk. If each tree level were a separate page, 30 levels would mean up to 30 random reads per lookup. The cost that matters is no longer comparisons but pages read, so the tree should have as few levels as possible.
2High Fan-Out: Hundreds of Keys per Page
A B+ tree solves this by making each node a full page. An internal node holds many keys and pointers to child pages; an index entry for a bigint key takes roughly 16 to 30 bytes including overhead, so an 8 KB or 16 KB page holds several hundred of them. The number of children per node is called the fan-out. With a fan-out of 500, three levels of internal pages can address 500 x 500 x 500 = 125 million leaf pages, far more than a billion rows need.
So an index on a billion-row table typically has a height of 3 or 4. The root and the second level are used by every lookup and stay in the database's buffer cache, so in practice a point lookup reads one or two pages that might need a disk access, plus the table page itself.
| Structure | Keys per node | Height | Random reads per lookup |
|---|---|---|---|
| Binary tree | 1 | about 30 | up to 30 if each node is on its own page |
| B+ tree, fan-out 100 | 100 | about 5 | about 5, top levels cached |
| B+ tree, fan-out 500 | 500 | about 4 | about 1 or 2 uncached in practice |
3Linked Leaves Make Range Scans Cheap
In a B+ tree, all the actual index entries live in the leaf pages, sorted by key, and each leaf has a pointer to the next leaf. Internal pages only guide the search. A range query such as created_at BETWEEN two dates does one descent to the first matching leaf, then walks sideways along the leaf chain until it passes the end of the range, reading entries in key order.
That is why an index is useful for ORDER BY and range conditions as well as equality, and why an ORDER BY ... LIMIT 20 query over an index can stop after reading a page or two. It is also why keyset pagination, WHERE id > :lastSeen ORDER BY id LIMIT 20, stays fast on page 10,000, while OFFSET 200000 makes the database walk and throw away 200,000 entries first.
- Root pagea few hundred separator keys, always cached
- Internal pageshundreds of keys each, usually cached
- Leaf pagessorted index entries, linked left to right
- Table pagesthe rows the leaf entries point to (or the rows themselves in InnoDB)
4 layers, from the top down. Step through them one at a time.
InnoDB stores rows inside the primary key's leaves; PostgreSQL leaves point into a separate heap.
4What This Means for Composite Indexes
A composite index on (customer_id, created_at) sorts its leaf entries by customer_id first, and by created_at only among entries with the same customer_id, like a phone book sorted by surname and then first name. That order decides which queries it serves well.
So when we add an index for a Spring Data method like findByCustomerIdAndCreatedAtAfter, the equality column goes first and the range column second. Reversing them forces the database to scan a wide range of created_at and filter by customer. Newer versions can sometimes skip-scan an index without a condition on its leading column when that column has few distinct values (MySQL 8.0.13 and later, PostgreSQL 18), but we should not rely on that; check the plan with EXPLAIN.
5Why Random UUID Keys Hurt
With a sequential key such as a bigint identity, every new row goes at the right end of the index. The database fills the last leaf page, starts a new one, and the hot pages stay in memory. With a random UUID (UUID.randomUUID() in Java, version 4), each new key lands at a random position, so each insert touches a random leaf page that may not be cached. When that page is full, the database splits it into two half-full pages. Over time the index is larger than it needs to be, inserts cause many random reads and writes, and the cache holds less useful data.
This hurts most in InnoDB, where the primary key is the clustered index that physically holds the rows, so random keys scatter the table itself. Time-ordered identifiers such as UUID version 7, which start with a timestamp, keep the benefits of UUIDs while inserting near the right end. PostgreSQL 18 can generate them with uuidv7(); in Java the JDK only generates version 4, so we use a library or generate them in the database.
6Follow-up questions
Why is it called a B+ tree rather than a B-tree?
In a classic B-tree, internal nodes also hold full entries. In a B+ tree, entries live only in the leaves and the leaves are linked, so internal nodes hold only separator keys and fit more of them per page, which raises fan-out, and range scans just follow the leaf chain. Database documentation often says B-tree when the structure is really a B+ tree.
How does this help when reading a query plan?
An Index Scan or Index Range Scan means one descent plus a leaf walk, cheap when few rows match. A Seq Scan on a large table, or an index scan that reads most of the index, means the index did not match the query's shape, often because the leading column has no condition or a function wraps the column.
Should every column used in a WHERE clause get an index?
No. Each index is another B+ tree that every insert and update must maintain, and the planner ignores indexes that match too many rows. We index the queries that matter, with column order matching their equality and range conditions, and drop indexes that statistics show are unused.
7Conclusion
In this answer, we saw that databases use B+ trees because they read data in pages, and a page-sized node with hundreds of keys gives a tree only 3 or 4 levels deep for a billion rows, so a lookup reads very few pages, while a binary tree's 30 levels suit RAM but not disk. Linked, sorted leaves make range scans, ORDER BY and keyset pagination cheap. For Java developers, this explains why a composite index serves queries on its leftmost columns first and why random UUID keys cause page splits and poor locality compared with sequential or time-ordered keys.
Open with
The difference comes from what a lookup costs. In memory, following a pointer is cheap, so a binary tree with about log2 of n levels, around 30 for a billion keys, is fine.
Then, if they want more
A database reads fixed-size pages, 8 KB in PostgreSQL or 16 KB in InnoDB, and an uncached page is a disk read, so 30 levels could mean 30 random reads.
A B+ tree makes each node a page holding hundreds of keys, so the fan-out is in the hundreds and a billion rows fit in a tree of height 3 or 4. The top levels stay cached, so a point lookup usually costs one or two real page reads.
All entries live in the leaves, sorted and linked, so a range query descends once and then walks the leaf chain, which also makes ORDER BY with LIMIT and keyset pagination fast. For me as a Java developer this has practical consequences.
A composite index is sorted by the leftmost column first, so an index on customer_id, created_at serves customer equals 42 and date after X, but not date alone; I put equality columns before range columns.
And random UUID v4 keys insert at random positions, causing page splits, bloated indexes and poor caching, especially in InnoDB where the primary key is clustered; sequential ids or UUID v7 avoid that.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 18, MySQL 8.0
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "Databases use B+ trees for indexes while in-memory libraries use binary trees. Explain...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/8749b25ea3/