B-Tree Indexes

Lecture 8 Β· a B-tree is a balanced tree index where both internal nodes and leaf nodes can store search keys and record pointers. Balanced means all leaf nodes are at the same level, so the search path length is almost equal for all records.

Why tree-based indexing?

B-tree and B+ tree are tree-based indexing structures used in databases to search, insert, delete and retrieve records efficiently. They are used because simple indexes may become very large β€” and if the index is very large, searching the index itself becomes costly. B-trees solve this by organizing index entries in a balanced tree structure.

Without indexing
The DBMS may search row by row:
101 β†’ 102 β†’ 103 β†’ 104 β†’ 105 β†’ 106
6-step search
With B-tree indexing
The DBMS follows a path from the root node to the required value.
2-step search

Storage structure

A B-tree is stored as a separate entity/table. It stores the key and a pointer β€” not the entire table. In this case: Emp_ID and the address of the row.

EMPLOYEE table

Emp_IDNameDepartmentSalaryMemory Location
10RahimCSE50000v53
11KarimEEE45000v54
12NilaCSE55000v55
13HasanBBA40000..
14RonyCSE60000..
15MinaEEE52000..
16AbdulCSE50000..
17KuddusEEE45000..
18NilimaCSE55000..
19HasnainBBA40000..
20RobiCSE60000..
21MinaEEE52000..
22NinaCSE55000..

One B-tree node

101112v53v54v55

Top row = the actual key (Emp_ID). Bottom row = the physical address/pointer to the data row stored in memory.

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

Split rule used in this course. Insert first; if a node then holds more than 3 keys, split it. Split on the middle β€” N/2 (ceiling) with N = 4 keys β†’ the 3rd key is promoted to the parent, the first two stay in the left node and the last one goes to the right node.
InsertActionResulting shape
10, 11, 12Add keys in sorted order β€” the single (root) node fills up.[10 11 12]
13The node now has 4 keys β†’ split. Split on the middle, N/2 (ceiling) with N = 4 β†’ the 3rd key, which is 12. 12 moves up and becomes the root.[12] β†’ [10 11] Β· [13]
14Goes into the right leaf.[12] β†’ [10 11] Β· [13 14]
15Still fits (3 keys max).[12] β†’ [10 11] Β· [13 14 15]
16[13 14 15 16] overflows β†’ split, promote 15 into the root.[12 15] β†’ [10 11] Β· [13 14] Β· [16]
17Goes into the last leaf.[12 15] β†’ … Β· [16 17]
18Still fits.[12 15] β†’ … Β· [16 17 18]
19[16 17 18 19] overflows β†’ split, promote 18.[12 15 18] β†’ [10 11] Β· [13 14] Β· [16 17] Β· [19]
20Goes into the last leaf.[12 15 18] β†’ … Β· [19 20]
21Still fits.[12 15 18] β†’ … Β· [19 20 21]
22[19 20 21 22] overflows β†’ promote 21; the root [12 15 18 21] now overflows too β†’ split it and promote 18. The height grows.final tree below

Final B-Tree

18121521101113141617192022rootinternalleaves
B-Tree of height 3. Every key 10–22 appears exactly once β€” 18, 12, 15 and 21 live in internal nodes and are not repeated in the leaves.
Read it back: root [18] β†’ left [12 15] β†’ leaves [10 11] [13 14] [16 17]; right [21] β†’ leaves [19 20] [22]. All leaves are at the same level, so the tree is balanced.

Search for 16: 16 < 18 β†’ left; 15 < 16 < … β†’ third child of [12 15] β†’ leaf [16 17] β€” three node reads. Search for 18: found at the root β€” one read. That β€œa search may end at an internal node” is a defining property of the B-tree.
The tree only grows at the root. The height increased exactly once β€” when inserting 22 forced the root itself to split. That is why all leaves always stay on the same level.

Practice online: planetscale.com β€” B-trees and database indexes
Next: B+ Tree indexes β†’