Sub topic 2
Records, Blocking Factor, and Number of Blocks
Assume you know basic database records, integer arithmetic, and that a disk reads data in fixed-size blocks. This chapter explains how to calculate how many records fit in a block and how many block accesses a file needs.
Records and blocks
A record is one row of a table. A record contains fields such as an employee ID, name, and salary. A disk block is a fixed-size region transferred between storage and main memory in one operation.
Let:
Bbe the block size in bytes.Rbe the record size in bytes.rbe the total number of records.bfrbe the blocking factor, or number of records per block.bbe the number of data blocks.
For fixed-length records, the blocking factor depends on whether records may cross block boundaries.
Unspanned organization
In an unspanned organization, one record must fit entirely inside one block. Any unused bytes at the end of a block cannot hold part of the next record.
The blocking factor is:
bfr = floor(B / R)
The number of blocks is:
b = ceil(r / bfr)
For example, let B = 1024 bytes, R = 100 bytes, and r = 30,000.
bfr = floor(1024 / 100) = 10b = ceil(30,000 / 10) = 3,000
Each block uses 1,000 bytes for records, so 24 bytes remain unused.
Spanned organization
In a spanned organization, a record may cross a block boundary. This uses space more efficiently, but reading or writing one record may require two block accesses.
Ignoring record headers and boundary metadata, the approximate number of blocks is:
b = ceil(r × R / B)
For the same values:
b = ceil(30,000 × 100 / 1024) = 2,930
The exact storage system may reserve bytes for a slot directory or record metadata, so use the formula specified by the question.
Comparing the two organizations
| Organization | Record crosses a block? | Main advantage | Main cost |
|---|---|---|---|
| Unspanned | No | Simple record access | Wasted space |
| Spanned | Yes | Better space utilization | A record may need multiple block reads |
For unspanned records, calculate the blocking factor before calculating the number of blocks. Do not use B / R without taking the floor. A partial record cannot occupy a slot.
import math
def unspanned_blocks(block_size, record_size, record_count):
blocking_factor = block_size // record_size
if blocking_factor == 0:
raise ValueError("The record does not fit in one block")
block_count = math.ceil(record_count / blocking_factor)
return blocking_factor, block_count
bfr, blocks = unspanned_blocks(1024, 100, 30000)
print("blocking factor:", bfr)
print("data blocks:", blocks)A reliable calculation method
- Write down
B,R, andr. - Identify whether records are spanned or unspanned.
- For unspanned records, calculate
floor(B / R). - Divide the record count by the blocking factor.
- Round the block count upward, because a partial final block still occupies one block.
Exercise
An unspanned file contains 12,001 records. Each record is 210 bytes, and each block is 1,000 bytes. What are the blocking factor and number of blocks?
Show answer
The blocking factor is floor(1,000 / 210) = 4. The number of blocks is ceil(12,001 / 4) = 3,001.