B-tree explained in plain terms means understanding how a database finds one row among millions without scanning them all. The central question is how an index can stay fast as a table grows from hundreds of rows to billions, and the answer is a balanced tree: a structure that keeps every search short by keeping the tree shallow and every leaf at the same depth.
A B-tree is a self-balancing tree data structure that stores sorted keys in nodes with many children, so that searching, inserting, and deleting all take logarithmic time. It was designed for storage, where reading a block from disk is far slower than working in memory, and it remains the default index in most relational databases.
The idea is simple: each node holds many keys, so the tree stays short, and every leaf sits at the same depth, so every search costs about the same. This article explains what a B-tree is, how it works, and where it is used.
Key takeaways
- A B-tree is a balanced tree that keeps sorted data searchable in logarithmic time.
- Each node holds many keys, so the tree stays shallow even for huge tables.
- All leaves sit at the same depth, so every lookup costs about the same.
- B-trees power the default indexes of most relational databases.
- They suit range queries and ordered scans, not just single lookups.
What a B-tree is
A B-tree is a tree in which each node can hold many keys and many children, unlike a binary tree, where each node holds one key and two children. The number of children a node may have is set by the tree’s order, and a node is split when it fills up and merged when it empties, which keeps the tree balanced without ever rebuilding it from scratch.
The defining property is balance. Every leaf is at the same depth, so the distance from the root to any key is the same, and the height of the tree grows only as the logarithm of the number of keys. A B-tree holding a billion keys with a branching factor of a hundred is only about five levels deep, which means a lookup touches only five nodes.
B-trees were invented by Rudolf Bayer and Edward McCreight in 1972, at Boeing, for storing data on disk. A disk reads data in blocks, and a node is sized to fit one block, so each step down the tree costs one block read and no more. Our guide to LSM trees explains a different index design that favours writes over reads, and our guide to file systems explains the storage layer on which B-trees sit.
How a B-tree works
Searching starts at the root and compares the wanted key with the keys in the node, choosing the child whose range contains it. The search repeats at each level until it reaches a leaf, where the key is either found or shown to be absent, and the number of steps is the height of the tree.
Inserting a key follows the same search to the right leaf, then adds the key there. If the leaf is full, it splits into two nodes and the middle key moves up to the parent, which may split in turn, all the way to the root if needed. Splitting keeps every node within its size limits and the tree balanced.
Deleting a key removes it from its node, and if the node falls below its minimum size, it borrows a key from a sibling or merges with one. The merge may shrink the parent, and in the extreme case the root disappears and the tree becomes one level shorter, still balanced.
Range queries are where B-trees shine. Because the keys are stored in order and the leaves are linked, finding every key between two values means finding the first and then walking forward, with no extra searching. This is why a database can answer a query for all orders in a month almost as cheaply as a query for a single order.
The cost of a B-tree is the write overhead of keeping it balanced. Every insert or delete may split or merge nodes, and each of those steps writes a block to disk, so a B-tree does more write work than a simple log. That is the trade it makes: slower writes in exchange for fast reads and ordered access.

Comparing a B-tree with other indexes
A B-tree is one of several ways to index data, and each was built with a different emphasis. A hash index answers single-key lookups in constant time but cannot order keys, while an LSM tree writes fast but reads through layers, and each approach trades one strength for another.
The table below sets out the main contrasts between the leading approaches.
| Aspect | B-tree | Hash index | LSM tree |
|---|---|---|---|
| Lookup speed | Logarithmic, few block reads | Constant, one probe | Logarithmic, checks several layers |
| Range queries | Excellent, keys in order | Not supported | Good, after merging layers |
| Write cost | Moderate, splits and merges | Low, one write | Very low, append only |
| Ordered scans | Natural, leaves linked | Not possible | Possible, with compaction |
| Best for | General database indexes | Exact-match lookups | Write-heavy workloads |
The practical result is that a B-tree is the right default for a general database, a hash index suits caches and exact lookups, and an LSM tree fits systems that write far more than they read.
Where B-trees are used
Relational databases use B-trees for their primary indexes. PostgreSQL, MySQL, and SQLite all build B-tree indexes by default, and a query that filters on an indexed column uses the tree to find matching rows without scanning the table.
File systems use B-trees to track files and directories. NTFS, ext4, and Apple’s APFS all organise directory entries in B-trees, so looking up a file by name in a directory with millions of entries stays fast.
Key-value stores use B-trees when reads matter more than writes. LMDB and Berkeley DB store their data in B-trees, which makes them a good fit for embedded databases and for caches that need ordered access.
Search engines use B-tree variants for their term dictionaries. The list of terms is kept sorted, so finding the documents that contain a word, and the words that start with a prefix, are both cheap operations on the same structure.
Operating systems use B-trees in their virtual memory and process tables. The kernel keeps track of memory regions and open files in trees that must support fast lookup and ordered traversal, which is exactly what a B-tree provides.
Strengths and limits
The greatest strength of a B-tree is its predictability. Every lookup costs about the same, every operation is logarithmic, and the structure degrades gracefully as data grows, which is why it has survived fifty years of new storage technology.
It is also a strength that B-trees keep data in order. Range queries, ordered scans, and prefix searches all come for free, which no hash-based structure can offer, and which is why databases still choose B-trees as their default.
The limits are real, though. B-trees write more than a log-structured design, because splits and merges rewrite whole nodes, and a B-tree on a solid-state drive can wear it faster than an append-only design. For workloads that are almost entirely writes, an LSM tree is the better choice.
B-tree checklist
- Use a B-tree index for columns filtered by equality or range.
- Index the columns your queries actually filter on, not every column.
- Keep the index small; wide keys make the tree deeper.
- Expect writes to cost more than reads; measure before adding indexes.
- Use composite indexes for queries that filter on several columns together.
- Rebuild or vacuum indexes that have grown fragmented over time.
- Avoid indexing columns with very few distinct values.
- Test range queries; a B-tree earns its keep on ordered access.
- Consider an LSM tree when writes dominate reads.
Frequently asked questions
What is a B-tree in simple terms?
It is a balanced tree that stores sorted keys in wide nodes, so any lookup, insert, or delete takes only a few steps, however large the data grows.
Why are B-trees used in databases?
Because they keep lookups logarithmic, support range queries, and match the block structure of disks, so each step costs a single block read.
What is the difference between a B-tree and a binary search tree?
A binary search tree holds one key per node and can grow unbalanced, while a B-tree holds many keys per node and stays balanced, which makes it far shallower.
How deep is a B-tree with a billion keys?
With a branching factor of a hundred, about five levels, so a lookup touches only five nodes from root to leaf.
What happens when a B-tree node fills up?
It splits into two nodes and the middle key moves up to the parent, which may split in turn, keeping the tree balanced.
When should I not use a B-tree?
When the workload is almost entirely writes, or when only exact-match lookups are needed, where an LSM tree or a hash index fits better.
The bottom line
B-tree explained in one line is this: keep sorted keys in wide, balanced nodes, and every search stays short.
It is the index that made large databases practical, and the reason a query can find one row among billions in a handful of steps.
This article is general information about how B-trees work, not advice about choosing an index for a particular system.







