Computer Science and IT‹›⌂ 0:00 studied

Module 10 · 15 min read · Free chapter

File organisation and indexing

In short

Query cost is counted in block accesses. The blocking factor is ⌊B/R⌋ records per block. A primary index is sparse, a secondary index on a key is dense, and a multilevel index needs about log to the base fan-out levels. A B+ tree keeps data pointers only in linked leaves, so it has a larger fan-out and fast range queries.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: why a book has an index

Imagine a 900-page book and a question: "On which page is normal form explained?" Without help you would turn page after page until you found it. But the book has an index at the back, sorted alphabetically, that says "Normal form, 212", so you open page 212 directly.

A database faces the same problem on a much bigger scale. Its tables live on disk, and the disk does not hand over single rows; it hands over whole blocks of a few kilobytes each. Reading one block from disk is thousands of times slower than working on data already in memory.

So the real cost of a query is the number of block accesses. A good file organisation and a good index bring that number down from thousands to three or four. This chapter shows how records sit in blocks, how files are arranged, and how indexes and B+ trees find a record in a handful of reads.

Why it matters. When a railway booking site finds your PNR among crores of records in a blink, an index is doing the work. GATE turns this into numericals: count the blocks, count the index levels, and find the order of a B+ tree.

Records and blocks

A record is one row stored as bytes, such as an employee record of 100 bytes. Records of a file are packed into blocks, the fixed-size units that the disk reads and writes, often a few kilobytes. If every record has the same length, it is a fixed-length record, which keeps the arithmetic simple.

The blocking factor is the number of whole records that fit in one block. With block size BB and record size RR, and no record allowed to cross a block boundary, it is

bfr=⌊BR⌋\mathit{bfr} = \left\lfloor \frac{B}{R} \right\rfloor

This arrangement is called unspanned, and the leftover bytes at the end of each block are wasted. In spanned organisation a record may start in one block and finish in the next, which wastes no space but needs a pointer to the continuation.

Worked example. Let B=1024B = 1024 bytes, R=100R = 100 bytes and the file hold r=30,000r = 30{,}000 records. Then bfr=⌊1024100⌋=10\mathit{bfr} = \lfloor \dfrac{1024}{100} \rfloor = 10 records per block, and each block wastes 1024−1000=241024 - 1000 = 24 bytes. The file needs b=⌈3000010⌉=3000b = \lceil \dfrac{30000}{10} \rceil = 3000 blocks.

Why it matters. Every later numerical in this chapter starts from bfr\mathit{bfr} and bb. If you get these two numbers right, the costs of search and the size of every index follow directly.

Heap, sorted and hashed files

How the records are arranged inside the file decides how fast each operation is. There are three basic arrangements, and each is good at something different.

Organisation Insert Search on the key Good for
Heap (unordered) Cheap: add at the end average b2\dfrac{b}{2}, worst bb Bulk loading, small tables
Sorted on a field Costly: shift records ⌈log⁡2b⌉\lceil \log_2 b \rceil Range queries and ordered output
Hashed on a field Cheap: go to the bucket about one block Equality search

Worked example. Take the file of b=3000b = 3000 blocks from the last section. A heap file needs on average 30002=1500\dfrac{3000}{2} = 1500 block reads to find a record, and 3,000 if the record is absent. If the file is sorted on the search field, binary search needs ⌈log⁡23000⌉=12\lceil \log_2 3000 \rceil = 12 reads, since 211=2048<3000≤4096=2122^{11} = 2048 < 3000 \le 4096 = 2^{12}.

A hashed file uses a hash function, such as h(K)=K mod Mh(K) = K \bmod M, to send each record to one of MM buckets. To find a key we compute h(K)h(K) and read that bucket, which is usually one block. When a bucket fills up, extra records go into an overflow chain, and each extra block in the chain adds one more access.

Hashing has one weakness. A query such as "salary between 40 and 60" cannot use it, because neighbouring keys land in unrelated buckets. Sorted files and tree indexes handle such range queries well.

Why it matters. No arrangement wins everywhere, so real databases keep data in one arrangement and add indexes for the other needs. GATE asks the block-access count for each case, and the answer always comes from this table.

Primary, clustering and secondary indexes

