Sub topic 3
File organisation: the options and what they trade off
This chapter assumes you know how a file of fixed-length records maps onto disk blocks. That means the blocking factor bfr = floor(B / R) and the block count b = ceil(r / bfr). You should also know binary search and the idea that the cost of an operation is the number of block accesses. Later chapters treat each organisation in detail. Here the goal is to see the whole menu and know what each choice gives up.
What a file organisation decides
A file organisation is the rule for deciding which block holds which record. The rule controls how many blocks you must read to find a record, and how much work an insertion or deletion causes. No organisation is best at everything. Each makes some operations cheap by making others expensive.
Three base organisations matter for GATE:
- A heap file (unordered file) stores records in insertion order, usually appended to the last block.
- A sorted file (ordered or sequential file) keeps records physically ordered on an ordering field.
- A hashed file applies a hash function to a hash field, and the result picks the block (bucket).
An index is a separate structure that sits beside one of these files. Indexes come in later chapters. The organisation of the data file is the starting point.
The operations that get compared
Five operations decide the trade-offs:
- Equality search on the key, such as
ID = 4711. - Range search, such as
ID BETWEEN 4000 AND 5000. - Insertion of a new record.
- Deletion of a record.
- Full scan, which reads every record in any order.
Let b be the number of data blocks.
| Operation | Heap | Sorted (on search key) | Hashed (on search key) |
|---|---|---|---|
| Equality search | b/2 average, b worst | ceil(log2 b) | about 1 |
| Range search | b | ceil(log2 b) plus the blocks in the range | b (hash gives no order) |
| Insert | 1 read plus 1 write at the end | search plus shifting about b/2 blocks | about 1 read plus 1 write |
| Delete | search, then 1 write | search, then rewrite or use a deletion marker | about 1 read plus 1 write |
| Search on a non-ordering field | b/2 to b | b/2 to b | b |
The heap column is the baseline. The sorted column buys fast search by making insertion expensive. The hashed column makes equality lookups nearly free but gives up ordering.
Heap files
A heap file appends a new record to the last block, so insertion costs about two accesses: read the last block, then write it back. Searching has no shortcut. If the key exists, you read half the file on average. If it does not exist, you read all b blocks.
Deletion leaves a hole. Real systems mark the record as deleted and reclaim the space during reorganisation. Heaps are the right choice for bulk loading and for tables that are always scanned in full. Heaps are also the usual base for a table that carries secondary indexes.
Sorted files
If the file is sorted on the search field, binary search over blocks finds a record in ceil(log2 b) accesses. A range query finds the first match by binary search, then reads consecutive blocks. That is why sorted files are good at ranges.
Insertion is the weak point. The new record must go in its sorted position, so later records shift. On average half the file moves, which costs about b/2 reads and b/2 writes. Common mitigations are leaving free space in each block, keeping an unsorted overflow file that is merged in periodically, and using deletion markers.
A sorted file helps only for searches on the ordering field. A search on any other field still needs a linear scan.
Hashed files
A hashed file computes h(key) to get a bucket number, then reads that bucket. With few collisions, an equality search costs about one block access. Insertion and deletion are also close to constant cost.
The limits matter just as much:
- A range query gets no help, because hashing scatters adjacent keys. You scan all buckets.
- Many records can hash to one bucket, and then you follow overflow chains. This lengthens searches.
- A static hash table with a fixed bucket count degrades as the file grows. Dynamic schemes such as extendible and linear hashing exist to fix this.
Choosing between them
Real tables often face a mix of workloads, and that is what motivates indexes. An index lets one data file support fast search on a field without forcing the file itself into a particular order.
Computing the costs
This script applies the table to a concrete file. It uses the numbers from the blocking-factor chapter: B = 1024, R = 100, r = 30000. It also assumes a range query matches records spread over k consecutive blocks.
import math
def organisation_costs(B, R, r, k):
# B: block size, R: record size, r: number of records
# k: number of consecutive blocks covered by a range query
bfr = B // R
b = math.ceil(r / bfr)
search_sorted = math.ceil(math.log2(b))
return {
"heap": {
"equality_avg": b / 2,
"range": b,
"insert": 2,
},
"sorted": {
"equality": search_sorted,
"range": search_sorted + (k - 1),
"insert": search_sorted + b, # about b/2 reads + b/2 writes
},
"hashed": {
"equality": 1,
"range": b,
"insert": 2,
},
}
costs = organisation_costs(B=1024, R=100, r=30000, k=10)
for name, ops in costs.items():
print(name, ops)With bfr = 10 and b = 3000, the sorted file finds a key in 12 accesses, against 1500 on average for a heap. The same sorted file needs about 3012 accesses to insert a record. The heap needs 2.
Common errors
- Applying
ceil(log2 b)to a file that is not sorted on the field being searched. Binary search needs the ordering field. - Treating a hashed file as good for ranges. It is not.
- Forgetting that the cost of a sorted-file insert includes the search as well as the shifting.
- Using
b/2for an unsuccessful heap search. That case reads allbblocks. - Mixing up record count and block count. Costs are always in blocks.
Exercise
A file has 40000 fixed-length records of 128 bytes each, stored unspanned in blocks of 1024 bytes. The file is sorted on its key. (a) How many blocks does it use? (b) How many block accesses does an equality search on the key need in the worst case with binary search? (c) How many does an equality search on the key need on average if the same file is a heap and the key exists?
Show answer
(a) bfr = floor(1024 / 128) = 8, so b = ceil(40000 / 8) = 5000 blocks. (b) ceil(log2 5000) = 13 accesses, since 2^12 = 4096 is less than 5000 and 2^13 = 8192 is at least 5000. (c) b/2 = 2500 accesses on average.
Exercise
A table is loaded once from a feed and then queried only with the predicate order_date BETWEEN d1 AND d2. Which of heap, sorted on orderdate, or hashed on orderdate should you choose, and why?
Show answer
Sorted on order_date. A binary search finds the first matching block in ceil(log2 b) accesses, and the matching records then sit in consecutive blocks. A heap needs a full scan of b blocks, and a hashed file scatters adjacent dates across buckets, so it also needs a scan of every bucket. The one-time load means the high insertion cost of a sorted file is paid once.