Skip to main content

Databases (DBMS)

Indexing & File Organization

Indexing questions are calculations on disk blocks: find the blocking factor, index size and search cost, then compute B+ tree order, fan-out, capacity and height.

~5 hrs ~1–1.5 marks · High 9 samples · 8 practice · 8 cards

GATE CSE indexing and file organization — blocking factor, heap/sorted/hashed files, primary, clustering and secondary indexes, sparse vs dense, B and B+ tree order and height, static and extendible hashing, with worked numericals, sample questions, flashcards and drills.

Learning outcomes

  • Compute blocking factor, number of blocks and search cost for heap and sorted files
  • Size primary (sparse), clustering and secondary (dense) indexes and their access cost
  • Derive B-tree and B+ tree order from block size and compute capacity and height
  • Explain static hashing, extendible hashing and the effect of global and local depth

File organization, blocking and search cost

  • Records are stored in fixed-size disk BLOCKS. BLOCKING FACTOR bfr = floor(block size / record size) (records do not span blocks in the unspanned organization). NUMBER OF BLOCKS b = ceil(r / bfr) for r records.
  • HEAP (unordered) file: insertion is cheap (append, 1 block access); search needs a linear scan: on average b/2 blocks for an existing key, b blocks in the worst case or for a missing key.
  • SORTED (ordered) file on a key: BINARY SEARCH takes ceil(log2 b) block accesses; insertion is costly (shift records). Range queries on the ordering field are efficient.
  • HASHED file: a hash function maps the key to a bucket (block); equality search costs about 1 block access, but range queries are not supported.
  • Disk access time = seek time + rotational latency + transfer time. Average rotational latency is half a rotation. Reading a block dominates cost, so the unit of cost in GATE is the number of BLOCK ACCESSES.
  • A file can have at most ONE physical ordering, hence at most one primary or clustering index, but many secondary indexes.
  • SPANNED vs UNSPANNED: in an unspanned organization a record never crosses a block boundary (bfr = floor(B / R), some space wasted per block). In a spanned organization records may continue in the next block, so blocks needed = ceil(r * R / B) and no space is wasted.
  • RANGE and EQUALITY costs: a heap needs b accesses for any search; a sorted file needs ceil(log2 b) to find the first match and then reads the following blocks for a range; a hash file needs about 1 access for equality but cannot answer ranges.
  • INSERTION cost: heap 1 block read/write at the end; sorted file moves on average half the file (or uses an overflow area); hash file goes to one bucket. DELETION leaves empty space (deletion marker) in heap and hash files.
  • DISK TIMES: block transfer time = block size / transfer rate; average rotational latency = half a revolution = 0.5 * 60 / rpm seconds; average access time = seek + rotational latency + transfer. For 7200 rpm the average latency is 4.17 ms.

Formulas to remember

  • bfr = floor(B / R); b = ceil(r / bfr)
  • Linear search (key present): b/2 on average, b worst case
  • Binary search on ordered file: ceil(log2 b)
File organizationEquality search (blocks)Range searchInsert
Heap (unordered)b/2 average, b worstb (scan all)1 (append)
Sorted on search keyceil(log2 b)ceil(log2 b) + extra blocksexpensive (shift records)
Hashed on search key~1not supported~1

Worked example

Problem. Block size 1024 bytes, record size 100 bytes, 10 000 records. Find bfr, b, and the binary-search cost on a sorted file.

Solution. bfr = floor(1024 / 100) = 10. b = ceil(10 000 / 10) = 1000 blocks. Binary search = ceil(log2 1000) = 10 block accesses (since 2^10 = 1024). A linear search needs 500 accesses on average.