An index is a small sorted file of entries of the form (search-key value, block pointer). Because each entry is much smaller than a record, many entries fit in one block, so searching the index is cheap. Indexes are named by the field they are built on and by how many entries they have.

A dense index has an entry for every record, or every search-key value. A sparse index has entries for only some of them, usually one per data block. A sparse index works only when the data file is sorted on the same field, so that once we reach the right block we can find the record inside it.

Index Built on Entries Dense or sparse
Primary Ordering key field of a sorted file One per data block (anchor record) Sparse
Clustering Ordering non-key field One per distinct value Sparse
Secondary Any non-ordering field One per record (for a key field) Dense

A file can be physically sorted on only one field. So it can have at most one primary or clustering index, but as many secondary indexes as needed.

Worked example. Continue with r=30,000r = 30{,}000 records in b=3000b = 3000 blocks of B=1024B = 1024 bytes, and let an index entry be a 9-byte key plus a 6-byte block pointer, so 15 bytes. The index blocking factor is bfri=⌊102415⌋=68\mathit{bfr}_i = \lfloor \dfrac{1024}{15} \rfloor = 68.

A primary index needs one entry per data block, so 3,000 entries in ⌈300068⌉=45\lceil \dfrac{3000}{68} \rceil = 45 index blocks. Binary search on it takes ⌈log⁡245⌉=6\lceil \log_2 45 \rceil = 6 reads, and one more read fetches the data block, so the search costs 7 accesses instead of 12.

A dense secondary index on a non-ordering key needs one entry per record, so 30,000 entries in ⌈3000068⌉=442\lceil \dfrac{30000}{68} \rceil = 442 blocks. Binary search takes ⌈log⁡2442⌉=9\lceil \log_2 442 \rceil = 9 reads plus one data read, which is 10 accesses. Without this index the same search on the heap order would cost 1,500 reads on average.

Why it matters. Index size and search cost are among the most frequent two-mark numericals in GATE. The steps never change: find bfri\mathit{bfr}_i, count the entries, divide, and take log⁡2\log_2.

Multilevel indexes

Even the secondary index above has 442 blocks, and binary search on it still costs 9 reads. But the index is itself a sorted file, so we can build a sparse index on the index. Repeating this, level after level, gives a multilevel index whose top level fits in a single block.

Each index block points to bfri\mathit{bfr}_i blocks of the level below, and this number is called the fan-out, written fo\mathit{fo}. With r1r_1 entries in the first level, the number of levels is

t=⌈log⁡for1⌉t = \lceil \log_{\mathit{fo}} r_1 \rceil

A search reads one block per level and then the data block, so it costs t+1t + 1 accesses.

Worked example. For the dense secondary index, the first level has 442 blocks. The second level needs one entry per first-level block, so ⌈44268⌉=7\lceil \dfrac{442}{68} \rceil = 7 blocks, and the third level needs ⌈768⌉=1\lceil \dfrac{7}{68} \rceil = 1 block. So t=3t = 3 levels, and a search costs 3+1=43 + 1 = 4 accesses instead of 10.

For the primary index of 45 blocks, one more level of ⌈4568⌉=1\lceil \dfrac{45}{68} \rceil = 1 block is enough. That gives t=2t = 2 levels and 2+1=32 + 1 = 3 accesses. Compare this with the 1,500 average reads of a plain heap search.

The weakness of a static multilevel index is change. Inserting and deleting records shifts entries inside the sorted index levels, which is slow. The B-tree and B+ tree solve this by leaving free space in each node and splitting nodes only when they overflow.

Why it matters. The fan-out makes the number of levels grow like a logarithm with a big base. With a fan-out of 68, four levels already cover 68468^4, which is over 2 crore entries.

B-trees and B+ trees: structure, order and capacity

A B-tree of order pp is a balanced search tree stored one node per block. Each node holds up to p−1p - 1 sorted keys and up to pp pointers to children, and in a B-tree every key also carries a data pointer to its record. Every internal node except the root has at least ⌈p2⌉\lceil \dfrac{p}{2} \rceil children, the root has at least two unless it is a leaf, and all leaves sit at the same depth.

A B+ tree changes two things. Data pointers appear only in the leaves, so internal nodes hold just keys and child pointers and therefore more of them. The leaves are also linked left to right in key order, so a range query finds its first key and then walks along the leaves.

