Introduction

A relation is stored as a file of records, and the file is stored on disk in fixed-size blocks (pages). The disk transfers whole blocks, so the cost of a database operation is measured in block accesses, not in record comparisons. File organization decides how records are packed into blocks and in what order the blocks hold them.

The quantities to compute are the blocking factor, the number of blocks a file needs, and the number of block accesses for a search under a given organization. These numbers are also the inputs to every index calculation in Indexing.

Prerequisites

None within the GATE syllabus. Familiarity with logarithms and with the idea of a hash function helps.

Core concepts

  • Record: the stored form of one tuple. A fixed-length record has size RR bytes; variable-length records need a slotted-page layout or length fields.
  • Block: the unit of transfer between disk and memory, size BB bytes.
  • Blocking factor (records per block, unspanned): bfr=⌊B/R⌋\text{bfr} = \lfloor B / R \rfloor.
  • Unspanned organization: a record never crosses a block boundary. Each block wastes B−bfr⋅RB - \text{bfr} \cdot R bytes.
  • Spanned organization: a record may continue in the next block, using a pointer at the end of the block. No space is wasted except the pointer; needed when R>BR > B.
  • Number of blocks for rr records (unspanned): b=⌈r/bfr⌉b = \lceil r / \text{bfr} \rceil.
A 1024-byte block holding ten 100-byte records and 24 unused bytes in the unspanned layout; below, the spanned layout where record 11 starts in block 1 after record 10 and continues in block 2, with a 4-byte pointer at the end of block 1
Figure 1. Unspanned and spanned placement of 100-byte records in 1024-byte blocks.

Three primary file organizations.

  • Heap (unordered) file: a new record goes into the last block (or any block with space). Insertion is cheap; search is a linear scan.
  • Sorted (sequential) file: records are kept in order of an ordering field. Search on that field is a binary search over blocks; insertion must keep the order, so it shifts records or uses overflow blocks and periodic reorganization.
  • Hashed file: a hash function hh on a hash field maps each record to one of MM buckets (one block or a short chain of blocks). An equality search on the hash field reads one bucket.
Eight keys stored three ways with two records per block: a heap file in insertion order, a sorted file in key order, and a hashed file with five buckets under h of k equals k mod 5 where bucket 2 has an overflow chain
Figure 2. The keys 12, 7, 22, 3, 17, 8, 15, 2 in a heap file, a sorted file and a hashed file with h(k)=k mod 5h(k) = k \bmod 5, two records per block.

How it works: access costs

Let bb be the number of blocks of the file. Costs count block accesses.

OperationHeap fileSorted on the search fieldHashed on the search field
Equality search on a key, foundabout b/2b/2 for a uniformly located existing key (exactly (b+1)/2(b+1)/2 for equally occupied blocks), bb worstabout lg⁡b\lg b1 if no overflow
Equality search, not found or non-keybbabout lg⁡b\lg b, plus following blocks for duplicates1 plus overflow blocks
Range searchbbbinary search, then consecutive blocksbb (hashing destroys order)
Insertread and write the last blockfind the place, then shift: about b/2b/2 blocks read and written on averageread and write one bucket

A search on a field other than the ordering or hash field gets no help from the organization: it is a scan of bb blocks.

Binary search over blocks. Read the middle block; if the key lies between its first and last record the search ends in that block, otherwise continue in one half. The worst case reads ⌊lg⁡b⌋+1\lfloor \lg b \rfloor + 1 blocks. Many texts write this as ⌈lg⁡b⌉\lceil \lg b \rceil; the two agree unless bb is a power of 2, where the exact worst case is one more (for b=1024b = 1024: 11 against 10). For this specified algorithm, use the exact worst case ⌊lg⁡b⌋+1\lfloor\lg b\rfloor+1 for b≥1b\ge1. Treat ⌈lg⁡b⌉\lceil\lg b\rceil only as an approximation or a different model explicitly prescribed by the question, never an exact default.

Mathematical explanation

Unspanned waste. With bfr=⌊B/R⌋\text{bfr} = \lfloor B / R \rfloor, the unused space per block is B mod RB \bmod R when R≤BR \le B, so the file wastes b⋅(B mod R)b \cdot (B \bmod R) bytes, ignoring the partly filled last block.

Spanned size. With a pointer of pp bytes per block, the usable space is B−pB - p, and the file needs ⌈rR/(B−p)⌉\lceil rR / (B - p) \rceil blocks.

Static hashing and overflow. If rr records are spread over MM buckets of capacity cc records, at least ⌈r/c⌉\lceil r / c \rceil buckets are needed; real files allocate more (a fill factor below 1) because a skewed hash sends many records to one bucket and creates overflow chains, which cost extra accesses.

Worked examples

Example 1: blocking factor and file size. B=1024B = 1024 bytes, R=100R = 100 bytes, r=30000r = 30000 records.

  • Unspanned: bfr=⌊1024/100⌋=10\text{bfr} = \lfloor 1024/100 \rfloor = 10, b=30000/10=3000b = 30000/10 = 3000 blocks, waste 2424 bytes per block, 7200072000 bytes in all.
  • Spanned with a 4-byte block pointer: ⌈3000000/1020⌉=2942\lceil 3000000 / 1020 \rceil = 2942 blocks. Without any pointer it would be ⌈3000000/1024⌉=2930\lceil 3000000/1024 \rceil = 2930.
  • In the spanned layout of Figure 1, block 1 holds records 1 to 10 (1000 bytes), the first 20 bytes of record 11, and the pointer; the other 80 bytes of record 11 start block 2.

