1. Gyanastra
  2. Database Systems
  3. File Organisation, Indexing and B and B+ Trees
  4. File Organization, Blocking Factor and Ordered Files
Free lesson, no account needed 11 min read 10 practice questions after it
Asked in past papers4 times
  • GATE CSE202420082005
  • ISRO2015

Database Systems · File Organisation, Indexing and B and B+ Trees

File Organization, Blocking Factor and Ordered Files

Overview

A file organization is the way records of a relation are physically arranged on disk blocks. The DBMS chooses it to make the dominant operation (scan, point lookup, range search, insert) cheap in block I/Os, because disk I/O is the unit of cost — not bytes, not records.

Three ideas drive every GATE/NET question on this topic:

  1. Blocking factor (bfr) — how many records fit in one block. This converts "records" into "blocks", and blocks into I/O.
  2. Ordered (sorted) file — records sorted on a search key; enables binary search on the data file and makes a primary index possible.
  3. Index fan-out — how many (key, pointer) entries fit in one index block; this gives the number of index levels and hence the search cost.

Almost every numerical question is: compute bfr → compute number of blocks → compute index entries per block → compute levels → add I/Os.

Key concepts

Record types

  • Fixed-length records: every record same size RR bytes → simple bfr, no spanning issues.
  • Variable-length records: need slotted page / record headers; bfr is computed on average or worst case.

Spanned vs unspanned

  • Unspanned: a record must lie entirely within one block. Wasted space at block end. bfr =⌊B/R⌋=\lfloor B/R \rfloor.
  • Spanned: a record may continue into the next block. No waste, but a record may need 2 block accesses. Average records per block is B/RB/R (not rounded up); the file needs b=⌈rR/B⌉b=\lceil rR/B\rceil blocks.

File organizations

  • Heap (unordered/pile): records appended anywhere; insert is 1 I/O (write last block); full scan needs all blocks; point search on non-key needs full scan; deletion leaves holes.
  • Sequential/ordered (sorted) file: records sorted on a search key.
    Full scan still reads all blocks, but binary search on the sorted blocks reduces point lookup to ⌈log⁡2b⌉\lceil \log_2 b \rceil block reads (where bb = number of data blocks).
    Inserts are expensive (shift/reorganize).
  • Hash file: bucket address = h(K) mod Mh(K) \bmod M; point lookup ≈ 1 I/O; useless for range queries and for full scan is same as heap (must read all blocks).
  • Clustered file: related records of multiple relations stored together; good for joins on the cluster key.

Indexes (needed for the index questions)

  • Primary index: built on the ordering key field of a sorted data file;
    one entry per data block (block anchor = first/least key in the block).
    Sparse index.
    Number of index entries = number of data blocks bb.
  • Clustering index: built on an ordering non-key field; one entry per distinct value of that field.
  • Secondary index: built on a non-ordering field; must be dense — one entry per record (or per distinct value with a block-pointer list).
    Number of entries = number of records rr (or distinct values).
  • Multilevel index: the index file itself is ordered, so build an index over the index. Fan-out fof_o = entries per index block.

I/O efficiency for a full scan (GATE 2024 idea): a scan must touch every record, so it reads all data blocks regardless of organization.
Sorted file, heap file, and any index-based file all cost bb block reads for a complete scan. An index does not help a full scan (it only adds overhead if used).
So for "scan operation", sorted and heap are both I/O efficient (they read each block once, sequentially); unclustered tree/hash indexes are not efficient for scan because following unclustered pointers causes random I/O and may re-read blocks.

How it works (steps)

Step 1 — Blocking factor of the data file bfr=⌊BR⌋ (unspanned)bfr=BR (spanned, average, not rounded)bfr = \left\lfloor \frac{B}{R} \right\rfloor \text{ (unspanned)} \qquad bfr = \frac{B}{R} \text{ (spanned, average, not rounded)} BB = block size in bytes, RR = record size in bytes.

Step 2 — Number of data blocks (unspanned; for spanned use b=⌈rR/B⌉b=\lceil rR/B\rceil) b=⌈rbfr⌉b = \left\lceil \frac{r}{bfr} \right\rceil rr = number of records.

Step 3 — Index record size and fan-out For a primary index entry: size =K+P= K + P where KK = key field size, PP = block pointer size. fo=⌊BK+P⌋f_o = \left\lfloor \frac{B}{K+P} \right\rfloor For a secondary index entry on a key field: same formula but entries = rr (dense).

Step 4 — Number of first-level index blocks

  • Primary index: b1=⌈b/fo⌉b_1 = \lceil b / f_o \rceil (entries = data blocks).
  • Secondary index on key: b1=⌈r/fo⌉b_1 = \lceil r / f_o \rceil (entries = records).

