Quick revision
The short answer
A B-tree index, strictly a B+tree, keeps all keys in sorted order in linked leaf pages, with a few levels of inner pages above them that hold separator keys, so finding any key takes 3 or 4 page reads even for millions of rows. Equality, ranges, ORDER BY and LIKE 'abc%' all use that sort order: the database descends to the first matching key and walks the leaves sideways. LIKE '%abc' gives no starting point in the order, so the index cannot narrow the search.
What to remember
In this answer, we saw that a B-tree keeps keys sorted in linked leaf pages under a few levels of separator pages, so lookups take a handful of page reads. Equality and ranges descend to the first match and walk the leaves sideways, ORDER BY reads entries in order and lets LIMIT stop early, and LIKE 'abc%' becomes a range, while LIKE '%abc' has no starting point and needs a trigram index instead. In PostgreSQL, prefix LIKE under a non-C collation needs text_pattern_ops or COLLATE "C".
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.
A B-tree index, strictly a B+tree, keeps all keys in sorted order in linked leaf pages, with a few levels of inner pages above them that hold separator keys, so finding any key takes 3 or 4 page reads even for millions of rows. Equality, ranges, ORDER BY and LIKE 'abc%' all use that sort order: the database descends to the first matching key and walks the leaves sideways. LIKE '%abc' gives no starting point in the order, so the index cannot narrow the search.
Why this question is asked
Indexes are the most common answer to a slow query, and the B-tree is the default index in PostgreSQL, MySQL and almost every other database. Knowing its shape lets us predict which queries it helps without guessing, which is exactly what interviewers check with this question. In this answer, we will look at how the tree is laid out in pages, follow a lookup and a range scan, see how ORDER BY and LIMIT benefit, explain why a prefix LIKE works and a leading wildcard does not, and cover the collation detail in PostgreSQL.
1How the Tree Is Laid Out on Disk
An index is stored in fixed-size pages, 8 KB in PostgreSQL and 16 KB in InnoDB. At the bottom are leaf pages. Each leaf holds index entries in sorted key order, and each entry points to the table row: in PostgreSQL with a tuple id, the page and slot of the row in the heap, and in InnoDB with the primary key. Leaf pages are linked to their neighbours, so the database can move from one leaf to the next without going back up.
Above the leaves are inner pages. An inner page holds separator keys and pointers to child pages: everything below 'k' is in this child, everything from 'k' to 'p' is in that one. At the top is a single root page. Because one 8 KB page holds hundreds of separators, each level multiplies the reach by hundreds. That is why the depth stays tiny: about 3 levels for a few million rows and 4 for hundreds of millions, and a lookup costs one page read per level, O(log n).
- Root pageseparators: m
- Inner pagesa .. f | f .. m, and m .. s | s .. z
- Leaf pages, sorted and linkedada@ -> anna@ -> bob@ -> ... -> zoe@
- Table rowseach leaf entry points to one row
4 layers, from the top down. Step through them one at a time.
2Equality and Range Lookups
For WHERE email = 'bob@x.com', the database starts at the root, compares the value with the separators, follows the matching child pointer, and repeats until it reaches a leaf. There it finds the entry and follows its pointer to the row. Three or four page reads, most of them for inner pages that are almost always in memory.
A range uses the same descent and then keeps going. For WHERE created_at BETWEEN '2026-01-01' AND '2026-01-31', the database descends to the first entry at or after 1 January, then walks the linked leaf pages to the right, entry by entry, and stops at the first entry after 31 January. The cost is the descent plus the number of matching entries, no matter how big the table is.
3Why ORDER BY and LIMIT Get Cheaper
Because the leaves are already sorted, reading entries in leaf order returns rows in key order. A query with ORDER BY created_at can therefore skip the sort step entirely. With DESC, the database walks the leaves backwards, which B-trees support just as well. With a LIMIT, it can stop after the first few entries, so the newest 20 orders out of 200 million cost only a few page reads.
4Prefix LIKE Works, a Leading Wildcard Does Not
A pattern like LIKE 'abc%' means 'every value that starts with abc'. In sorted order, all those values sit next to each other, between 'abc' and 'abd'. So the database rewrites it into a range, email >= 'abc' AND email < 'abd', and uses the index exactly like a BETWEEN.
A pattern like LIKE '%abc' means 'ends with abc'. Values ending in abc are spread all through the sorted order: 'xabc', 'mabc', 'aabc'. There is no first key to descend to and no point to stop, so the index cannot narrow anything, and the database scans the table or the whole index. In PostgreSQL the usual answer for such searches is a trigram index from the pg_trgm extension, which indexes three-character pieces of each value and works with GIN.
5The Collation Catch in PostgreSQL
There is one detail that surprises people. A collation is the set of rules that decides how text sorts, and most databases are created with a language-aware collation such as en_US.UTF-8. Under such rules the sort order is not a simple byte order, so 'starts with abc' is not guaranteed to be one contiguous range, and PostgreSQL will not use a normal index for LIKE 'abc%'.
The fix is either an index built with the text_pattern_ops operator class, which compares character by character, or an index and query using COLLATE "C". A database created with the C collation, or with a deterministic ICU setup that the planner can use, may not need this, so check your version and collation.
Note that a text_pattern_ops index cannot serve an ordinary ORDER BY email under the language collation, so a table may need both indexes if it has both kinds of query.
6Follow-up questions
How deep would a B-tree over 100 million rows be?
With bigint keys, a PostgreSQL leaf page holds a few hundred entries and an inner page a few hundred separators. 100 million entries need roughly 300,000 leaf pages, one inner level reduces that to about a thousand pages, a second inner level to a handful, and then the root. So about 4 levels, which is 4 page reads per lookup, and the top levels are nearly always cached. Wider keys, such as long text, lower the fan-out and may add one more level.
Why does a function on the column, such as lower(email), stop the index being used?
The index is sorted by email, not by lower(email). For WHERE lower(email) = 'bob@x.com', the database would have to compute lower() for every entry to find matches, which is no better than a scan. The fix is an expression index, CREATE INDEX ON users (lower(email)), which stores and sorts the computed values, so a query using exactly the same expression can descend the tree. Alternatively use a case-insensitive type or collation.
Is a B-tree good for every column?
It is the right default for anything we compare, sort or range over. It is a poor fit for full-text search, arrays and jsonb containment, which use GIN, and for very large append-only tables queried by time ranges, where BRIN can be far smaller.
7Conclusion
In this answer, we saw that a B-tree keeps keys sorted in linked leaf pages under a few levels of separator pages, so lookups take a handful of page reads. Equality and ranges descend to the first match and walk the leaves sideways, ORDER BY reads entries in order and lets LIMIT stop early, and LIKE 'abc%' becomes a range, while LIKE '%abc' has no starting point and needs a trigram index instead. In PostgreSQL, prefix LIKE under a non-C collation needs text_pattern_ops or COLLATE "C".
Open with
A B-tree index, really a B+tree, keeps all the keys sorted in leaf pages that are linked to their neighbours, and above them a few levels of inner pages hold separator keys that say which child covers which key range.
Then, if they want more
Because a page holds hundreds of separators, the depth stays small, around three or four levels even for a hundred million rows, so a lookup is a few page reads. For equality the database descends from the root to the leaf with the key.
For a range it descends to the first matching key and then walks the linked leaves sideways until the range ends. Since the entries are already in order, ORDER BY on the indexed column needs no sort, it can even read backwards for DESC, and with a LIMIT it stops after the first rows.
A prefix LIKE such as 'abc%' is really a range between 'abc' and 'abd', so it uses the same mechanism. A leading wildcard like '%abc' matches values spread throughout the sorted order, so there is no starting point and the index cannot help; in PostgreSQL I would use a pg_trgm GIN index.
One catch in PostgreSQL is that with a language collation the planner will not use a normal index for prefix LIKE, so I would add a text_pattern_ops index or use COLLATE C.
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.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
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 "How is a B-tree index organised on disk, and why can it answer equality, range (BETWEEN...": https://scrivio-interview-library.netlify.app/databases/indexing/27bf534a43/