Primary, clustering and secondary indexes

  • An INDEX is an auxiliary ordered file of (search key, pointer) entries; it is much smaller than the data file so searching it is cheaper. Index entry size = key size + pointer size; index blocking factor bfri = floor(B / entry size).
  • PRIMARY INDEX: the data file is ORDERED on the KEY field; the index has one entry per DATA BLOCK (anchor record), so it is SPARSE. Number of index entries = b; index blocks bi = ceil(b / bfri). Search cost with binary search on the index = ceil(log2 bi) + 1.
  • CLUSTERING INDEX: the data file is ordered on a NON-KEY field; one entry per distinct value; it is sparse. SECONDARY INDEX: on a field that does NOT determine the file order; it must be DENSE, one entry for every record (or one per distinct value with an extra level of pointers). Search cost = ceil(log2 bi) + 1.
  • DENSE index: an entry for every search-key value (record). SPARSE index: entries for only some values (e.g. one per block); needs the file to be ordered on the search key. Dense indexes are faster to search but larger and costlier to maintain.
  • MULTILEVEL INDEX: build an index on the index until the top level fits in one block. Number of levels = ceil(log_(bfri) of the first-level entries) and cost = number of levels + 1 data-block access. A B+ tree is the dynamic version of a multilevel index.
  • Clustered (clustering/primary) index: data order = index order, so at most one per table and efficient for range queries. Non-clustered (secondary) index: any number, but each lookup fetches blocks scattered over the file.
  • Dense secondary index size: entries = number of records r; blocks = ceil(r / bfri).
IndexFile ordered onEntriesDense / sparse
Primarykey fieldone per data blocksparse
Clusteringnon-key fieldone per distinct valuesparse
Secondary (key)not ordered on fieldone per recorddense
Secondary (non-key)not ordered on fieldone per record or value + pointer blockdense

Worked example

Problem. A sorted file occupies 1000 blocks and has a primary index with 16-byte entries; block size 1024 bytes. Find index blocks and the search cost.

Solution. Entries per index block = floor(1024 / 16) = 64. The sparse index has 1000 entries, so bi = ceil(1000 / 64) = 16 blocks. Binary search in the index = ceil(log2 16) = 4 accesses, plus 1 data-block access = 5.

A sorted file of three blocks with a dense index of nine entries on the left and a sparse primary index of three entries on the right, plus the index size and search cost formulas.
Dense index: one entry per record (works on any field). Sparse index: one entry per block (needs the file ordered on the key), so it is about bfr times smaller.

Multilevel indexes: worked numericals

  • To build a MULTILEVEL INDEX, treat the first-level index as a sorted file and put a sparse index on it; repeat until one block remains. Each upper level has ceil(previous level blocks) entries, one per block of the level below.
  • LEVELS: first level has N1 entries (b for a primary index, r for a dense secondary index); blocks at level i = ceil(N_i / bfri); N_(i+1) = blocks at level i. Stop when blocks = 1. Search cost = (number of index levels) + 1 data-block access.
  • Compared with a single-level index with binary search (ceil(log2 bi) + 1), a multilevel index replaces the logarithm base 2 by base bfri, so it needs far fewer accesses.
  • For a secondary index on a NON-KEY field, either keep one entry per record (duplicates in the index) or one entry per distinct value pointing to a block of record pointers (extra block access).
  • When asked for the "number of index blocks", sum the blocks of every level; when asked for the "number of block accesses", count levels plus the data block.
LevelEntriesBlocks = ceil(entries / 64)Remarks
Data file100,000 records10,000 (bfr = 10)record size 100 B, block 1024 B
Index level 1 (sparse primary)10,000157one entry per data block
Index level 21573one entry per level-1 block
Index level 331top level fits in one block

Worked example

Problem. A sorted file of 100 000 records of 100 bytes each is stored in 1024-byte blocks. A primary index has 16-byte entries. Find the number of levels of a multilevel primary index and the cost of a search, and compare with a single-level index that uses binary search. Then repeat for a dense secondary index on a non-ordering key (one entry per record).

Solution. bfr = floor(1024 / 100) = 10, so b = 10 000 blocks; bfri = floor(1024 / 16) = 64. Level 1: 10 000 entries, ceil(10 000 / 64) = 157 blocks. Level 2: 157 entries, ceil(157 / 64) = 3 blocks. Level 3: 3 entries, 1 block. So 3 levels, and a search costs 3 + 1 = 4 accesses. Single-level with binary search: ceil(log2 157) + 1 = 8 + 1 = 9 accesses. Secondary dense index: 100 000 entries, ceil(100 000 / 64) = 1563 blocks; level 2: ceil(1563 / 64) = 25 blocks; level 3: 1 block. Multilevel cost = 3 + 1 = 4; single-level binary cost = ceil(log2 1563) + 1 = 11 + 1 = 12.

