Skip to content
GATEiit

GATE DA · Database Management and Warehousing

File organization and indexing

How records are stored in disk blocks (heap, sorted and hashed files), blocking factor and block-access cost, primary, clustering and secondary indexes, multilevel indexes, and B and B+ trees with order and height calculations.

Before you start

  • Linear and binary search →

    Block-access costs for heap and sorted files are linear and binary search counted in blocks.

  • Trees and hash tables →

    B+ trees are balanced search trees and hashed files are hash tables on disk.

  • Logarithms, floor and ceiling

    Every count of blocks, levels and node capacity uses them.

Concept notes

1. Records, blocks and blocking factor

Data is read from disk one block (page) at a time, so cost is measured in block accesses, not comparisons.

With block size B bytes and fixed record size R bytes (R≤B), the blocking factor is the number of records per block: bfr=⌊BR⌋ For a file of r records, the number of blocks is b=⌈rbfr⌉

This is unspanned organisation: a record never crosses a block boundary, so B−bfr⋅R bytes per block are wasted. In spanned organisation a record may continue in the next block (needed when R>B), and no space is wasted.

Use floor for how many fit, and ceiling for how many blocks are needed. Mixing them up is the commonest arithmetic error.

The same formulas apply to index files: an index entry is (search key, pointer), of size K+P, so the index blocking factor (also the fan-out of a multilevel index) is ⌊B/(K+P)⌋.

2. File organisations

Organisation How records are placed Equality search on key (blocks) Range search Insert
Heap (unordered) at the end b/2 average, b worst full scan cheap: last block
Sorted (sequential) ordered on a field ⌈log⁡2b⌉ by binary search good: find start, then scan costly: shift records or use overflow
Hashed block chosen by h(key) about 1 (plus overflow chain) poor: full scan cheap
  • A search on a non-key field of a heap must read all b blocks, since there may be many matches.
  • Binary search on a sorted file works only for the ordering field.
  • Static hashing with M buckets: h(K) mod M gives the bucket; a full bucket uses overflow blocks, which lengthen searches. Dynamic schemes (extendible and linear hashing) grow the number of buckets as the file grows.
  • Hashing gives no help for range queries, because consecutive keys go to unrelated buckets.

When to use what: heap for bulk loading and full scans; sorted for range queries on one field; hashing for exact-match lookups.

3. Single-level indexes: primary, clustering, secondary

An index is a sorted file of (key, pointer) entries. Being much smaller than the data file, it is binary searched in ⌈log⁡2bi⌉ block accesses, plus 1 to read the data block.

Index Built on Entries Dense or sparse Per file
Primary ordering key field of a sorted file one per data block (block anchor) sparse at most one
Clustering ordering non-key field one per distinct value sparse at most one
Secondary any non-ordering field one per record (key field) dense many

A dense index has an entry for every search-key value (every record, for a key); a sparse index has entries for only some values, so it needs the file to be ordered on that key.

A file is physically ordered on one field only, so it can have either a primary or a clustering index, not both, plus any number of secondary indexes.

For a secondary index on a non-key field, each entry points to a block of record pointers (an extra level of indirection), or the index repeats the key once per record.

Cost with a single-level index: ⌈log⁡2bi⌉+1 block accesses, where bi = number of index blocks.

4. Multilevel indexes and B+ trees

A multilevel index indexes the index. Level 1 has r1 entries; each higher level has one entry per block of the level below. With fan-out fo=⌊B/(K+P)⌋, the levels shrink by a factor of fo until one block (the top level) remains. The number of levels is t=⌈log⁡for1⌉, and a search costs t+1 block accesses (one per level plus the data block).

A B+ tree is a dynamic multilevel index that stays balanced on insert and delete.

  • Internal nodes hold up to p child pointers and p−1 keys; they only guide the search.
  • Leaves hold the keys with their record (data) pointers, and a pointer to the next leaf, so range queries scan along the leaves.
  • Every node except the root is at least half full: at least ⌈p/2⌉ children for internal nodes. The root needs at least 2 children (unless it is a leaf).
  • All leaves are at the same depth.

