Database Systems · Static Hashing Operations Cost and Hash versus B+ Tree
Where this chapter sits
You already know what a hash file is: a hash function maps a search key to a bucket number in , and a bucket is one disk block (or a small chain of blocks). This chapter is about cost: what an insert, a delete, and a rehash actually cost in disk I/Os, why hashing is useless for ranges, and how to choose between hashing and a B+ tree for a given workload. All costs below are in disk block accesses (I/Os); we ignore CPU and treat one bucket read/write as one I/O unless stated.
Notation used throughout:
| Symbol | Meaning |
|---|---|
| number of buckets in the file | |
| number of records currently stored | |
| number of records that fit in one block (blocking factor) | |
| hash function, returns bucket number in | |
| average bucket-chain length in blocks (overflow blocks per bucket) | |
| load factor (fraction of bucket capacity used) |
Chained static hashing: insertion and deletion cost
Structure. Bucket holds up to records. When it is full, an overflow block is appended to that bucket's chain. The directory of bucket addresses is fixed for the life of the file — that is what "static" means.
1.1 Insertion
Method:
- Compute — free (CPU only).
- Read the primary bucket: 1 I/O.
- If it has a free slot, write it back: 1 I/O. Total 2 I/Os.
- If full, walk the overflow chain to its end: each overflow block read is 1 I/O; then write the new record into a new (or the last partially filled) overflow block: 1 I/O.
So insertion cost . (This assumes the pointer to a newly allocated overflow block costs no extra write; if the pointer inside the previous block must be rewritten, add 1 I/O.)
Worked example 1. , , records, all buckets full, no overflow yet. Insert a record hashing to bucket 37.
- Read bucket 37: 1 I/O. It holds 10 records = full.
- No overflow block exists, so we allocate one and write the record: 1 I/O.
- Total = 2 I/Os (the new overflow block is written, not read first).
Worked example 2. Same file, but bucket 37 already has a chain of 3 overflow blocks, the last one full.
- Read primary bucket: 1.
- Read overflow block 1: 1; block 2: 1; block 3: 1 (to discover it is full).
- Write new overflow block 4: 1.
- Total = 5 I/Os.
Key point for exams: insertion is cheap and constant on average — expected — because the chain is short when is kept near 0.8 or below. It degrades to only when the file is badly overfull.
1.2 Deletion
Method:
- Read primary bucket: 1 I/O. Scan it.
- If not found, read each overflow block in turn: 1 I/O each, until found or chain ends.
- Remove the record and write the modified block back: 1 I/O.
Deletion cost for the write-back.
Worked example 3. Delete a record in bucket 37's second overflow block (chain length 3).
- Read primary: 1; read overflow 1: 1; read overflow 2: 1 (found).
- Write overflow 2 back: 1.
- Total = 4 I/Os.
Subtlety — do we reclaim an emptied overflow block? Two conventions exist:
- Lazy: leave the empty block in the chain. Cost stays as above; chains never shrink, so lookups stay slow after many deletes.
- Eager: unlink the empty block and free it. When the chain is walked from the head, the predecessor block was just read and is still in memory, so no re-read is needed: write the predecessor with its updated link (in place of writing back the emptied block). The I/O count is the same as for lazy deletion.
State which convention you assume, and follow the convention given in the question.