Sub topic 1
Disk storage and why block accesses are the cost measure
Prerequisites
This chapter assumes you already know the following:
- Basic data structures: arrays, linked lists and binary search.
- Logarithms, ceiling and floor arithmetic.
- That a database table holds records, and that a key field identifies a record.
- That a disk is read in fixed-size blocks, not byte by byte.
Everything later in the tutorial (blocking factors, heap and sorted files, indexes, B-trees and B+ trees) rests on one idea. Moving a block between disk and memory costs far more than any computation done on it. This chapter explains the hardware reason for that and sets up the cost model the rest of the tutorial uses.
Where a database file lives
A database is too big for main memory, and memory loses its contents when power is lost. So the records live on secondary storage, usually a magnetic hard disk or a solid-state drive. The DBMS (database management system) copies the parts it needs into memory buffers, works on them there, and writes changes back.
The unit of transfer is the block (also called a page). A block is a fixed number of contiguous bytes, commonly 512 bytes to 8 KB in GATE problems. You cannot read one record from disk. You read the whole block that contains it.
Anatomy of a magnetic disk
A hard disk is a stack of circular platters spinning on one spindle. Each platter surface is divided into concentric tracks. Each track is divided into sectors, the smallest unit the hardware can address. A block is one or more consecutive sectors. The set of tracks at the same radius on all surfaces is a cylinder.
Disk capacity follows from simple multiplication:
capacity = surfaces × tracks per surface × sectors per track × bytes per sector
An arm moves read/write heads over the surfaces. All heads move together, so only one track per cylinder is read at a time without moving the arm.
The three parts of an access time
Reading a block takes three steps in sequence.
| Component | What happens | Typical size | Formula |
|---|---|---|---|
| Seek time | The arm moves the head to the right track | 3 to 12 ms | Given, or an average |
| Rotational latency | The platter turns until the block is under the head | 2 to 6 ms | On average half a rotation: 0.5 × 60000 / RPM ms |
| Transfer time | The block passes under the head | Under 0.1 ms | Block size / transfer rate |
Seek and rotation are mechanical movements. Transfer is only the time to read bytes that stream past the head. For a small block, the first two terms dominate by a factor of hundreds.
Random versus sequential access
The cost depends on where the next block is. If it is on a different track, you pay a full seek and rotational latency again. If it is the next block on the same track, the head is already in place and you pay only transfer time. This is why a scan of consecutive blocks is much cheaper per block than the same number of scattered reads.
The program below puts numbers on this. It uses a 7200 RPM disk, a 9 ms average seek, a 4096-byte block and a 100 MB/s transfer rate.
def block_times(seek_ms, rpm, block_bytes, rate_mb_s):
rotational_ms = 0.5 * 60000 / rpm # half a rotation on average
transfer_ms = block_bytes / (rate_mb_s * 1_000_000) * 1000
random_ms = seek_ms + rotational_ms + transfer_ms # one isolated block
return rotational_ms, transfer_ms, random_ms
rot, xfer, rand = block_times(seek_ms=9, rpm=7200, block_bytes=4096, rate_mb_s=100)
print(f"rotational latency: {rot:.3f} ms")
print(f"transfer time: {xfer:.5f} ms")
print(f"random access: {rand:.3f} ms")
print(f"ratio random/transfer: {rand / xfer:.0f}")The output is a rotational latency of 4.167 ms, a transfer time of 0.04096 ms and a random access of 13.208 ms. A random access costs about 322 times the transfer alone. A CPU executes millions of instructions in 13 ms, so any in-memory work on the block is negligible next to fetching it.
The cost model used in this tutorial
Because I/O dominates, we count the number of block accesses (block transfers) and ignore CPU time. Every cost formula in the next chapters has this form: "how many blocks must be read or written to do this operation?"
The model rests on a few conventions:
- Reading one block costs 1 access. Writing one block costs 1 access.
- Searching inside a block already in memory is free.
- A modification such as an insertion into a sorted file counts as a read plus a write of each block touched.
- Unless a question says otherwise, the file starts on disk and nothing is cached.
Why this model shapes the design of indexes
Two facts drive everything that follows.
- Blocks are the unit of cost. An ideal structure answers a query by touching very few blocks.
- Sequential access is cheap, random access is expensive. Structures that keep related records in neighbouring blocks serve range queries well.
Compare two ways to find one record in a file of b blocks. A linear scan reads about b/2 blocks on average if the record exists, and b if it does not. Binary search on a sorted file reads about ⌈log₂ b⌉ blocks. An index lets you reach the right block in a handful of accesses. A B+ tree typically needs 3 or 4 even for millions of records, because each node is one block and holds hundreds of keys.
A GATE-style calculation
GATE questions often give geometry instead of times. Consider this one.
A disk has 100 sectors per track, 512 bytes per sector, and rotates at 6000 RPM. The average seek time is 8 ms. A block is 4 sectors. What is the average time to read one block?
- One rotation takes 60000 / 6000 = 10 ms.
- Average rotational latency is half of that, 5 ms.
- A track holds 100 sectors and passes under the head in 10 ms, so one sector takes 0.1 ms. A 4-sector block takes 0.4 ms to transfer.
- Total is 8 + 5 + 0.4 = 13.4 ms.
If the next block is adjacent on the same track, its cost is only 0.4 ms, because the seek and rotational delay are not repeated.
Common mistakes
- Using a full rotation for latency. The average is half a rotation unless the question asks for the worst case. The worst case is a full rotation.
- Mixing units. RPM is per minute, and times are usually in milliseconds. Convert before you add.
- Mixing KB definitions. In GATE problems, block and record sizes are usually in powers of two (1 KB = 1024 bytes). Disk transfer rates may use powers of ten. Use the question's own convention and do not switch halfway.
- Counting CPU work. Comparing keys inside a buffered block adds nothing to the cost.
- Treating every access as random. Reading consecutive blocks of one file avoids repeated seeks. Check whether the question says the blocks are contiguous.
What comes next
The next chapter turns records into blocks. It computes how many records fit in a block (the blocking factor) and how many blocks a file needs. Those two numbers, along with the cost model above, are the inputs to every formula in this tutorial.
Exercise
A disk rotates at 10,000 RPM and has an average seek time of 5 ms. Blocks are 8192 bytes and the transfer rate is 200 MB/s (1 MB = 1,000,000 bytes). Find (a) the average time to read one randomly placed block, and (b) the time to read 100 consecutive blocks stored on one track, if the first needs a seek and rotational latency and the rest need only transfer time.
Show answer
Rotation takes 6 ms, so average latency is 3 ms. Transfer time is 8192 / 200,000,000 s = 0.04096 ms. (a) 5 + 3 + 0.04096 = 8.04096 ms, about 8.04 ms. (b) 8.04096 + 99 × 0.04096 = 8.04096 + 4.05504 = 12.096 ms. Reading the 100 blocks together costs about 1.5 random accesses, not 100.
Exercise
A file occupies 2,000 blocks. Under the block-access cost model, how many block accesses does a linear search read on average when the record is present, and how many when it is absent?
Show answer
On average about 2000 / 2 = 1000 blocks when the record is present, assuming it is equally likely to be anywhere. When it is absent, all 2,000 blocks must be read.