Module 8: Storage & Indexing · Lecture 35 · Week 13
File Organization & Heap Files
How relations are stored on disk; heap, sorted, and clustered files; the cost of insert, delete, and scan
Learning Objectives
- Describe the three main file organizations: heap, sorted, and clustered
- Analyze I/O costs for insert, delete, search, and sequential scan under each organization
- Explain the free space directory and why it matters for heap file management
- Explain why sorted files are rarely used as the primary organization and what replaces them
1. What Is a File Organization?
A file organization finds how records (tuples) are laid out across the pages of a relation's physical storage file. The choice of file organization strongly affects the cost of different operations. There is no single best organization; the right choice depends on the workload (reads vs. writes, point lookups vs. range scans).
The three core organizations are:
- Heap File: records in no particular order; new records appended to any page with free space
- Sorted File: records ordered by one or more key attributes
- Clustered File: records ordered according to an index key; used by clustered B+ tree indexes
2. Heap Files
A heap file stores records in an arbitrary order: wherever there is free space. It is the simplest and most common base organization for RDBMS tables.
2.1 Free Space Management
The heap file manager must track which pages have free space to accommodate new inserts. Two approaches:
Linked List of Free Pages
Maintain two linked lists: one for pages with free space, one for full pages. Simple but slow: to find a page with enough space for a large record, you may traverse many nodes.
Page Directory (Preferred)
A header page (or set of directory pages) stores one entry per data page: (pageId, freeByteCount). To insert a record, scan the directory to find a page with enough space. Directories are small and fit in the buffer pool, making this fast and low-overhead. Most production DBMSs use this approach.
2.2 Heap File Operations and I/O Cost
| Operation | I/O Cost | Notes |
|---|---|---|
| Sequential scan (all tuples) | B | Read all B pages; always full pass |
| Search (equality on non-key) | B/2 (avg), B (worst) | Must scan all pages; stop at first match if key |
| Insert | 2 (read + write one page) | Find page with space via directory; write record; update directory |
| Delete by RID | 2 (read + write one page) | Mark slot as empty; may trigger page compaction |
| Delete by value (non-key) | B + matching writes | Full scan to find all matching records, then delete each |
Where B = number of pages in the relation. The insert cost is O(1) because the directory tells us immediately where free space is; the full-scan search cost is O(B) because there is no ordering to exploit.
2.3 Record Deletion Strategies
When a record is deleted from a heap file, the space in the page must be reclaimed. Two strategies:
- Immediate compaction: on deletion, shift remaining records in the page to close the gap. Simple but causes record addresses (RIDs) to change, which invalidates index entries pointing to those records.
- Tombstone / mark deleted: mark the slot as deleted but do not move records. RIDs remain valid. Periodically run a VACUUM/REPACK operation to reclaim space. PostgreSQL uses this approach (called dead tuples).
3. Sorted Files
A sorted file keeps all pages in order of a specific sort key (e.g., student_id). This enables binary search for point queries and efficient range scans.
3.1 Sorted File Operations and I/O Cost
| Operation | I/O Cost | Notes |
|---|---|---|
| Sequential scan (all tuples) | B | Same as heap; all pages must be read |
| Search by sort key (equality) | log₂(B) | Binary search on pages |
| Range scan on sort key | log₂(B) + pages_in_range | Find first page, then sequential read |
| Insert | B (worst case) | Must maintain sort order; shifting pages is expensive |
| Delete | B/2 (avg) + shift cost | Find record (cheap) but shifting remaining records is expensive |
Why Sorted Files Are Rarely Used Directly
Although sorted files offer O(log B) search, inserts and deletes are O(B) in the worst case because maintaining sort order requires shifting pages. For a large table, this is prohibitively expensive. The solution is to use a B+ tree index which provides O(log B) search AND O(log B) inserts/deletes by organizing pages into a balanced tree. We study B+ trees in L36–L37.
4. Clustered vs. Unclustered Files
A file is clustered with respect to an index if the physical order of the pages on disk roughly matches the order of the index. A file is unclustered if the physical order is independent of the index order.
Why Clustering Matters for Range Scans
If a relation is clustered on (department_id), then a query like WHERE department_id = 5 can read a contiguous run of pages from disk: one seek, then sequential transfer. If unclustered, the same query may require one disk seek per matching record, potentially visiting many different pages scattered across the disk. For large tables, clustering can reduce range-scan I/O by orders of magnitude.
4.1 Clustering Constraint
A relation can only be physically clustered on one index at a time, because there is only one physical ordering of pages on disk. In SQL Server this is the clustered index; in PostgreSQL it is controlled with the CLUSTER command; in MySQL InnoDB, the primary key always defines the clustered organization.
5. Comparing File Organizations
| Organization | Sequential Scan | Equality Search | Range Scan | Insert | Delete |
|---|---|---|---|---|---|
| Heap | B | B/2 | B | 2 | B/2 + 1 |
| Sorted | B | log₂B | log₂B + pages | B | B |
| Clustered B+ tree | B | log_d(B) | log_d(B) + pages | log_d(B) | log_d(B) |
The clustered B+ tree is clearly superior for mixed workloads. It matches sorted file performance for scans and equality searches, lowering insert/delete cost from O(B) to O(log B).
6. Column-Oriented Storage (Brief Introduction)
Traditional row-oriented (N-ary) storage stores all attributes of a tuple together in one page. Column-oriented (columnar) storage stores each attribute in a separate file/column stripe. This layout is strongly efficient for analytical queries that read only a few columns from millions of rows:
- Row-store advantage: point queries, updates, inserts (entire tuple in one place)
- Column-store advantage: aggregations, projections on few columns, better compression (homogeneous data in a column compresses well)
Modern systems like Snowflake, BigQuery, and PostgreSQL's cstore extension use columnar storage for analytical (OLAP) workloads. We revisit this in L45.
7. Summary
- Heap files store records in any order; insert is O(1) with a page directory, but search is O(B)
- Sorted files enable O(log B) search but O(B) insert; B+ trees solve this tradeoff
- Clustered files match physical page order to index order, enabling efficient range scans with minimal seeks
- A relation can be clustered on at most one index; secondary indexes are unclustered