DBMS Notes
File organization refers to how data records are physically arranged and stored on disk. The choice of file organization directly impacts the performance of...
Overview
File organization refers to how data records are physically arranged and stored on disk. The choice of file organization directly impacts the performance of data retrieval, insertion, deletion, and update operations. Since disk I/O is the primary bottleneck in database systems (millions of times slower than memory access), minimizing the number of disk blocks read is the central optimization goal.
1. Heap File (Unordered File Organization)
Records are stored in no particular order. New records are appended to the end of the file (or inserted wherever free space exists).
| | Block 1 | [Employee 7][Employee 2][Employee 9] | |
| | Block 2 | [Employee 4][Employee 1][Employee 5] | |
| | Block 3 | [Employee 8][Employee 3][ free ] | |
| Operation | Cost | Explanation |
|---|---|---|
| Insert | O(1) | Append to last block (or first block with space) |
| Search (equality) | O(N/B) | Must scan all blocks (linear search) |
| Search (range) | O(N/B) | Must scan all blocks |
| Delete | O(N/B) + cleanup | Find record (scan) + either mark deleted or compact |
| Update | O(N/B) | Find record + rewrite |
Where N = total records, B = records per block.
Best for: Small tables, tables where insertions dominate, temporary staging tables, log tables where data is always appended.
2. Sequential File (Sorted/Ordered File)
Records are stored sorted on a specific attribute called the ordering key (often the primary key).
| | Block 1 | [E001, Alice][E002, Bob][E003, Charlie] | |
| | Block 2 | [E004, David][E005, Eve][E006, Frank] | |
| | Block 3 | [E007, Grace][E008, Henry][ free ] | |
| Operation | Cost | Explanation |
|---|---|---|
| Search (on ordering key) | O(log₂(B)) | Binary search on blocks |
| Search (on non-key) | O(N/B) | Still requires full scan |
| Range query (on key) | O(log₂(B) + result blocks) | Find start, read sequentially |
| Insert | O(N/B) | Must shift records to maintain order (expensive!) |
| Delete | O(log₂(B)) | Find + mark as deleted (periodic reorganization needed) |
Handling insertions: Since maintaining perfect order on every insert is costly, databases use an overflow area (linked chain of blocks for newly inserted records). Periodically, the file is reorganized (merged with overflow and re-sorted).
Best for: Tables with few insertions/deletions, range queries on the ordering key, batch-loaded data warehouses.
3. Hash File Organization
Records are distributed across buckets using a hash function applied to a designated key attribute.
| Hash function | h(EmpID) = EmpID mod 4 |
| Bucket 0 | [E004][E008][E012] (IDs divisible by 4) |
| Bucket 1 | [E001][E005][E009] (remainder 1) |
| Bucket 2 | [E002][E006][E010] (remainder 2) |
| Bucket 3 | [E003][E007][E011] (remainder 3) |
| Operation | Cost | Explanation |
|---|---|---|
| Search (on hash key) | O(1) average | Compute bucket, read one block |
| Search (range) | O(N/B) | Hash destroys ordering — full scan needed |
| Insert | O(1) average | Compute bucket, append to block |
| Delete | O(1) average | Compute bucket, find and remove |
Best for: Equality searches on the hash key (e.g., lookup by primary key). Terrible for range queries.
4. Clustered File Organization
Records from multiple related tables are stored together in the same blocks, based on a common join attribute.
| Block 1 | [Dept: CS, DeptName: Computer Science] |
| [Emp | E001, Alice, CS] |
| [Emp | E003, Charlie, CS] |
| Block 2 | [Dept: HR, DeptName: Human Resources] |
| [Emp | E002, Bob, HR] |
| [Emp | E005, Eve, HR] |
Advantage: Joining Department and Employee on DeptID requires reading fewer blocks (related data is co-located).
Disadvantage: Operations on a single table become slower (records of one table are scattered across blocks shared with another table).
Comparison Summary
| File Organization | Search (Key) | Search (Non-Key) | Insert | Range Query | Best Use Case |
|---|---|---|---|---|---|
| Heap | O(N) | O(N) | O(1) | O(N) | Write-heavy, small tables |
| Sequential | O(log N) | O(N) | O(N)* | O(log N + result) | Read-heavy, range queries |
| Hash | O(1) | O(N) | O(1) | O(N) | Equality lookups |
| Clustered | Varies | O(N) | Complex | Good for join key | Frequent joins between tables |
*With overflow management, sequential insert amortizes better.
Choosing the Right Organization
The decision depends on the workload:
- OLTP (Online Transaction Processing): Mix of reads and writes on individual records → Hash or B+ tree index on heap
- OLAP (Online Analytical Processing): Bulk scans and range queries → Sequential or columnar storage
- Logging/Audit: Append-only writes, rare reads → Heap
- Data Warehouse: Batch-loaded, join-heavy queries → Clustered or partitioned
In practice, modern DBMS systems use heap files as the base storage with B+ tree indexes built on top for fast access — combining the insertion efficiency of heaps with the search efficiency of ordered structures.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for File Organization.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Database Management Systems (DBMS) topic.
Search Terms
dbms, database management systems (dbms), unit, file, organization, file organization
Related Database Management Systems (DBMS) Topics