Sub topic 3
File Organisation: The Options and What They Trade Off
This chapter assumes that you understand records, disk blocks, blocking factor, binary search, and basic database keys. The main measure is block access: reading or writing one fixed-size block between storage and memory. A method that needs fewer block accesses is usually faster than one that scans the same data record by record.
What file organisation controls
File organisation is the way records are arranged in a data file. The arrangement affects four common operations:
- Searching for one record.
- Finding all records in a key range.
- Inserting a new record.
- Deleting or updating an existing record.
No organisation is best for every operation. The correct choice depends on the most common search condition and the update workload.
Heap files
A heap file stores records in no particular order. A new record can usually be placed in any block with free space, often at the end of the file.
Heap files are useful when:
- Records arrive frequently.
- Searches are usually full scans.
- No single field provides the main access path.
- An index handles important searches separately.
To find a record by a non-indexed field, the system may need to inspect every data block. If the file contains \(b\) blocks and the target is equally likely to appear anywhere, a successful linear search examines about \(b/2\) blocks on average. An unsuccessful search examines all \(b\) blocks.
Insertion is cheap because the system does not preserve an order. Deletion can leave unused space, and updates that increase a record's size may require moving it.
Sorted files
A sorted file stores records in order by a search field, such as student ID. Binary search can locate the approximate block containing a key, requiring about \(\lceil \log_2 b\rceil\) block accesses for \(b\) blocks.
Sorted files are useful when:
- Equality searches use the ordering field.
- Range queries are common.
- Results must be returned in order.
- Sequential processing is important.
The main cost is maintenance. Inserting a record may require shifting records or creating overflow blocks. Deleting records can create gaps. Frequent updates may therefore make a sorted file expensive to maintain.
Hashed files
A hashed file uses a hash function to map a search key to a bucket, which is a block or group of blocks assigned to that hash value. With a good hash function and moderate occupancy, equality searches are fast on average.
Hashing is a strong choice for queries such as:
WHERE account_id = 48192It is poor for range queries such as “all account IDs from 48000 through 49000,” because nearby keys do not necessarily map to nearby buckets. Collisions, where different keys map to the same bucket, require overflow handling.
| File organisation | Equality search | Range search | Insertion | Main weakness |
|---|---|---|---|---|
| Heap | Slow without an index | Slow | Cheap | Linear scans |
| Sorted | Good on ordering field | Good | Expensive | Reorganisation |
| Hashed | Good on hash field | Poor | Usually good | Not ordered |
Choosing among the options
Use a heap file when write simplicity matters. Use a sorted file when range scans or ordered processing dominate. Use a hashed file when equality lookups dominate and range access is rare. In practice, indexes often provide several access paths over the same underlying file.
Exercise
A file receives many new records and is searched occasionally by scanning all records. Which organisation is the simplest choice, and why?
Show answer
A heap file is the simplest choice because insertion does not require preserving an order. Occasional full scans do not justify the maintenance cost of sorting the file.