B-trees, B+ trees and hashing

  • ORDER p (GATE convention): a node has at most p child pointers and p-1 keys. B TREE: every node stores keys with their record pointers, p*Pb + (p-1)*(K + Pr) <= B. B+ TREE: internal nodes store only keys and child pointers, p*Pb + (p-1)*K <= B; LEAF nodes hold all keys with record pointers and a next-leaf pointer, pleaf*(K + Pr) + Pb <= B. Leaves are linked, giving fast range scans.
  • MINIMUM OCCUPANCY: every non-root internal node has at least ceil(p/2) children (ceil(p/2) - 1 keys); the root has at least 2 children (if not a leaf). In a B+ tree a non-root leaf has at least ceil((p-1)/2) keys. All leaves are at the same level, so the tree is height-balanced.
  • CAPACITY (root at level 1, h levels): maximum keys in the leaves = p^(h-1) * (p-1) for leaf order p. HEIGHT for N keys and fan-out f is about ceil(log_f N). Number of block accesses for a B+ tree search = h (tree levels) + 1 (data block) when leaves hold record pointers.
  • INSERTION: insert in the proper leaf; if it overflows, SPLIT. In a B+ tree a leaf split COPIES the middle key up to the parent (the key stays in a leaf); an internal-node split PUSHES the middle key up (it does not stay). A split propagating to the root increases height by 1. Follow the textbook rule stated in the question for the split point.
  • DELETION may borrow from a sibling or MERGE nodes (underflow); B+ trees keep all leaves at one depth. B+ trees are better than B trees for range queries and give a larger fan-out in internal nodes (no record pointers there), hence a smaller height.
  • STATIC HASHING: fixed number of buckets, hash h(k) mod M; collisions beyond a block go to OVERFLOW chains, and performance degrades as the file grows. DYNAMIC hashing adapts: EXTENDIBLE HASHING keeps a DIRECTORY of 2^d entries (d = global depth) pointing to buckets whose local depth is at most d; when a bucket whose local depth equals d overflows the directory doubles, otherwise only the bucket splits. LINEAR HASHING grows one bucket at a time without a directory.
  • Hash indexes support only equality search; ordered indexes (B+ tree) support range and sorted access.

Formulas to remember

  • B+ internal node: p*Pb + (p-1)*K <= B (maximum p)
  • B tree node: p*Pb + (p-1)*(K + Pr) <= B
  • Max keys in B+ leaves with h levels and order p: p^(h-1) * (p-1)
  • Min children of non-root internal node = ceil(p/2)
  • Extendible hashing directory size = 2^(global depth)
Quantity (B = 1024, K = 10, Pb = 6, Pr = 8)FormulaValue
B+ internal order p6p + 10(p - 1) <= 102464 (63 keys, 64 pointers)
B+ leaf: max keys n18n + 6 <= 102456 keys
B tree node order p6p + (p - 1)(10 + 8) <= 102424p <= 1042, p = 43
Min children, non-root internalceil(p / 2)32
Max keys in a 3-level B+ tree64^2 * 56229,376
Max keys in a 4-level B+ tree64^3 * 5614,680,064

Worked example

Problem. Block size 1024 bytes, key 10 bytes, block (child) pointer 6 bytes. Find the order p of a B+ tree internal node and the minimum number of children of a non-root internal node.

Solution. Need 6p + 10(p - 1) <= 1024, i.e. 16p <= 1034, so p <= 64.6 and p = 64 (check: 64*6 + 63*10 = 1014 <= 1024; p = 65 gives 1030 > 1024). Minimum children of a non-root internal node = ceil(64/2) = 32.

A small B+ tree with a root of keys 20, 40, 60 and four linked leaves, with the order and height calculation for block size 1024, key 10, block pointer 6 and record pointer 8.
B+ tree: internal nodes only route (keys + child pointers), leaves hold keys with record pointers and are chained for range scans. Fan-out 64 gives 4 levels for 10^6 keys.

