Sub topic 1
Disk Storage and Why Block Accesses Are the Cost Measure
Prerequisites
You should know arrays, linked lists, binary search, balanced search trees, logarithms, and the basic idea of a database record with a key field. You should also know that secondary storage, such as a disk or SSD, is read in fixed-size blocks.
The storage hierarchy
A database usually stores a table on secondary storage rather than entirely in main memory. Secondary storage is much larger, but accessing it is slower. The processor can perform many memory operations while waiting for one storage operation to finish.
A block is the fixed-size unit transferred between storage and main memory. For example, a system might use blocks of 4,096 bytes. A database request normally reads one or more complete blocks, even when it needs only one record inside a block.
The main performance measure is therefore the number of block accesses, also called I/O operations. A block access includes locating the block and transferring its contents to memory.
| Operation | Typical cost model |
|---|---|
| Compare values already in memory | Very small |
| Read one storage block | Dominant cost |
| Scan several consecutive blocks | Number of blocks read |
The exact time depends on the device and workload. For GATE-style questions, count block accesses unless the question explicitly supplies another cost model.
Why blocks matter
Suppose a record has size 100 bytes and a block has size 1,000 bytes. With unspanned records, a record must fit completely inside one block. The blocking factor, written as bfr, is
\[ bfr = \left\lfloor \frac{B}{R} \right\rfloor \]
where B is the block size and R is the record size.
Here, bfr = floor(1000 / 100) = 10. A file containing 3,000 records therefore needs
\[ b = \left\lceil \frac{r}{bfr} \right\rceil = \left\lceil \frac{3000}{10} \right\rceil = 300 \]
data blocks.
The ceiling is important because a partially filled final block still requires one complete block access. If records may cross block boundaries, the file can use space more efficiently, but reading one record may require two blocks. That is the trade-off between spanned and unspanned records.
Counting accesses
A heap file stores records in no particular order. To find a record by key, a search may inspect every block. If the matching record is equally likely to appear anywhere, the average cost is approximately b / 2 block accesses. The worst case is b.
A sorted file supports binary search over blocks. Its search cost is approximately
\[ \lceil \log_2 b \rceil \]
block accesses, assuming the block directory or boundaries can be used directly. Insertions are more expensive because records may need to move to preserve order.
An index reduces the number of data blocks that must be examined. The index itself occupies blocks, so its access cost must also be counted.
Exercise
A file has 2,501 fixed-length records. Each block stores 10 records. How many data blocks are required?
Show answer
The file requires ceil(2,501 / 10) = 251 blocks. The last block contains one record.