Module 10 · 15 min read · Free chapter
File organisation and indexing
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.
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 and record size , and no record allowed to cross a block boundary, it is
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 bytes, bytes and the file hold records. Then records per block, and each block wastes bytes. The file needs blocks.
Why it matters. Every later numerical in this chapter starts from and . 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 , worst | Bulk loading, small tables |
| Sorted on a field | Costly: shift records | 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 blocks from the last section. A heap file needs on average 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 reads, since .
A hashed file uses a hash function, such as , to send each record to one of buckets. To find a key we compute 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 records in blocks of 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 .
A primary index needs one entry per data block, so 3,000 entries in index blocks. Binary search on it takes 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 blocks. Binary search takes 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 , count the entries, divide, and take .
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 blocks of the level below, and this number is called the fan-out, written . With entries in the first level, the number of levels is
A search reads one block per level and then the data block, so it costs 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 blocks, and the third level needs block. So levels, and a search costs accesses instead of 10.
For the primary index of 45 blocks, one more level of block is enough. That gives levels and 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 , which is over 2 crore entries.
B-trees and B+ trees: structure, order and capacity
A B-tree of order is a balanced search tree stored one node per block. Each node holds up to sorted keys and up to 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 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 bytes, a key bytes, a data pointer bytes and a tree pointer bytes. A B-tree node holds tree pointers and key-and-data-pointer pairs:
A B+ tree internal node holds only tree pointers and keys, so , which gives and . A B+ tree leaf holds key-and-data-pointer pairs and one next-leaf pointer, so gives . 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 leaves. Each leaf holds 31 keys, so the tree indexes up to records, while any search reads only 3 index blocks.
For a B-tree of order with levels, a full tree holds keys. The emptiest legal tree has one key in the root and keys in every other node, which gives keys. For and , the range is from up to 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:
- Compare file organisations, such as heap, sorted and hashed files, for insertion, equality search and range search.
- Classify and explain indexes: primary, clustering and secondary, and dense against sparse, each with a diagram.
- Construct a B-tree or B+ tree by inserting a given key sequence, and sometimes show a deletion.
- 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).
- Distinguish between dense and sparse indexes. Why must a sparse index be built on the field the file is sorted on? (10 marks)
- 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)
- 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 and the number of blocks is .
- A heap search costs blocks on average, a sorted search costs , 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 accesses, and a multilevel index search costs with .
- The B-tree order satisfies , and the B+ tree internal order satisfies .
- B+ trees keep data pointers only in linked leaves, which gives a larger fan-out and fast range queries.
- A B-tree of order with levels holds at most 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.