B+ tree insertion and height: worked numericals

  • For order p (maximum p child pointers) a node holds at most p - 1 keys. A leaf with p keys after an insertion is overflowing and must split; GATE questions often define the leaf capacity directly ("each node holds at most 3 keys").
  • LEAF SPLIT: the overflowing leaf is divided into two nodes (usually left gets ceil(keys / 2)); the SMALLEST key of the new right leaf is COPIED into the parent. INTERNAL SPLIT: the overflowing node (with k = p keys) is split and the MIDDLE key is MOVED UP; it does not remain in either half.
  • HEIGHT FOR GIVEN KEY COUNT: the minimum height uses maximum fill, the maximum height uses minimum fill. For n keys, order p: h_min = smallest h with p^(h-1) * (p - 1) >= n (leaf order p), h_max uses ceil(p/2) as the fan-out.
  • B tree vs B+ tree for the same block: B+ internal order is larger (no record pointers in internal nodes), so the B+ tree is shorter or equal in height.
  • NUMBER OF NODES visited by a search = height (one block per level); all leaves are at the same depth so every search costs the same.
  • Deleting from a node that falls below the minimum first tries to BORROW from a sibling (and fix the parent key); if the sibling is at minimum, the nodes are MERGED and the parent loses a key, which can cascade up and shrink the tree by one level.

Worked example

Problem. Insert 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 in this order into an empty B+ tree in which every node holds at most 3 keys (order 4). On a leaf split the left leaf keeps 2 keys and the smallest key of the right leaf is copied up; on an internal split the middle key moves up. Find the height and the number of leaves, and the contents of the root.

Solution. Insert 10, 20, 30: one leaf [10 20 30]. Insert 40: leaf overflows, split into [10 20] [30 40]; copy 30 up, root [30]. Insert 50 gives [30 40 50]; insert 60 overflows it, split into [30 40] [50 60], copy 50 up: root [30 50]. Insert 70: [50 60 70]; insert 80 overflows it, split [50 60] [70 80], copy 70 up: root [30 50 70] (3 keys, still allowed). Insert 90: [70 80 90]. Insert 100 overflows it, split [70 80] [90 100], copy 90 up: the root would hold [30 50 70 90] (4 keys), so it splits: [30 50], middle key 70 moves up, [90]. New root [70]. Final tree: height 3, root [70]; internal nodes [30 50] and [90]; 5 leaves [10 20] [30 40] [50 60] [70 80] [90 100].

Hashing: static, extendible and linear

  • A HASH FILE stores a record in the bucket (block) chosen by h(key). Good hash functions spread keys evenly. LOAD FACTOR = records / (buckets * bucket capacity); search cost grows with overflow chain length.
  • STATIC HASHING with M buckets: bucket index = h(k) mod M. Overflow records go to chained overflow blocks. Expected cost of an equality search ~ 1 + (average chain length). Resizing requires rehashing the whole file.
  • EXTENDIBLE HASHING: directory of 2^d pointers (d = global depth) indexed by the first d bits of the hash. Each bucket has a LOCAL DEPTH ld <= d, and 2^(d - ld) directory entries point to it. A full bucket with ld < d splits without changing the directory size; a full bucket with ld = d forces the directory to DOUBLE and then splits.
  • After a split the local depth of both new buckets is ld + 1, and the keys are redistributed by the next bit of their hash. If all keys still go to one side, the split repeats (the directory may double again).
  • LINEAR HASHING grows one bucket at a time in a fixed order using a split pointer and a hash-function family h_i(k) = k mod (2^i * N); it needs no directory, but uses overflow chains until the bucket in line is split.
  • Hash indexes are great for equality joins and point lookups, useless for range queries, ORDER BY or prefix search; B+ tree indexes handle both.
SchemeDirectoryGrowthRange queries
Static hashingnonefixed M buckets, overflow chainsno
Extendible hashing2^d pointerssplit one bucket; directory doubles when ld = dno
Linear hashingnonesplit the bucket at the split pointerno
B+ treetree of keyssplits propagate upwardyes

Worked example

Problem. An extendible hash file has global depth 2 and bucket capacity 3. Directory entry 00 points to bucket A (local depth 2) holding 1, 2, 3; entry 01 to bucket B (local depth 2) holding 4, 5, 6; entries 10 and 11 both point to bucket C (local depth 1) holding 9, 12, 14. The hash of a key is its 4-bit binary value and the first bits select the directory entry. Insert 11 and then 7. Find the global depth, directory size and number of buckets at the end.