Step 5 — Levels of a multilevel index b2=⌈b1/fo⌉,  b3=⌈b2/fo⌉,… until bk=1b_2 = \lceil b_1/f_o \rceil,\; b_3 = \lceil b_2/f_o \rceil, \dots \text{ until } b_k = 1 Number of levels =k= k. Total index blocks =b1+b2+⋯+bk= b_1 + b_2 + \dots + b_k.

Step 6 — Search cost

  • Binary search on sorted data file: ⌈log⁡2b⌉\lceil \log_2 b \rceil block reads.
  • Multilevel primary index: kk index block reads + 1 data block read =k+1= k+1.
  • Secondary index (dense, multilevel): kk index reads + 1 data block (if key unique).

Formulas

QuantityFormulaSymbols
Blocking factor (unspanned)⌊B/R⌋\lfloor B/R \rfloorBB block bytes, RR record bytes
Blocking factor (spanned)B/RB/R (average, not rounded); blocks =⌈rR/B⌉=\lceil rR/B\rceilsame
Data blocks⌈r/bfr⌉\lceil r/bfr \rceilrr records
Index entry sizeK+PK+PKK key bytes, PP pointer bytes
Fan-out⌊B/(K+P)⌋\lfloor B/(K+P) \rfloorentries per index block
Primary index entriesbb (one per data block)bb data blocks
Secondary index entries (key)rr (one per record)rr records
Clustering index entriesnumber of distinct values of ordering non-key—
Levelssmallest kk with bk=1b_k=1bi+1=⌈bi/fo⌉b_{i+1}=\lceil b_i/f_o\rceil
Binary search I/O⌈log⁡2b⌉\lceil \log_2 b \rceilbb data blocks
Index search I/Ok+1k+1kk levels + 1 data block
Full scan I/Obball data blocks

Worked examples (with all numbers)

Example 1 — GATE-style (ordered file + primary index; original paper/year unverified). Given: r=25,000r = 25{,}000 records, R=100R = 100 bytes, key K=15K = 15 bytes, block-aligned (unspanned); we assume B=1024B = 1024 bytes and pointer P=6P = 6 bytes (illustrative values). Compute (a) data blocks, (b) primary index blocks, (c) total blocks, (d) levels and search cost.

  • bfr=⌊1024/100⌋=10bfr = \lfloor 1024/100 \rfloor = 10 records/block.
  • Data blocks b=⌈25000/10⌉=2500b = \lceil 25000/10 \rceil = 2500.
  • Index entry =15+6=21= 15 + 6 = 21 bytes; fan-out fo=⌊1024/21⌋=48f_o = \lfloor 1024/21 \rfloor = 48.
  • Primary index entries =b=2500= b = 2500; first level b1=⌈2500/48⌉=53b_1 = \lceil 2500/48 \rceil = 53.
  • b2=⌈53/48⌉=2b_2 = \lceil 53/48 \rceil = 2; b3=⌈2/48⌉=1b_3 = \lceil 2/48 \rceil = 1. So 3 levels.
  • Total index blocks =53+2+1=56= 53 + 2 + 1 = 56. Total file blocks =2500+56=2556= 2500 + 56 = 2556.
  • Search: 3 index reads + 1 data read =4= 4 block I/Os (vs ⌈log⁡22500⌉=12\lceil \log_2 2500 \rceil = 12 for binary search on data alone).

Example 2 — GATE-style (secondary index on a candidate key; original paper/year unverified). Given: r=1,50,000r = 1{,}50{,}000 records, B=4096B = 4096 bytes, pointer P=7P = 7 bytes, candidate key ANum =12= 12 bytes. Index record = (ANum, record pointer). Find index blocks and levels.

  • Index entry =12+7=19= 12 + 7 = 19 bytes; fo=⌊4096/19⌋=215f_o = \lfloor 4096/19 \rfloor = 215.
  • Secondary index on a key → dense → entries =r=150,000= r = 150{,}000.
  • b1=⌈150000/215⌉=698b_1 = \lceil 150000/215 \rceil = 698.
  • b2=⌈698/215⌉=4b_2 = \lceil 698/215 \rceil = 4; b3=⌈4/215⌉=1b_3 = \lceil 4/215 \rceil = 1. 3 levels.
  • Total index blocks =698+4+1=703= 698 + 4 + 1 = 703. Search =3+1=4= 3 + 1 = 4 I/Os.

