← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

B-Trees & B+ Trees

data_structures Advanced

Also Known As

B-tree B+ tree database index structure InnoDB index

TL;DR

Self-balancing tree structures used in database indexes — each node holds multiple keys, keeping the tree shallow and minimising disk I/O for range queries.

Explanation

A B-Tree stores sorted keys in nodes with multiple children. B+ Trees (the standard in databases) store all data in leaf nodes; internal nodes only store keys for routing. Leaf nodes are linked — enabling efficient range scans. Properties: balanced (all leaves at same depth), fan-out of hundreds (each node holds many keys), O(log n) search/insert/delete. InnoDB, PostgreSQL, and most databases use B+ Trees for indexes. The high fan-out (order 100+) means even billion-row tables have trees only 3-4 levels deep.

Common Misconception

Binary search trees and B-trees are the same — BSTs have 2 children per node; B-trees have hundreds, keeping the tree extremely shallow and cache-friendly for disk-based storage.

Why It Matters

Understanding B+ Trees explains why a database index on a billion-row table still returns in milliseconds (3-4 disk reads), why composite index column order matters (leftmost prefix), and why random writes fragment the B-tree.

Common Mistakes

  • Adding indexes on low-cardinality columns — a boolean column has only 2 values; the B-tree provides no benefit.
  • Not understanding that a full-table scan can be faster than an index for large result sets.
  • UUID v4 primary keys causing B-tree fragmentation — random inserts split nodes constantly.
  • Too many indexes on a write-heavy table — every insert/update must update all B-trees.

Code Examples

✗ Vulnerable
-- B-tree fragmented by UUID v4 primary key:
CREATE TABLE events (
    id CHAR(36) PRIMARY KEY DEFAULT (UUID()),  -- Random UUID
    data JSON
);
-- Every INSERT goes to a random leaf position
-- Constant node splits and page fills
-- Index fragmentation: 70% over time
✓ Fixed
-- B-tree stays ordered with UUID v7 (time-sorted):
CREATE TABLE events (
    id BINARY(16) PRIMARY KEY,  -- UUID v7: timestamp-prefixed
    data JSON
);
-- Inserts always append near the rightmost leaf
-- Minimal fragmentation
-- Same O(log n) lookup, better write performance

-- Check fragmentation:
SHOW TABLE STATUS LIKE events;  -- Data_free shows fragmentation

Added 16 Mar 2026
Edited 22 Mar 2026
Views 32
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 2 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 2 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 14 Perplexity 5 Google 4 Unknown AI 2 Ahrefs 1
crawler 23 crawler_json 3
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Medium
⚡ Quick Fix
Understanding B-tree internals explains why composite index column order matters and why equality conditions before range conditions is always faster
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
Composite index with range column before equality column; queries not using index despite its existence
Auto-detectable: ✗ No mysql-workbench db_explain_advanced
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: File

✓ schema.org compliant