Solution. Insert 11 = 1011: prefix 10 leads to bucket C (full, local depth 1 < global depth 2), so only C splits: 10 -> {9, 11} and 11 -> {12, 14}, both with local depth 2. Directory size stays 4; buckets = 4. Insert 7 = 0111: prefix 01 leads to bucket B (full, local depth 2 = global depth 2), so the directory doubles to global depth 3 (8 entries) and B splits on the third bit: 010 -> {4, 5} and 011 -> {6, 7}, each with local depth 3. Buckets A, C0, C1 keep local depth 2 and are each pointed to by 2 directory entries. Final: global depth 3, directory size 8, number of buckets 5.

Static hashing with h(k) = k mod 4 and an overflow chain, next to an extendible hashing directory with global depth 2 and the rules for bucket split versus directory doubling.
Hashing: static hashing chains overflow blocks, extendible hashing splits one bucket or doubles the directory. Neither supports range queries.

Exam tips

  • Write bfr, b and the index size step by step; floor for records per block, ceil for the number of blocks.
  • Primary index = sparse (one entry per block); secondary index must be dense (one entry per record).
  • Order p from the block size: maximise p subject to p*Pb + (p-1)*K <= B (B+ internal).
  • Max keys in a B+ tree of h levels = p^(h-1) * (p-1); minimum child count of a non-root internal node = ceil(p/2).
  • A leaf split copies a key up; an internal split pushes a key up. Follow the rule given in the question.
Exam pattern · 9 questions

GATE sample questions & answers

9 GATE-style MCQs for Indexing & File Organization with full explanations. Attempt each question before revealing the answer.

Q1 Blocking-factor numerical

A file has 10 000 fixed-length records of 100 bytes each, stored in 1024-byte blocks (unspanned). The number of blocks needed is:

  • A) 977
  • B) 1000
  • C) 1024
  • D) 10 000
Show answer & explanation

Answer: B

bfr = floor(1024 / 100) = 10, so b = 10 000 / 10 = 1000 blocks.

Q2

The file in the previous question is sorted on a key. The maximum number of block accesses to find a record by binary search is:

  • A) 8
  • B) 9
  • C) 10
  • D) 11
Show answer & explanation

Answer: C

ceil(log2 1000) = 10 because 2^9 = 512 < 1000 <= 1024 = 2^10.

Q3

A sorted file of 1000 blocks has a sparse primary index with 16-byte entries (block size 1024 bytes). The number of block accesses to retrieve a record using binary search on the index and the data block is:

  • A) 4
  • B) 5
  • C) 6
  • D) 11
Show answer & explanation

Answer: B

bfri = 64, index blocks = ceil(1000 / 64) = 16, binary search = ceil(log2 16) = 4, plus 1 data block = 5.

Q4

A dense secondary index is built on a file with 10 240 records. Each index entry uses 16 bytes and a block is 1024 bytes. How many blocks does the index occupy?

  • A) 100
  • B) 160
  • C) 200
  • D) 640
Show answer & explanation

Answer: B

bfri = 64 entries per block; 10 240 / 64 = 160 blocks.

Q5

In a B+ tree, block size 1024 bytes, key 10 bytes and block pointer 6 bytes. The maximum order p of an internal node is:

  • A) 60
  • B) 63
  • C) 64
  • D) 65
Show answer & explanation

Answer: C

6p + 10(p - 1) <= 1024 gives p <= 64.6, so p = 64.

Q6

A B+ tree of order p = 4 (internal nodes have at most 4 children and leaves at most 3 keys) has 3 levels. The maximum number of keys stored in its leaves is:

  • A) 27
  • B) 36
  • C) 48
  • D) 64
Show answer & explanation

Answer: C

Level 2 has up to 4 nodes, level 3 up to 16 leaves; each leaf holds at most 3 keys: 16 * 3 = 48 = 4^2 * 3.

Q7

The minimum number of children of a non-root internal node in a B+ tree of order 7 is:

  • A) 2
  • B) 3
  • C) 4
  • D) 7
Show answer & explanation

Answer: C

ceil(7 / 2) = 4.

Q8

In extendible hashing, if the global depth is 3, the directory contains:

  • A) 3 entries
  • B) 6 entries
  • C) 8 entries
  • D) 9 entries
Show answer & explanation