Worked example: finding the order. Let a block be B=512B = 512 bytes, a key V=9V = 9 bytes, a data pointer Pr=7P_r = 7 bytes and a tree pointer P=6P = 6 bytes. A B-tree node holds pp tree pointers and p−1p - 1 key-and-data-pointer pairs:

6p+16(p−1)≤512  ⇒  22p≤528  ⇒  p=246p + 16(p - 1) \le 512 \;\Rightarrow\; 22p \le 528 \;\Rightarrow\; p = 24

A B+ tree internal node holds only pp tree pointers and p−1p - 1 keys, so 6p+9(p−1)≤5126p + 9(p - 1) \le 512, which gives 15p≤52115p \le 521 and p=34p = 34. A B+ tree leaf holds pleafp_{\mathit{leaf}} key-and-data-pointer pairs and one next-leaf pointer, so 16pleaf+6≤51216p_{\mathit{leaf}} + 6 \le 512 gives pleaf=31p_{\mathit{leaf}} = 31. The B+ tree's internal fan-out of 34 beats the B-tree's 24, which is why databases prefer it.

Worked example: capacity. A full B+ tree with this order and three levels has a root with 34 children, then 34×34=115634 \times 34 = 1156 leaves. Each leaf holds 31 keys, so the tree indexes up to 1156×31=35,8361156 \times 31 = 35{,}836 records, while any search reads only 3 index blocks.

For a B-tree of order pp with hh levels, a full tree holds ph−1p^h - 1 keys. The emptiest legal tree has one key in the root and ⌈p2⌉−1\lceil \dfrac{p}{2} \rceil - 1 keys in every other node, which gives 2⌈p2⌉h−1−12\lceil \dfrac{p}{2} \rceil^{h - 1} - 1 keys. For p=5p = 5 and h=3h = 3, the range is from 2⋅32−1=172 \cdot 3^2 - 1 = 17 up to 53−1=1245^3 - 1 = 124 keys.

Why it matters. Order and capacity questions appear in GATE almost every year. The B+ tree's leaf links and larger fan-out explain why nearly every database index is a B+ tree.

Insertion and node splits

How does a B+ tree stay balanced as records arrive? Every new key goes into the correct leaf. If the leaf has room, the work ends there; if it overflows, the leaf splits into two and a key is passed up to the parent.

Worked example. Let every node hold at most 3 keys, and insert 10, 20, 30, 40, 50 and 60 into an empty B+ tree. The first three keys fill one leaf [10 20 30]. The key 40 makes it overflow, so it splits into [10 20] and [30 40], and a copy of 30, the first key of the right leaf, goes up to form a new root [30].

Next, 50 fits into the right leaf, giving [30 40 50]. The key 60 overflows it again, so it splits into [30 40] and [50 60], and a copy of 50 goes up, making the root [30 50]. The tree now has 2 splits, one root and three linked leaves [10 20], [30 40] and [50 60].

A split of an internal node, and any split in a B-tree, works differently: the middle key moves up and does not stay below. Insert the same keys into a B-tree with at most 3 keys per node, and the overflow [10 20 30 40] sends 30 up, leaving [10 20] and [40]. After 50 and 60 the leaves are [10 20] and [40 50 60], so the key 30 lives only in the root.

Splits can climb all the way up. When the root itself splits, a new root is created above it, and this is the only way the height of the tree grows. That is why all leaves always stay at the same level.

Why it matters. GATE gives a key sequence and asks for the number of splits or the final root. Draw each step and copy or move the key exactly as the tree type demands.

Descriptive lens: how written papers ask this

Written papers for IT officer posts, state Assistant Programmer exams and university tests ask about storage and indexing in four ways:

  1. Compare file organisations, such as heap, sorted and hashed files, for insertion, equality search and range search.
  2. Classify and explain indexes: primary, clustering and secondary, and dense against sparse, each with a diagram.
  3. Construct a B-tree or B+ tree by inserting a given key sequence, and sometimes show a deletion.
  4. Justify a design choice, such as why databases prefer B+ trees to binary search trees or to hashing.

Practice questions (our own, not previous-year questions).

  1. Distinguish between dense and sparse indexes. Why must a sparse index be built on the field the file is sorted on? (10 marks)
  2. Insert the keys 5, 15, 25, 35, 45, 55 and 65 into an empty B+ tree whose nodes hold at most 3 keys, and show the tree after each split. (15 marks)
  3. Explain why a B+ tree is preferred over a B-tree for database indexes, with a calculation of the order for a 4 KB block. (10 marks)