Order from block size (block pointer P, record pointer Pr, key K):

  • internal: p⋅P+(p−1)K≤B
  • leaf: pleaf(K+Pr)+P≤B

With h internal levels above the leaves, a B+ tree with max fan-out p has at most ph leaves.

B+ tree of order 3306010203045607590leaves hold every key with its record pointer; dashed arrows link leaves for range scansinternal node: keys only guide search
A small B+ tree: the root's keys 30 and 60 split the leaves; every key appears in a leaf, and leaves are chained left to right.

5. B tree versus B+ tree

A B tree of order p stores data pointers in every node, internal ones included; each key appears exactly once. A B+ tree keeps data pointers only in the leaves; internal keys are copies used for routing.

Feature B tree B+ tree
Data pointers in all nodes only in leaves
Key repeated? no internal keys also appear in leaves
Internal node fan-out lower (each entry also holds a data pointer) higher
Height for the same data equal or more equal or less
Equality search may stop at an internal node always reaches a leaf
Range query in-order traversal of the tree follow the leaf chain

B tree node of order p: p tree pointers, p−1 keys and p−1 data pointers: p⋅P+(p−1)(K+Pr)≤B.

Because internal B+ tree nodes carry no data pointers, more keys fit per block, so the tree is shallower and needs fewer block accesses. This and the cheap range scans are why DBMSs use B+ trees for indexes.

Height bounds for a B+ tree with n keys: more keys per node means fewer levels. The minimum height uses full nodes (p children); the maximum uses half-full nodes (⌈p/2⌉ children, root with 2).

Formulas to remember

Blocking factor (unspanned)
bfr=⌊BR⌋
Number of blocks
b=⌈rbfr⌉
Binary search on a sorted file
⌈log⁡2b⌉ block accesses
Single-level index search
⌈log⁡2bi⌉+1
Index fan-out
fo=⌊BK+P⌋
Multilevel index levels
t=⌈log⁡for1⌉,cost=t+1
B+ tree internal node order
p P+(p−1) K≤B
B+ tree leaf order
pleaf (K+Pr)+P≤B
B tree node order
p P+(p−1)(K+Pr)≤B

Common mistakes

  • Using ceiling for the blocking factor or floor for the number of blocks; it is floor for what fits and ceiling for what is needed.
  • Forgetting the +1 block access to read the data block after searching an index.
  • Counting one primary index entry per record; a primary index is sparse, with one entry per data block.
  • Using the leaf formula for internal nodes (or the other way round); leaves have record pointers plus one next-leaf pointer.
  • Confusing order as 'maximum keys' with order as 'maximum pointers'; read which one the question defines.
  • Assuming a file can have both a primary and a clustering index.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Sorted file and primary index

numerical

A file has r=30000 records of R=100 bytes, sorted on its key; block size B=1024 bytes, unspanned. The key is 9 bytes and a block pointer 6 bytes. Find the block accesses to find a record by key (a) by binary search on the file, (b) through a primary index.

  1. Step 1: Find the blocking factor and the number of blocks.

    Try it on paper first.

Example 2: Turning it into a multilevel index

numerical

Continue the previous example: build a multilevel index on the 45-block primary index (fan-out 68). How many levels and how many block accesses?

  1. Step 1: Build levels until one block remains.

    Try it on paper first.

Example 3: Order of a B+ tree

numerical

Block size 512 bytes, key 10 bytes, block pointer 6 bytes, record pointer 7 bytes. Find the maximum number of child pointers in an internal node and of keys in a leaf of a B+ tree.

  1. Step 1: Write and solve the internal-node inequality.

    Try it on paper first.

Example 4: Choosing the right index

conceptual

A STUDENT file is physically sorted on roll_no (a key). Queries also look up students by email (unique) and by city (many per city). Which indexes can be built on roll_no, email and city?

  1. Step 1: The ordering field.

    Try it on paper first.

Previous year questions

3 official GATE DA questions on this topic, newest first.

Practise these 3 PYQs →

Topic test

10 questions: up to 3 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE DA syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.