Answer: C

The directory has 2^d entries; 2^3 = 8.

Q9

Which statement about indexes is TRUE?

  • A) A table can have several clustering indexes
  • B) A secondary index on an unordered field must be dense
  • C) A sparse index can be built on a non-ordered field
  • D) B+ tree leaves are not linked
Show answer & explanation

Answer: B

Only one physical order exists, so one clustering index. Sparse indexes require ordering on the search key; a secondary index must therefore be dense. B+ tree leaves are linked.

Topic drill · 8 questions

8 practice questions — Indexing & File Organization

Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.

P1

Block size 4096 bytes and record size 512 bytes; the file has 1000 records. The number of blocks is:

  • A) 100
  • B) 125
  • C) 128
  • D) 512
Show answer & explanation

Answer: B) 125

bfr = 4096 / 512 = 8; b = 1000 / 8 = 125.

P2

The number of block accesses of a binary search on the sorted file of 125 blocks (worst case) is:

  • A) 6
  • B) 7
  • C) 8
  • D) 125
Show answer & explanation

Answer: B) 7

ceil(log2 125) = 7 since 2^6 = 64 < 125 <= 128 = 2^7.

P3

A dense secondary index has 8500 entries of 12 bytes each; block size 1024 bytes. The index occupies:

  • A) 85 blocks
  • B) 100 blocks
  • C) 101 blocks
  • D) 120 blocks
Show answer & explanation

Answer: B) 100 blocks

bfri = floor(1024 / 12) = 85; blocks = 8500 / 85 = 100.

P4

Block 512 bytes, key 8 bytes, block pointer 8 bytes. The maximum order p of a B+ tree internal node is:

  • A) 30
  • B) 31
  • C) 32
  • D) 33
Show answer & explanation

Answer: C) 32

8p + 8(p - 1) <= 512 gives 16p <= 520 and p = 32.

P5

A B+ tree has order p = 5 (leaf holds at most 4 keys) and 2 levels (root plus leaves). The maximum number of keys is:

  • A) 4
  • B) 16
  • C) 20
  • D) 25
Show answer & explanation

Answer: C) 20

The root has up to 5 children, each a leaf with up to 4 keys: 5 * 4 = 20 = p^(h-1) (p-1) with h = 2.

P6

An extendible-hashing directory has 16 entries. The global depth is:

  • A) 2
  • B) 3
  • C) 4
  • D) 16
Show answer & explanation

Answer: C) 4

2^d = 16 gives d = 4.

P7

A B+ tree index with 3 levels (root to leaf) is used to find a record whose pointer is in a leaf. The number of block accesses, including the data block, is:

  • A) 3
  • B) 4
  • C) 5
  • D) 2
Show answer & explanation

Answer: B) 4

Three tree blocks (root, internal, leaf) plus one data block = 4.

P8

Which of the following supports range queries efficiently?

  • A) static hashing
  • B) extendible hashing
  • C) B+ tree index
  • D) linear hashing
Show answer & explanation

Answer: C) B+ tree index

B+ tree leaves are ordered and linked, so range scans are efficient. Hash structures support only equality search.

Topic FAQs

What is the difference between a sparse and a dense index?

A dense index has an entry for every record (search-key value); a sparse index has an entry for only some values, typically one per data block, and needs the file to be ordered on the index field.

Why are B+ trees preferred over B trees in databases?

B+ internal nodes hold only keys, so fan-out is larger and the tree is shorter; leaves are linked, so range scans and sorted access are fast.

How many clustered indexes can a table have?

At most one, because the data file can be physically ordered in only one way. It can have many non-clustered (secondary) indexes.

How does extendible hashing grow?

When a bucket overflows and its local depth equals the global depth, the directory doubles (global depth +1) and the bucket splits; if the local depth is smaller only the bucket splits and the directory pointers are redistributed.

Quick revision · 8 cards

8 flashcards — Indexing & File Organization

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 Blocking factor and number of blocks?
Q2 Binary search cost on a sorted file?
Q3 Primary vs secondary index?
Q4 B+ tree internal-node order inequality?
Q5 Max keys in a B+ tree with h levels and order p?
Q6 Minimum children of a non-root internal node?
Q7 B tree vs B+ tree?
Q8 Extendible hashing directory size?