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:
- Blocking factor (bfr) — how many records fit in one block. This converts "records" into "blocks", and blocks into I/O.
- Ordered (sorted) file — records sorted on a search key; enables binary search on the data file and makes a primary index possible.
- 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 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 .
- Spanned: a record may continue into the next block. No waste, but a record may need 2 block accesses. Average records per block is (not rounded up); the file needs 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 block reads (where = number of data blocks).
Inserts are expensive (shift/reorganize). - Hash file: bucket address = ; 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 . - 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 (or distinct values). - Multilevel index: the index file itself is ordered, so build an index over the index. Fan-out = 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 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 = block size in bytes, = record size in bytes.
Step 2 — Number of data blocks (unspanned; for spanned use ) = number of records.
Step 3 — Index record size and fan-out For a primary index entry: size where = key field size, = block pointer size. For a secondary index entry on a key field: same formula but entries = (dense).
Step 4 — Number of first-level index blocks
- Primary index: (entries = data blocks).
- Secondary index on key: (entries = records).
Step 5 — Levels of a multilevel index Number of levels . Total index blocks .
Step 6 — Search cost
- Binary search on sorted data file: block reads.
- Multilevel primary index: index block reads + 1 data block read .
- Secondary index (dense, multilevel): index reads + 1 data block (if key unique).
Formulas
| Quantity | Formula | Symbols |
|---|---|---|
| Blocking factor (unspanned) | block bytes, record bytes | |
| Blocking factor (spanned) | (average, not rounded); blocks | same |
| Data blocks | records | |
| Index entry size | key bytes, pointer bytes | |
| Fan-out | entries per index block | |
| Primary index entries | (one per data block) | data blocks |
| Secondary index entries (key) | (one per record) | records |
| Clustering index entries | number of distinct values of ordering non-key | — |
| Levels | smallest with | |
| Binary search I/O | data blocks | |
| Index search I/O | levels + 1 data block | |
| Full scan I/O | all data blocks |
Worked examples (with all numbers)
Example 1 — GATE-style (ordered file + primary index; original paper/year unverified). Given: records, bytes, key bytes, block-aligned (unspanned); we assume bytes and pointer bytes (illustrative values). Compute (a) data blocks, (b) primary index blocks, (c) total blocks, (d) levels and search cost.
- records/block.
- Data blocks .
- Index entry bytes; fan-out .
- Primary index entries ; first level .
- ; . So 3 levels.
- Total index blocks . Total file blocks .
- Search: 3 index reads + 1 data read block I/Os (vs for binary search on data alone).
Example 2 — GATE-style (secondary index on a candidate key; original paper/year unverified). Given: records, bytes, pointer bytes, candidate key ANum bytes. Index record = (ANum, record pointer). Find index blocks and levels.
- Index entry bytes; .
- Secondary index on a key → dense → entries .
- .
- ; . 3 levels.
- Total index blocks . Search I/Os.
Example 3 — GATE 2008 (secondary index on a key of an ordered file). Given: records, bytes, key field bytes, block pointer bytes, bytes, unspanned. Secondary index on the key field, multilevel. Find levels and total index blocks.
- Data ; data blocks .
- Secondary index on key → dense → entries . Entry size bytes.
- .
- ; ; . 3 levels, total index blocks . 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 ), you get , → 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 data blocks; sorted and heap files store records contiguously in blocks, giving sequential -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 data-block reads.
Example 5 — binary search vs index on an ordered file. data blocks. Binary search I/Os. With a primary index of fan-out : , → 2 levels → I/Os. Index wins because fan-out .
Comparison
| Organization | Point lookup | Range query | Full scan | Insert | Sorted on key? |
|---|---|---|---|---|---|
| Heap | (full scan) | (efficient) | 1 (append) | No | |
| Ordered/sequential | (binary search) | matching blocks | (efficient) | expensive (reorganize) | Yes |
| Hash | ~1 | (useless) | (efficient) | ~1 | No |
| Primary index (sparse) | + range blocks | (index not used) | moderate | Yes (on key) | |
| Secondary index (dense) | + many random blocks | not efficient (unclustered) | moderate | No |
| Index type | Built on | Density | Entries |
|---|---|---|---|
| Primary | ordering key field | sparse | number of data blocks |
| Clustering | ordering non-key field | sparse | number of distinct values |
| Secondary | non-ordering field | dense | number of records (or distinct values) |
Remember (traps and exceptions)
-
Scan cost is always 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 for bfr and wastes the tail of each block; spanned wastes nothing, so blocks .
-
Block-aligned in the stem means unspanned — records never cross blocks.
-
Fan-out uses floor: ; you cannot fit a partial entry.
-
Levels stop when the count reaches 1, and the top level block is counted. Search I/O (the 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 until reorganization.