Answer plan for question 3.

  • State that each node of either tree is stored in one disk block, so the fan-out decides the height and the number of block reads.
  • Show that a B-tree node must store a data pointer with every key, while a B+ tree internal node stores only keys and child pointers.
  • Compute both orders from the inequalities for an assumed key and pointer size, and show that the B+ tree's order is larger.
  • Conclude that the larger fan-out gives a shorter tree, so a search reads fewer blocks.
  • Add that the linked leaves of a B+ tree make range queries and full ordered scans efficient, which a B-tree cannot match.

Quick revision

  • The cost of a file operation is the number of block accesses.
  • The blocking factor is ⌊BR⌋\lfloor \dfrac{B}{R} \rfloor and the number of blocks is ⌈rbfr⌉\lceil \dfrac{r}{\mathit{bfr}} \rceil.
  • A heap search costs b2\dfrac{b}{2} blocks on average, a sorted search costs ⌈log⁡2b⌉\lceil \log_2 b \rceil, and a hashed equality search costs about one block.
  • A primary index is sparse with one entry per block, a clustering index has one entry per distinct value, and a secondary index on a key is dense.
  • A file has at most one primary or clustering index but can have many secondary indexes.
  • A single-level index search costs ⌈log⁡2bi⌉+1\lceil \log_2 b_i \rceil + 1 accesses, and a multilevel index search costs t+1t + 1 with t=⌈log⁡for1⌉t = \lceil \log_{\mathit{fo}} r_1 \rceil.
  • The B-tree order satisfies pP+(p−1)(V+Pr)≤BpP + (p - 1)(V + P_r) \le B, and the B+ tree internal order satisfies pP+(p−1)V≤BpP + (p - 1)V \le B.
  • B+ trees keep data pointers only in linked leaves, which gives a larger fan-out and fast range queries.
  • A B-tree of order pp with hh levels holds at most ph−1p^h - 1 keys.
  • In a B+ tree leaf split the key is copied up, in a B-tree split it moves up, and height grows only at the root.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 14 answered

  1. The cost of a database file operation is usually measured as:

  2. Block size 1,024 bytes, record size 100 bytes, unspanned, 30,000 records. The number of blocks is:

  3. A file sorted on the search key has 3,000 blocks. Binary search needs at most how many block accesses?

  4. Which file organisation is poorest for a range query such as salary between 40 and 60?

  5. A primary index on an ordered file has one entry per:

  6. How many primary or clustering indexes can one data file have?

  7. Index entries are 15 bytes, blocks are 1,024 bytes, and a dense secondary index covers 30,000 records. How many block accesses does a search need with binary search on this single-level index?

  8. A first-level index has 442 blocks and the fan-out is 68. A multilevel index search costs how many block accesses including the data block?

  9. In a B-tree of order pp, every internal node other than the root has at least how many children?

  10. Which feature belongs to a B+ tree but not to a B-tree?

  11. Block 512 bytes, key 9 bytes, tree pointer 6 bytes. The order pp of a B+ tree internal node is:

  12. Block 512 bytes, key 9 bytes, data pointer 7 bytes, tree pointer 6 bytes. The order pp of a B-tree is:

  13. The maximum number of keys in a B-tree of order 5 with 3 levels is:

  14. Keys 10, 20, 30, 40, 50, 60 are inserted into an empty B+ tree whose nodes hold at most 3 keys, with the leaf split into two keys left and two right. The final root is:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

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

A dense index has an entry for every record, while a sparse index has entries for only some, usually one per block, and needs a sorted file.

Why is a B+ tree preferred over a B-tree?

Its internal nodes hold no data pointers, so more keys fit per block and the tree is shorter, and its linked leaves make range queries fast.

How is the order of a B+ tree found?

For an internal node, take the largest p with p·P + (p − 1)·V ≤ B, where P is the pointer size, V the key size and B the block size.

How many primary indexes can a file have?

At most one primary or clustering index, because a file can be physically sorted on only one field, but it can have many secondary indexes.

More in this module: Databases

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Database Systems (IIT Kanpur, 106104135): file organisation, indexing, B and B+ trees