1. Gyanastra
  2. Database Systems
  3. Static Hashing Operations Cost and Hash versus B+ Tree
  4. Where this chapter sits
Free lesson, no account needed 4 min read

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 hh maps a search key to a bucket number in [0,B−1][0, B-1], 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:

SymbolMeaning
BBnumber of buckets in the file
rrnumber of records currently stored
bbnumber of records that fit in one block (blocking factor)
h(K)h(K)hash function, returns bucket number in [0,B−1][0,B-1]
LLaverage bucket-chain length in blocks (overflow blocks per bucket)
ffload factor =r/(B⋅b)= r/(B\cdot b) (fraction of bucket capacity used)

Chained static hashing: insertion and deletion cost

Structure. Bucket h(K)h(K) holds up to bb records. When it is full, an overflow block is appended to that bucket's chain. The directory of BB bucket addresses is fixed for the life of the file — that is what "static" means.

1.1 Insertion

Method:

  1. Compute h(K)h(K) — free (CPU only).
  2. Read the primary bucket: 1 I/O.
  3. If it has a free slot, write it back: 1 I/O. Total 2 I/Os.
  4. 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 =1+(overflow blocks read)+1= 1 + (\text{overflow blocks read}) + 1. (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. B=100B=100, b=10b=10, r=1000r=1000 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 — O(1)O(1) expected — because the chain is short when ff is kept near 0.8 or below. It degrades to O(L)O(L) only when the file is badly overfull.

1.2 Deletion

Method:

  1. Read primary bucket: 1 I/O. Scan it.
  2. If not found, read each overflow block in turn: 1 I/O each, until found or chain ends.
  3. Remove the record and write the modified block back: 1 I/O.

Deletion cost =(blocks read until found)+1= (\text{blocks read until found}) + 1 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.


Pages 1–2 of 2