Example 2: search costs on that file. Searching for a key value:

  • Heap: about 15001500 block accesses for a uniformly located existing key (exactly 1500.51500.5 for equally occupied blocks), 30003000 if it does not.
  • Sorted on the key: ⌈lg⁡3000⌉=12\lceil \lg 3000 \rceil = 12, and the exact worst case ⌊lg⁡3000⌋+1=12\lfloor \lg 3000 \rfloor + 1 = 12 agrees.
  • Sorted on the key, but the search is on another field: 30003000.

Example 3: a non-key field on a sorted file. If the file is sorted on a non-key field and 25 records match, they sit in consecutive blocks: with bfr=10\text{bfr} = 10 they span 3 or 4 blocks depending on where the run starts, so the cost is a binary search that locates the first block of the run plus up to 3 further blocks.

Example 4: static hashing with overflow (Figure 2). h(k)=k mod 5h(k) = k \bmod 5, one block of 2 records per bucket, keys 12, 7, 22, 3, 17, 8, 15, 2 in that order. Bucket 0: 15. Bucket 2 receives 12, 7, 22, 17, 2: the home block holds 12, 7, a first overflow block holds 22, 17, and a second holds 2. Bucket 3: 3, 8. Buckets 1 and 4 stay empty. A search for 2 reads 3 blocks; a search for 4 (not present) reads only bucket 4. The heap file of the same keys has 4 blocks and the sorted file holds [2,3],[7,8],[12,15],[17,22][2, 3], [7, 8], [12, 15], [17, 22].

Related material. Extendible hashing (Elmasri and Navathe 16.8; Korth 24.5, which spells it "extendable") avoids long overflow chains: a directory of 2d2^d pointers is indexed by dd bits of the hash value (the global depth), and each bucket records how many bits it really uses (its local depth). A full bucket with local depth below dd splits without touching the directory size; a full bucket with local depth dd doubles the directory first. Using the last bits of the key, capacity 2, and inserting 8, 3, 12, 5, 16 into an empty structure: 12 doubles the directory to d=1d = 1 (buckets {8,12}\{8, 12\}, {3}\{3\}); 5 joins {3,5}\{3, 5\}; 16 finds {8,12}\{8, 12\} full twice, since 88, 1212 and 1616 end in 00, so the directory doubles to d=2d = 2 and then to d=3d = 3. The result has global depth 3, 8 directory entries and 4 buckets: {8,16}\{8, 16\} (bits 000), {12}\{12\} (100), an empty bucket for 010 and 110 (local depth 2), and {3,5}\{3, 5\} for all odd keys (local depth 1). Korth indexes the directory by a prefix (the first ii bits) of the hash value instead of the last bits; the doubling and splitting rules are the same, but the buckets hold different keys, so use the bit order the question gives.

Important properties and observations

  • Cost is in block accesses. Record comparisons inside a block are not counted in these cost models.
  • Floors and ceilings: bfr\text{bfr} takes a floor, the block count takes a ceiling.
  • Spanned records waste no space but a record may need two block reads.
  • Sorted files help only for the ordering field; hashed files help only for equality on the hash field.
  • Hashing gives no order, so range queries scan the file.
  • The binary-search count convention (⌈lg⁡b⌉\lceil \lg b \rceil or ⌊lg⁡b⌋+1\lfloor \lg b \rfloor + 1) matters only when bb is a power of 2.

Common misconceptions.

  • "b=r⋅R/Bb = r \cdot R / B." That is the unrounded byte-volume ratio; the pointer-free spanned count is ⌈rR/B⌉\lceil rR/B\rceil; the unspanned count is ⌈r/⌊B/R⌋⌉\lceil r / \lfloor B/R \rfloor \rceil.
  • "A sorted file speeds up every query." Only queries on its ordering field.
  • "A hashed file needs exactly one access per search." Overflow chains add accesses.

GATE examination perspective

  • Blocking factor and block count. Use BB, RR and rr, unspanned unless stated.
  • Average and worst-case block accesses. Apply this to heap, sorted and hashed files.
  • Binary search on a sorted file. Watch the rounding convention.
  • Wasted space. Under unspanned organization.
  • These counts feed directly into index questions in Indexing.

Summary

Sources

  1. A. Silberschatz, H. F. Korth, S. Sudarshan, Database System Concepts, 7th ed., McGraw-Hill, 2019 (2020 copyright), 13.2 File Organization, 13.3 Organization of Records in Files, 14.5 Hash Indices, 24.5 Hash Indices.
  2. R. Elmasri, S. B. Navathe, Fundamentals of Database Systems, 7th ed., Pearson, 16.4 Placing File Records on Disk, 16.6 Files of Unordered Records (Heap Files), 16.7 Files of Ordered Records (Sorted Files), 16.8 Hashing Techniques.

Next steps