Course Content
Database Management System (DBMS)
File Organization: Heap, Sequential, and Hashed

 

File organization refers to the way records are stored on disk. Choosing the right file organization affects how efficiently we can insert, search, update, or delete data in a database. There are three basic types of file organization:


1. Heap File Organization (Unordered Files)

 

In heap file organization, records are stored in no particular order. New records are simply added to the end of the file, wherever space is available.

This method is also one of the simplest methods of file organization in DBMS. In this method, records are inserted in a sequential manner, but unlike the sequential file method, the data blocks are not allocated sequentially DBMS can choose any data block for the record to be inserted. There is no ordering of records in heap file organization once the data block is full, the next record is stored in the new data block, which might not be the next data block, as shown in the image:


Characteristics:
  • Fast insertion: Since data is added at the end, insertions are quick.
  • Slow searching: For search, the DBMS has to scan the entire file (linear search).
  • No order maintained among records.
  • Deletion marks records as invalid or removes them physically, depending on the DBMS.

Use Cases:
  • Suitable for small tables or tables where inserts are more frequent than reads.
  • Often used in temporary tables or log files.

Example:

Imagine you are adding new student records randomly to a database. They are just added at the end without checking any order or structure.


2. Sequential File Organization (Ordered Files)

 

In sequential file organization, records are stored in sorted order based on a specific attribute (usually the primary key or a search key).


Here, R1R2, and R3 up to R6 represent records of the file, the record is just a row in a table. There are two ways to organize records in sequential order depending upon the ordering schema of records.

Characteristics:
  • Efficient range queries: If you need all records between roll number 100 and 200, it can be done quickly.
  • Slower insertion and deletion: To maintain the sorted order, insertions require shifting data or creating overflow pages.
  • Searching can be done using binary search (if file is stored contiguously).
  • Often used in combination with indexing to speed up operations.

Use Cases:
  • Best for read-heavy applications where sorted data is often queried.
  • Useful in report generation, billing systems, etc.

Example:

Consider a payroll system where employee records are always stored sorted by employee ID or department.


3. Hashed File Organization

 

In hashed file organization, a hash function is applied to a key field (like employee ID or student ID) to determine the location (bucket) where the record should be stored.


In hash file organization, Hashing is used to generate the addresses of the memory blocks where actual records are mapped. Basically, a hashing function generates the address of those data blocks using the primary key as input, and those memory locations which are generated by these hash functions are called data buckets or data blocks.

Characteristics:
  • Fast retrieval for exact match queries (e.g., find student with ID 12345).
  • Not suitable for range queries (e.g., students with marks between 80 and 90).
  • Collisions (two keys mapping to the same location) are handled using overflow areas or chaining.
  • Performance depends on how well the hash function distributes the data.

Use Cases:
  • Ideal for exact key-based lookups, such as account number lookups in banking systems.
  • Used in hash-indexed files and internal database operations.

Example:

If the hash function is student_id % 100, then student ID 12345 would be placed in bucket 45.


Summary Comparison Table

Feature Heap Sequential Hashed
Insertion Speed Fast Moderate Fast
Search Speed Slow Fast (if ordered) Very Fast (exact)
Range Queries Poor Excellent Poor
Deletion/Update Moderate Complex Moderate
Best Use Case Frequent Inserts Sorted Reports Exact Lookups
0% Complete
WhatsApp Icon

Hi Instagram Fam!
Get a FREE Cheat Sheet on System Design.

Hi LinkedIn Fam!
Get a FREE Cheat Sheet on System Design

Loved Our YouTube Videos? Get a FREE Cheat Sheet on System Design.