B+ Tree Indexes

Lecture 8 Β· a B+ tree is a modified version of the B-tree and is more commonly used in database systems.

Definition

In a B+ tree:
  • Internal nodes store only search keys.
  • Actual record pointers are stored only in leaf nodes.
  • Leaf nodes are linked together in sorted order.
This makes the B+ tree very efficient for range queries.

Construction β€” keys 10 … 22, max 3 keys per node

InsertActionResulting shape
10, 11, 12Fill the first leaf.[10 11 12]
13Leaf overflows (4 keys) β†’ split 2 | 2 and copy up the separators. The parent keeps the first key of each child.[10 12] β†’ [10 11] Β· [12 13]
14Goes into the right leaf.[10 12] β†’ [10 11] Β· [12 13 14]
15[12 13 14 15] overflows β†’ split, copy up 14.[10 12 14] β†’ [10 11] Β· [12 13] Β· [14 15]
16Goes into the last leaf.[10 12 14] β†’ … Β· [14 15 16]
17Leaf overflows β†’ copy up 16, so the root becomes [10 12 14 16] which itself overflows β†’ split the internal node. The height grows.[10 14] β†’ [10 12] , [14 16]
18Goes into the leaf [16 17].[10 14] β†’ … leaf [16 17 18]
19[16 17 18 19] overflows β†’ split, copy up 18 into the right internal node.[10 14] β†’ [10 12] , [14 16 18]
20Goes into the leaf [18 19].leaf [18 19 20]
21[18 19 20 21] overflows β†’ copy up 20; the internal node [14 16 18 20] overflows β†’ split it and push 18 up to the root.[10 14 18] β†’ [10 12] , [14 16] , [18 20]
22Goes into the leaf [20 21] β†’ [20 21 22]. No overflow.final tree below

Final B+ Tree

10141810121416182010111213141516171819202122rootinternalleaves dashed green = leaf nodes linked together in sorted order β†’ fast range / sequential scans
B+ Tree of height 3. All 13 keys appear in the leaves; the keys above are only copies used for routing.

Operations

Searching

Start at the root and follow one child pointer per level until you reach a leaf. Every search ends at a leaf node, so every search costs exactly the height of the tree β€” which makes performance predictable.

Find key = 21 β†’ 21 β‰₯ 18 β†’ third child [18 20] β†’ 21 β‰₯ 20 β†’ leaf [20 21 22] β†’ found. 3 node reads.

Range query

WHERE Emp_ID BETWEEN 13 AND 20 β†’ descend once to the leaf containing 13 ([12 13]), then follow the leaf chain right: 13, 14, 15, 16, 17, 18, 19, 20. No further tree traversal is needed β€” this is exactly what a B-tree cannot do.

Insertion & deletion

  • Insertion β€” inserting a new key works the same as the B+ tree construction discussed above (insert into the correct leaf; split and copy up on overflow).
  • Deletion β€” after deleting the key, the tree is restructured following the same rule used for constructing the tree (if needed): borrow from a sibling, or merge two underflowing nodes and remove the separator from the parent.

Fan-out

Fan-out means the number of children a node can have. In a B+ tree, internal nodes store only keys and child pointers β€” not actual data record pointers β€” so each internal node can store more keys, which means each node can have more children.
Higher fan-out = fewer tree levels = faster search

Suppose one node can point to 100 child nodes. Then a 3-level B+ tree can index a very large number of records: Root β†’ Internal node β†’ Leaf node. Only a few disk reads may be needed to find a record.

B-Tree vs B+ Tree

FeatureB-treeB+ tree
Data pointer locationInternal and leaf nodesOnly leaf nodes
Internal nodesStore keys and record pointersStore only keys for navigation
Leaf nodesMay or may not be linkedUsually linked
Search pathMay end at an internal nodeAlways ends at a leaf node
Range queryLess efficientMore efficient
Fan-outLowerHigher
Common in DBMSLess commonMore common
Why databases prefer B+ trees. Internal nodes carry no data, so more keys fit in one disk page β†’ higher fan-out β†’ shorter tree β†’ fewer disk I/Os. Every search costs the same. And the linked leaves make BETWEEN, ORDER BY and full scans cheap. In MySQL/InnoDB the primary key is a clustered B+ tree that stores the rows themselves in its leaves.

Applied in the project (with a note on the two split conventions): Task 5 β€” B/B+ tree β†’