Database Systems · Static Hashing Operations Cost and Hash versus B+ Tree
Open addressing: deletion needs tombstones
Structure. No chains. All slots live in one array of buckets; on collision at we probe (linear probing) mod until a free slot is found. Each probe of a different bucket is a separate I/O.
2.1 Insertion
Method: probe from forward; the first empty slot is where the record goes. Cost = number of probes = number of I/Os (each probed bucket is read; the final one is written).
Worked example 4. , linear probing, . Insert keys 20, 31, 42 in that order.
- 20: → slot 9 free. 1 probe.
- 31: → occupied; probe 10 → free. 2 probes.
- 42: → occupied; 10 occupied; 0 free. 3 probes.
2.2 The deletion problem
If you simply empty the slot of a deleted key, a later search stops early and wrongly reports "not found". Example: after inserting 20, 31, 42 above, delete 31 (slot 10). Now search for 42: it hashes to 9 (occupied by 20), probes 10 — empty — and stops. 42 is at slot 0 but is never reached. Wrong answer.
Fix: tombstones. Mark the slot deleted (a tombstone), not empty. Search treats a tombstone as "keep probing"; insertion may reuse a tombstone but must remember the first tombstone seen and continue probing to confirm the key is not further along.
Method for delete: probe exactly as for search; on finding the key, mark the slot as a tombstone and write it back. Cost = probes to find + 1 write.
Worked example 5. In the table above, delete 31: probe 9 (20, no), probe 10 (31, found) → mark tombstone. Cost = 2 reads + 1 write = 3 I/Os. Now search 42: probe 9 (no), probe 10 (tombstone → continue), probe 0 (42, found). Correct.
Trap: tombstones accumulate. After many delete/insert cycles the table fills with tombstones, probe sequences lengthen, and search cost rises even though few live records remain. The only cure is a rehash (Section 4). Also: a tombstone may be reused by an insert, but only after the insert has confirmed the key is absent — otherwise you can create duplicate keys.
Why static hashing cannot do ranges or ordered scans
A hash function is deliberately designed so that adjacent keys land in unrelated buckets. Consequences:
- Range query
WHERE age BETWEEN 20 AND 30: you cannot compute which buckets hold ages 20–30. The only correct plan is a full scan of all buckets (plus overflow chains): cost . - Ordered scan /
ORDER BY/MIN/MAX: hashing gives no order, so you must read everything and sort: cost = full scan + external sort. - Prefix /
LIKE 'abc%': same problem — no locality.
What to use instead: a B+ tree (or any ordered index). A B+ tree keeps keys sorted in leaf level and links leaves, so a range costs one root-to-leaf descent plus a sequential scan of the qualifying leaves. That is the whole reason B+ trees exist alongside hashing.
| Query type | Static hash | B+ tree |
|---|---|---|
Equality K = v | expected — best | — good |
Range a ≤ K ≤ b | full scan, | descent + leaf scan — best |
ORDER BY K, MIN/MAX | full scan + sort | leaf scan, already sorted |
| Prefix match | full scan | range on prefix |