3.4 Two Representative Ways to Store Data: Heap and Clustered
Then where and how is the record itself stored? There are broadly two approaches.
Heap: In the next free spot, in arrival order
This approach stores each record anywhere in a data page that has free space, as records are inserted. Records are scattered regardless of key order, and the index leaf holds a row address such as "slot N of page M." The slot number seen in Chapter 1 is exactly this address. After descending the index to the leaf and checking the address, the database must read the corresponding data page once more to retrieve the record.
Figure 3-5. Heap table and PK index (PostgreSQL, Oracle)
flowchart TB
subgraph PK ["PK index (B+Tree)"]
R["Root page"] --> B["Branch page"]
B --> L1["Leaf page<br/>PK 1 → page 7, slot 3<br/>PK 2 → page 2, slot 1"]
B --> L2["Leaf page<br/>PK 3 → page 5, slot 2<br/>PK 4 → page 2, slot 4"]
end
subgraph HEAP ["Heap table (data pages)"]
H2["Page 2<br/>Rows stored in insertion order"]
H5["Page 5"]
H7["Page 7"]
end
L1 -->|"Go to the row address"| H7
L1 --> H2
L2 --> H5
L2 --> H2
The upper part is the PK index and the lower part is the heap table where the actual rows are stored (page and slot numbers are examples). The PK index is a B+Tree made of root, branch, and leaf pages, and the leaves hold the PKs in sorted order. What the leaf holds, however, is not the row itself but a row address consisting of a "page number and slot number."
On the heap table side, rows sit not in PK order but in whichever page has free space, in the order they were inserted. So rows with adjacent keys, such as PK 1 and PK 2, can be in different pages (page 7 and page 2). To read one row, you descend the index from root to leaf and then follow the arrow to read a data page once more.
Clustered table
This approach sorts the records themselves by key order and stores them in pages. Here the primary key index does not exist separately from the table. When you descend the index to the leaf, the record is right there in that leaf. There is no need to check an address and read another page.
Figure 3-6. Structure of a clustered index (InnoDB, SQL Server)
flowchart TB
R["Root page<br/>PK range → branch page"] --> B1["Branch page<br/>PK range → leaf page"]
R --> B2["Branch page<br/>PK range → leaf page"]
B1 --> L1["Leaf page<br/>Full rows for PK 1, 2, 3"]
B1 --> L2["Leaf page<br/>Full rows for PK 4, 5, 6"]
B2 --> L3["Leaf page<br/>Full rows for PK 7, 8, 9"]
L1 <--> L2
L2 <--> L3
In a clustered index, descending through the root and branches leads to leaf pages that store entire rows in PK order. Compared with Figure 3-5, there is no arrow from the leaf down to a data page. Because the leaf page is itself the table's data page, there is no need to make another hop by address as with a heap. Leaves are linked sideways, so a PK range scan finishes by reading the leaf pages in order.
The clustered approach has the advantage that the logical order and the actual physical storage order are the same. Records with nearby keys sit side by side in the same page, so reading "customer numbers 1000 through 1100" means reading pages consecutively. With a heap, records are scattered and several data pages may have to be read. There is a downside too. Records must be placed in key order, so they can only go in a fixed position, and as a result page splits can occur frequently in the PK.
Figure. Comparing B+Tree behavior when steadily inserting monotonically increasing keys and random keys (UUID)
flowchart TB
subgraph MONO ["Monotonically increasing key (1, 2, 3, …)"]
direction LR
M1["Leaf 1<br/>Full"] --> M2["Leaf 2<br/>Full"] --> M3["Rightmost leaf<br/>All inserts go here"]
M3 -.->|"When full"| M4["Add a new leaf on the right"]
end
subgraph RAND ["Random key (UUID v4)"]
direction LR
U1["Leaf 1<br/>Insert → split"] --- U2["Leaf 2<br/>Free space remains"] --- U3["Leaf 3<br/>Insert → split"] --- U4["Leaf 4<br/>Free space remains"]
end
S["Concurrent inserts"] --> M3
S --> U1
S --> U3
In the upper case, the monotonically increasing key, every insert heads for the rightmost leaf. The left leaves are already full and are not touched again, and when the rightmost leaf fills up, a new leaf is simply attached to its right, as the dotted line shows. In the lower case, the random key, inserts are scattered across many leaves, so splits occur throughout the middle of the leaves, and free space is left in each split page. Looking at the "Concurrent inserts" arrows at the top, the monotonically increasing key concentrates on one page while the random key spreads across several pages.
If the primary key increases monotonically, as in 1, 2, 3, new records are always created in the rightmost leaf page. When the page fills up, a new page is simply attached to its right, so the space in earlier pages is used as fully as possible. To create records, the buffer pool needs to cache only the last few pages. In exchange, when many sessions create records concurrently, they all try to modify the same page, causing contention on that page. A random key such as UUID v4 spreads inserts evenly, so contention is low, but page splits keep occurring in the middle of leaves, leaving free space in each page, and because inserting records requires many pages frequently, disk reads surge when the buffer pool is too small. In a clustered structure where the table itself is stored in primary key order (InnoDB, SQL Server) in particular, this difference shows up across the whole table.