Quick answer: A database file is a set of blocks (pages) that hold records. Data moves between disk and memory one block at a time, so query cost is counted in block accesses (I/O cost). For an unspanned file the blocking factor is ⌊(block size − header) / record size⌋. Without an index, finding a record costs up to n block accesses on an unordered field and ⌈log₂ n⌉ on the ordering field. Indexes cut this cost: they can be dense or sparse; primary, clustering or secondary; single-level or multilevel. Multilevel indexes are usually built as B-trees or B+ trees, which pack many keys into each node so the tree stays shallow.
Many of GATE's numerical DBMS questions come from this chapter: blocking factor, number of index blocks, access cost, and the order of B and B+ tree nodes. This chapter page connects the three detailed notes.
Chapter overview
| Topic | Key ideas | Detailed notes |
|---|---|---|
| File organization | Files, blocks, records, spanned vs unspanned, blocking factor, access cost without an index | File organization and blocking factor |
| Indexing | Dense vs sparse; primary, clustering and secondary indexes; index blocking factor; multilevel I/O cost | Indexing in DBMS |
| B and B+ trees | Node structure, order calculation, minimum and maximum keys, height, B vs B+ tree | B tree and B+ tree |
Storage hierarchy
- A database is a collection of files (tables).
- A file is a collection of blocks (pages).
- A block is a collection of records, possibly with a header.
Key formulas
B is the block size, H the header, R the record size, K the key size and P the pointer size.
| Quantity | Formula |
|---|---|
| Blocking factor (unspanned) | ⌊(B − H) / R⌋ |
| Blocking factor (spanned, average) | (B − H) / R |
| Blocks in file | ⌈records / BF⌉ |
| Index blocking factor | ⌊(B − H) / (K + P)⌋ |
| Search on an unordered field, no index | Up to n blocks |
| Search on the ordering field, no index | ⌈log₂ n⌉ |
| Single-level index search | ⌈log₂(index blocks)⌉ + 1 |
| Multilevel index with k levels | k + 1 |
Types of index at a glance
| Index | Search key | Dense or sparse | Number per file |
|---|---|---|---|
| Primary | Ordering field that is a key | Usually sparse | At most one |
| Clustering | Ordering field that is not a key | Sparse (one entry per distinct value) | At most one |
| Secondary (key) | Non-ordering key field | Dense | Many |
| Secondary (non-key) | Non-ordering non-key field | Dense, or with an extra level of indirection | Many |
A file can have a primary index or a clustering index, not both, because it can be physically sorted on only one field.
B and B+ trees in brief
| Feature | B-tree | B+ tree |
|---|---|---|
| Record pointers | In every node | Only in leaves |
| Internal nodes | Keys, record pointers and child pointers | Keys and child pointers only (higher order) |
| Leaves linked | No | Yes |
| Range queries | Slow | Fast |
| Best-case search | Can stop at the root (2 block accesses including the data block) | Always goes to a leaf |
Worked example
Question: A file has 10,000 records of 100 bytes in 4096-byte blocks (unspanned, no header). How many blocks does it use, and what does it cost to find a record by its ordering key without an index?
Step 1: BF = ⌊4096 / 100⌋ = 40.
Step 2: Blocks = 10,000 / 40 = 250.
Step 3: Binary search costs ⌈log₂ 250⌉ = 8 block accesses, compared with up to 250 on an unordered field.
Summary table: what to compute first
| Question type | Start with | Then |
|---|---|---|
| File size | Data blocking factor | ⌈records / BF⌉ |
| Index size | Index blocking factor | Entries (records or blocks) ÷ index BF |
| Index access cost | Number of index blocks | ⌈log₂(blocks)⌉ + 1, or k + 1 for multilevel |
| Tree order | Node-size inequality | Largest integer that fits |
Exam tips
- Every question in this chapter starts from two numbers: the data blocking factor and the index blocking factor. Get those right first.
- Write down whether the index is dense (count records) or sparse (count data blocks) before dividing.
- For B and B+ tree order, use the formula for the exact node type asked: B-tree node, B+ internal node or B+ leaf.
Common mistakes
- Not taking the floor for the unspanned blocking factor, or the ceiling for the number of blocks.
- Leaving out the data block access. Index access cost is index levels + 1.
- Building a sparse index on an unordered field. Sparse indexes need a sorted file.
- Using the B-tree order formula for B+ tree internal nodes.
Practice questions
Q1. Block size 1024 bytes, record size 100 bytes, unspanned. What is the blocking factor?
Answer: ⌊1024 / 100⌋ = 10.
Q2. Index entries are 16 bytes (9-byte key + 7-byte pointer) in 1024-byte blocks. What is the index blocking factor?
Answer: ⌊1024 / 16⌋ = 64.
Q3. Why are B+ trees preferred over AVL trees for database indexes?
Answer: Each B+ tree node fills a disk block with many keys, so the tree is shallow and needs far fewer block accesses. An AVL node holds one key, so the tree is much taller.
FAQs
What is the blocking factor in DBMS?
It is the number of records that fit in one block. For unspanned organization it is ⌊(block size − header) / record size⌋.
What is the difference between a dense and a sparse index?
A dense index has an entry for every record or search-key value. A sparse index has entries for only some records, usually one per block.
Why is a B+ tree better than a B-tree for databases?
B+ trees keep record pointers only in leaves, so internal nodes hold more keys and the tree is shallower. The linked leaves also make range queries fast.
How many primary indexes can a table have?
At most one, because a file can be physically sorted on only one field.
Why is query cost measured in block accesses?
Disks transfer whole blocks, and each disk access is much slower than processing in memory, so the number of blocks read decides the cost.
Conclusion
Compute the data and index blocking factors carefully, then apply the access-cost formulas or the tree-order inequality. Read file organization, indexing and B tree and B+ tree next. For the next DBMS chapter, see Transactions and Concurrency Control.