Sub topic 2
Records, blocking factor and number of blocks
This chapter assumes you know that a disk is read and written in fixed-size blocks, and that you are comfortable with floor and ceiling arithmetic. It turns a file of records into a count of blocks. Every later cost formula, from heap-file scans to B+ tree heights, starts from that count.
From records to blocks
A record is a fixed collection of fields, such as one row of a table. In the fixed-length case, the record size R is the sum of the field sizes, in bytes. Some textbook problems add a byte for a deletion marker or a header. If the question lists such a byte, include it. If it does not, do not invent one.
A block is the unit the disk transfers. Its size B is typically 512 bytes to a few kilobytes. The file has r records. We ask two questions:
- How many records fit in one block?
- How many blocks does the whole file need?
The answers depend on whether a record may cross a block boundary.
Unspanned organisation
In an unspanned file, every record lies completely inside one block. When the next record does not fit in the space left, it starts a new block. The unused tail of the block is wasted.
The number of records per block is the blocking factor:
bfr = floor(B / R)
The file then needs:
b = ceil(r / bfr)
The wasted space per block is B − bfr × R bytes.
For B = 1024, R = 100 and r = 30000:
- bfr = floor(1024 / 100) = 10
- b = ceil(30000 / 10) = 3000 blocks
- waste = 1024 − 1000 = 24 bytes per block
Use the floor for bfr and the ceiling for b. If you swap them, you get an impossible layout: either a fractional record in a block, or a file with too few blocks.
If R > B, then bfr = 0 and an unspanned layout is impossible. The file must be spanned.
Spanned organisation
In a spanned file, a record may start in one block and continue in the next. No space is wasted, apart from the unused end of the last block. To find a spanned record, you may need to read two blocks. Each block also needs a pointer to the next block that holds the rest of a split record.
With the pointer overhead ignored, as most GATE questions do, the file needs:
b = ceil(r × R / B)
For the same example, r × R = 3,000,000 bytes. Dividing by 1024 gives 2929.7, so b = 2930 blocks. This is 70 blocks fewer than the unspanned layout.
The average number of records per block is r / b. It is no longer a whole number, so the formula bfr = floor(B / R) does not describe a spanned file. Many questions that specify a spanned organisation use only the total-bytes formula above.
| Property | Unspanned | Spanned |
|---|---|---|
| Record may cross a block boundary | No | Yes |
| Blocking factor | floor(B / R) | Not a whole number in general |
| Blocks for r records | ceil(r / bfr) | ceil(r × R / B) |
| Wasted space | B − bfr × R in every block | Only in the last block |
| Reading one record | Exactly one block | One or two blocks |
| Works when R > B | No | Yes |
Computing it in code
The functions below implement both formulas. Integer arithmetic is used for the spanned ceiling to avoid floating-point surprises on large files.
import math
def unspanned(B, R, r):
bfr = B // R
if bfr == 0:
raise ValueError("record is larger than a block; the file must be spanned")
return {
"bfr": bfr,
"blocks": math.ceil(r / bfr),
"waste_per_block": B - bfr * R,
}
def spanned(B, R, r):
# ceiling of (r * R) / B using integers only
return -(-(r * R) // B)
print(unspanned(1024, 100, 30000))
print(spanned(1024, 100, 30000))The output is:
{'bfr': 10, 'blocks': 3000, 'waste_per_block': 24}
2930Building R from fields
Questions rarely give R directly. They list the fields, and you add them up.
Suppose a student record has these fields: roll number (4 bytes), name (30 bytes), department code (2 bytes), address (50 bytes) and phone (14 bytes). Then R = 4 + 30 + 2 + 50 + 14 = 100 bytes. This is the record used in the example above.
Keep these points in mind:
- Units must match. If B is given in kilobytes and R in bytes, convert first. For these problems, 1 KB is 1024 bytes unless the question says otherwise.
- Fixed-length records give an exact R. For variable-length records, a question usually gives an average or a maximum, and the answer is an estimate.
- Only the data counts here. Block headers, if the question mentions one, reduce the usable space, so use B minus the header size in place of B.
Why the block count is the cost
A disk read brings in a whole block, so reading one record costs one block access. It makes no difference whether you want 1 record or all 10 records in that block. Time spent searching inside the block in memory is negligible next to the disk transfer. This is why the chapters that follow measure cost in block accesses, and why the block count b is the starting number for those costs.
Two consequences follow:
- Scanning the whole file costs b block accesses. Making records smaller or blocks larger lowers b directly.
- Bigger bfr means fewer blocks, and so fewer accesses for the same file.
The same arithmetic returns later for index entries. An index entry is a small record, with a key of size V and a pointer of size P. The index blocking factor is floor(B / (V + P)), which is how the code in a later chapter finds the index size.
Worked example: both layouts side by side
A file has r = 1000 records of R = 150 bytes each. The block size is B = 512 bytes.
Unspanned:
- bfr = floor(512 / 150) = 3
- b = ceil(1000 / 3) = 334 blocks
- waste per block = 512 − 450 = 62 bytes
Spanned:
- Total bytes = 1000 × 150 = 150,000
- b = ceil(150000 / 512) = ceil(292.97) = 293 blocks
The spanned layout saves 41 blocks, about 12%. The price is that some records need two block reads. When the record size divides the block size almost evenly, such as R = 100 in a block of 1024 with only 24 bytes of waste, the saving is small. When the waste per block is large, as here, the saving is larger.
Edge cases to check
- Exact fit. If R divides B exactly, there is no waste, and both layouts give the same count when r × R is also a multiple of B.
- Partly full last block. The ceiling in both formulas covers the last block, which is usually not full.
- Small files. If r is smaller than bfr, then b = 1.
- Record larger than block. An unspanned layout cannot hold it, so a question that says "unspanned" with R > B has no valid answer.
Exercise
A file has 5000 fixed-length records of 120 bytes each. The block size is 1000 bytes. Compute the blocking factor, the number of blocks for an unspanned organisation, and the number of blocks for a spanned organisation (ignore pointer overhead).
Show answer
bfr = floor(1000 / 120) = 8. Unspanned blocks = ceil(5000 / 8) = 625. Spanned blocks = ceil(5000 × 120 / 1000) = ceil(600) = 600. The spanned file saves 25 blocks, because each unspanned block wastes 1000 − 960 = 40 bytes.
Exercise
Records are 300 bytes and blocks are 256 bytes. Can the file be stored unspanned? If not, how many blocks does a spanned file of 100 records need?
Show answer
No. bfr = floor(256 / 300) = 0, so no record fits inside a block. Spanned, the file needs ceil(100 × 300 / 256) = ceil(117.19) = 118 blocks.