IIT Madras Zanzibar · Teaching

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

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:

  1. Heap File: records in no particular order; new records appended to any page with free space
  2. Sorted File: records ordered by one or more key attributes
  3. 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.

Directory (header pages): ┌──────────┬───────────┐ │ Page ID │ Free bytes│ ├──────────┼───────────┤ │ 001 │ 3400 │ │ 002 │ 0 │ ← full │ 003 │ 1200 │ │ 004 │ 6800 │ └──────────┴───────────┘

2.2 Heap File Operations and I/O Cost

OperationI/O CostNotes
Sequential scan (all tuples)BRead 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
Insert2 (read + write one page)Find page with space via directory; write record; update directory
Delete by RID2 (read + write one page)Mark slot as empty; may trigger page compaction
Delete by value (non-key)B + matching writesFull 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:

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

OperationI/O CostNotes
Sequential scan (all tuples)BSame as heap; all pages must be read
Search by sort key (equality)log₂(B)Binary search on pages
Range scan on sort keylog₂(B) + pages_in_rangeFind first page, then sequential read
InsertB (worst case)Must maintain sort order; shifting pages is expensive
DeleteB/2 (avg) + shift costFind 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

OrganizationSequential ScanEquality SearchRange ScanInsertDelete
HeapBB/2B2B/2 + 1
SortedBlog₂Blog₂B + pagesBB
Clustered B+ treeBlog_d(B)log_d(B) + pageslog_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:

Modern systems like Snowflake, BigQuery, and PostgreSQL's cstore extension use columnar storage for analytical (OLAP) workloads. We revisit this in L45.

7. Summary