Example 3 — GATE 2008 (secondary index on a key of an ordered file). Given: r=16384r = 16384 records, R=32R = 32 bytes, key field =6= 6 bytes, block pointer =10= 10 bytes, B=1024B = 1024 bytes, unspanned. Secondary index on the key field, multilevel. Find levels and total index blocks.

  • Data bfr=⌊1024/32⌋=32bfr = \lfloor 1024/32 \rfloor = 32; data blocks b=⌈16384/32⌉=512b = \lceil 16384/32 \rceil = 512.
  • Secondary index on key → dense → entries =16384= 16384. Entry size =6+10=16= 6 + 10 = 16 bytes.
  • fo=⌊1024/16⌋=64f_o = \lfloor 1024/16 \rfloor = 64.
  • b1=⌈16384/64⌉=256b_1 = \lceil 16384/64 \rceil = 256; b2=⌈256/64⌉=4b_2 = \lceil 256/64 \rceil = 4; b3=⌈4/64⌉=1b_3 = \lceil 4/64 \rceil = 1. 3 levels, total index blocks =256+4+1=261= 256 + 4 + 1 = 261. The original GATE 2008 question asks only for the first- and second-level block counts: 256 and 4 (option C).
  • Note the distractor: if you wrongly treat it as a primary index (entries =b=512= b = 512), you get b1=⌈512/64⌉=8b_1 = \lceil 512/64 \rceil = 8, b2=1b_2 = 1 → 2 levels, which is the trap. The file is ordered on a non-key field, so the index on the key field is secondary and dense.

Example 4 — GATE 2024 (scan efficiency, conceptual). Question: which file organizations are I/O efficient for a scan? Answer: Sorted and Heap. Reasoning: a scan reads every record, so it must read all bb data blocks; sorted and heap files store records contiguously in blocks, giving sequential bb-block I/O. An unclustered tree or hash index is not efficient for scan: records are scattered, so following pointers causes random I/O and blocks may be revisited; the index adds reads without reducing the bb data-block reads.

Example 5 — binary search vs index on an ordered file. b=10,000b = 10{,}000 data blocks. Binary search =⌈log⁡210000⌉=14= \lceil \log_2 10000 \rceil = 14 I/Os. With a primary index of fan-out fo=100f_o = 100: b1=100b_1 = 100, b2=1b_2 = 1 → 2 levels → 2+1=32+1 = 3 I/Os. Index wins because fan-out >2> 2.

Comparison

OrganizationPoint lookupRange queryFull scanInsertSorted on key?
Heapbb (full scan)bbbb (efficient)1 (append)No
Ordered/sequential⌈log⁡2b⌉\lceil \log_2 b\rceil (binary search)⌈log⁡2b⌉+\lceil\log_2 b\rceil + matching blocksbb (efficient)expensive (reorganize)Yes
Hash~1bb (useless)bb (efficient)~1No
Primary index (sparse)k+1k+1k+1k+1 + range blocksbb (index not used)moderateYes (on key)
Secondary index (dense)k+1k+1k+1k+1 + many random blocksnot efficient (unclustered)moderateNo
Index typeBuilt onDensityEntries
Primaryordering key fieldsparsenumber of data blocks bb
Clusteringordering non-key fieldsparsenumber of distinct values
Secondarynon-ordering fielddensenumber of records rr (or distinct values)

Remember (traps and exceptions)

  • Scan cost is always bb data blocks for any organization; an index never helps a full scan. GATE 2024 answer: sorted + heap.

  • Primary index is sparse (one entry per block); secondary index is dense (one entry per record). Mixing these up changes the entry count and hence the levels — the classic GATE 2008 trap.

  • Ordered on a non-key field ⇒ index on the key field is secondary, not primary. Read the stem carefully: "ordered on a non-key field" is a deliberate trap.

  • Unspanned uses floor ⌊B/R⌋\lfloor B/R\rfloor for bfr and wastes the tail of each block; spanned wastes nothing, so blocks =⌈rR/B⌉=\lceil rR/B\rceil.

  • Block-aligned in the stem means unspanned — records never cross blocks.

  • Fan-out uses floor: ⌊B/(K+P)⌋\lfloor B/(K+P)\rfloor; you cannot fit a partial entry.

  • Levels stop when the count reaches 1, and the top level block is counted. Search I/O =k+1= k+1 (the +1+1 is the data block).

  • Binary search needs the file sorted on the search key; on a heap it is impossible.

  • Hash is useless for range queries and for "ordered output" — a frequent one-mark trap.

  • Pointer size is given separately from key size; forgetting to add it is the most common arithmetic error.

  • Clustering index entries = distinct values, not blocks and not records — a third count that appears in NET/BARC questions.

  • Deletion in a heap leaves holes; a scan still reads those blocks, so cost stays bb until reorganization.

Pages 1–2 of 2