{
    "slug": "b_tree_structure",
    "term": "B-Trees & B+ Trees",
    "category": "data_structures",
    "difficulty": "advanced",
    "short": "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.",
    "long": "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.",
    "aliases": [
        "B-tree",
        "B+ tree",
        "database index structure",
        "InnoDB index"
    ],
    "tags": [
        "data-structures",
        "database",
        "performance"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "database_indexing",
        "db_composite_indexes",
        "db_uuid_strategies",
        "covering_index"
    ],
    "prerequisites": [
        "database_indexing",
        "binary_tree",
        "big_o_notation"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/B%2B_tree"
    ],
    "bad_code": "-- B-tree fragmented by UUID v4 primary key:\nCREATE TABLE events (\n    id CHAR(36) PRIMARY KEY DEFAULT (UUID()),  -- Random UUID\n    data JSON\n);\n-- Every INSERT goes to a random leaf position\n-- Constant node splits and page fills\n-- Index fragmentation: 70% over time",
    "good_code": "-- B-tree stays ordered with UUID v7 (time-sorted):\nCREATE TABLE events (\n    id BINARY(16) PRIMARY KEY,  -- UUID v7: timestamp-prefixed\n    data JSON\n);\n-- Inserts always append near the rightmost leaf\n-- Minimal fragmentation\n-- Same O(log n) lookup, better write performance\n\n-- Check fragmentation:\nSHOW TABLE STATUS LIKE events;  -- Data_free shows fragmentation",
    "quick_fix": "Understanding B-tree internals explains why composite index column order matters and why equality conditions before range conditions is always faster",
    "severity": "info",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/b_tree_structure",
        "html_url": "https://codeclaritylab.com/glossary/b_tree_structure",
        "json_url": "https://codeclaritylab.com/glossary/b_tree_structure.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[B-Trees & B+ Trees](https://codeclaritylab.com/glossary/b_tree_structure) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/b_tree_structure"
            }
        }
    }
}