Quick answer: A database file is stored as blocks of records, and the DBMS reads and writes whole blocks. In unspanned organization every record sits entirely inside one block, which is simple but can waste space. In spanned organization a record can continue into the next block, which wastes no space but can cost extra accesses. For an unspanned file the blocking factor is ⌊(B − H) / R⌋, and N records need ⌈N / BF⌉ blocks. Without an index, finding a record takes up to n block accesses in an unordered (heap) file, or ⌈log₂ n⌉ with binary search on a file sorted by the search field.
These ideas feed into every indexing and B+ tree calculation in GATE. This topic is part of the File Organization and Indexing chapter.
Files, blocks and records
- Database: a collection of files (tables).
- File: a collection of blocks (pages). A block is the unit of transfer between disk and main memory.
- Block: a collection of records, possibly with a header.
Since disk access is far slower than memory access, I/O cost is measured as the number of blocks transferred.
Spanned vs unspanned organization
| Point | Spanned | Unspanned |
|---|---|---|
| Rule | A record may span more than one block | A whole record must fit in one block |
| Advantage | No wasted space at block ends | Lower access cost; simpler to manage |
| Disadvantage | Extra accesses for records that span blocks | May waste space at the end of each block |
| Suits | Variable-length or large records | Fixed-length records |
Blocking factor
The blocking factor (BF) is the number of records per block. For unspanned organization:
For spanned organization, the average number of records per block is (B − H) / R, without the floor.
Here B is the block size, H the block header size (0 unless given) and R the record size. The number of blocks the file needs is:
Worked example: spanned vs unspanned
1000 records of 300 bytes each, 1024-byte blocks, no header.
Step 1: Unspanned. BF = ⌊1024 / 300⌋ = 3, so each block wastes 1024 − 900 = 124 bytes. Blocks = ⌈1000 / 3⌉ = 334.
Step 2: Spanned. Total data = 1000 × 300 = 300,000 bytes. Blocks = ⌈300,000 / 1024⌉ = 293.
Spanned storage saves 41 blocks here, at the cost of some records being split across two blocks.
Common file organizations
| Organization | How records are placed | Search on the key | Insertion |
|---|---|---|---|
| Heap (unordered) | Anywhere, usually at the end | Linear: up to n blocks (about n/2 on average for a key) | Cheap (append) |
| Sequential (sorted) | Sorted on an ordering field | Binary search: ⌈log₂ n⌉ | Costly (must keep order; overflow areas) |
| Hashed | Bucket chosen by a hash function on the key | About 1 block, plus any overflow chain | Cheap unless buckets overflow |
About hashing: static hashing uses a fixed number of buckets, so heavy insertion builds long overflow chains. Dynamic schemes such as extendible and linear hashing grow the directory or the buckets as needed. Hashing is very fast for equality searches but doesn't help range queries.
Access cost without an index
For a file of n blocks:
- Search on an unordered field: n block accesses in the worst case, since the record may be in the last block or missing.
- Search on the ordering field: ⌈log₂ n⌉ block accesses with binary search.
Worked example
10,000 records of 100 bytes each in 4096-byte blocks, no header.
Step 1: BF = ⌊4096 / 100⌋ = 40.
Step 2: Blocks n = 10,000 / 40 = 250.
Step 3: On an unordered field, up to 250 accesses. On the ordering field, ⌈log₂ 250⌉ = 8 accesses.
Indexes cut these costs further. See indexing in DBMS.
Fixed vs variable-length records
- Fixed-length records are easy to locate, because record i starts at a known offset. They suit unspanned storage.
- Variable-length records (VARCHAR fields, optional fields) use slotted pages or delimiters and often use spanned storage.
Summary table
| Formula or case | Value |
|---|---|
| Unspanned BF | ⌊(B − H) / R⌋ |
| Number of blocks | ⌈N / BF⌉ |
| Wasted bytes per block (unspanned) | (B − H) − BF × R |
| Heap file search | Up to n blocks |
| Sorted file search on the key | ⌈log₂ n⌉ blocks |
| Hashed file equality search | About 1 block |
Exam tips
- Subtract the header from the block size before dividing by the record size.
- Floor for the blocking factor, ceiling for the number of blocks. Mixing these up is the most common slip.
- Check whether the question says spanned or unspanned. If it says nothing, GATE usually means unspanned.
Common mistakes
- Forgetting to subtract the header before dividing.
- Rounding the wrong way: no floor on the unspanned blocking factor, or no ceiling on the block count.
- Using binary search cost on an unordered field.
- Assuming hashing helps range queries. It doesn't.
Practice questions
Q1. Block size 2048 bytes, header 48 bytes, record size 150 bytes, unspanned. What is the blocking factor?
Answer: ⌊(2048 − 48) / 150⌋ = ⌊13.33⌋ = 13.
Q2. Using the same blocking factor, how many blocks do 1300 records need?
Answer: ⌈1300 / 13⌉ = 100.
Q3. A sorted file has 1000 blocks. What is the worst-case cost to find a record by its ordering key without an index?
Answer: ⌈log₂ 1000⌉ = 10 block accesses.
FAQs
What is the difference between spanned and unspanned organization?
In spanned organization a record can be split across two blocks. In unspanned organization each record must fit inside one block, which can leave unused space at the end of the block.
How do you calculate the blocking factor?
For an unspanned file, BF = ⌊(block size − header size) / record size⌋.
Why does binary search only work on sorted files?
Binary search needs the blocks sorted by the search field, so each comparison can throw away half of the remaining blocks.
When should hashing be used for file organization?
Use it when most queries are equality searches on a key. It does not support range queries efficiently.
Why is I/O cost counted in blocks?
The disk transfers whole blocks, and a disk access takes far longer than in-memory work, so the number of blocks read dominates the cost.
Conclusion
Work out the blocking factor and the number of blocks first, since every later cost depends on them. Then compare heap, sorted and hashed access costs. Continue with indexing in DBMS and B tree and B+ tree, or go back to the File Organization and Indexing chapter.