DE Notes (Module-IV)
DE Notes (Module-IV)
Placing File records on disk: Placing file records on disk involves mapping logical records into physical
blocks using techniques like Heap, Sequential, Hashed, or Clustered organization.
Records are packed into blocks using spanned (across blocks) or unspanned (within one block) methods to
optimize disk I/O, which is crucial for reducing access time.
File Organization Techniques:
Heap File Organization (Unordered): Records are placed wherever there is space, typically at the
end of the file, allowing for fast insertion but slower searches.
Sequential File Organization (Ordered): Records are placed in a specific order based on a key field,
making range queries efficient.
Hash File Organization: A hash function is applied to a record field to determine the specific disk
block address for placement, enabling fast direct access.
Clustered File Organization: Related records from multiple tables are stored together in the same or
nearby blocks to optimize join operations.
Record Packing Methods:
Unspanned Records: A record must fit entirely within one block. If it cannot, it moves to the next
block, leaving empty space, often used for fixed-length records.
Spanned Records: A large record can be split across multiple blocks, with pointers linking them. This
is more space-efficient, suitable for variable-length records.
Block Allocation Methods:
Contiguous Allocation: Files are assigned consecutive disk blocks, providing fast sequential access
but making file expansion difficult.
Linked Allocation: File blocks are linked together, making expansion easy, but direct access slower.
Indexed Allocation: Index blocks are used to store pointers to the actual data blocks, offering a
balance between efficiency and flexibility.
Hashing Techniques: Hashing is a database technique used for high-speed data retrieval. Instead of navigating
through a multi-level index structure (like a B-Tree), a hash function maps search keys directly to a specific disk location
(called a bucket), allowing for near-constant time O(1) access. That means storing and retrieving data in O(1) [in order
of one] time.
This is otherwise called as mapping technique because we try to map the larger value with smaller value by using
hashing.
Hash terminology:
Search Key: By using this key, we can search something in the database.
Hash Table: This is a table looks like an array to store the data based on search key.
Hash Function (h): A mathematical function that takes a search key as input and returns a bucket address.
Basically, there are three hash functions used. (K Mod n, Mid Square method and Folding method).
Bucket: A storage unit (usually a disk block) that holds one or more data records.
Hash Index: The resulting address or "slot" generated by the hash function where the record is stored.
Collision: Occurs when the hash function generates the same bucket address for two different keys.
Example:
Search Key (24, 52, 91, 67, 48, 83)
Hash Table
Hash Function (K Mod 10, Mid Square method and Folding method).
0
1 91
52
2
83
3
24
4
5
6
67
7
48
8
9
Hash Table
1) K Mod 10: Here, K is the search key. The first search key is 24.
24 Mod 10 = 4 (Remainder value), and this (4) is the hash value. 24 will store at the index 4. Then
52 Mod 10 = 2 (Remainder value), and this (2) is the hash value. 52 will store at the index 2. Then so on upto last to
store the value.
2) Mid Square Method: Suppose the key is 123. This method finds out the square of the middle number and the no is
2 (i.e. 22). Here the hash value comes to 4 and the key 123 will store at index 4.
3) Folding Method: Suppose the key is 123456 and the hash table is from 0-999. This method divides the number in
two equal part (i.e. 123 and 456) and then add the numbers.
123 + 456 = 579. Here the hash value comes to 579 and the key 123456 will store at index 579.
Suppose there is a search key 62 and we want to store it in the hash table. 62 Mod 10 = 2. Index 2 is already filled
with 52. Here the collision occurs. To avoid the collision, we use some other technology.
Types of Hashing Techniques:
1. Static Hashing
In static hashing, the number of buckets in the database remains constant. The hash function always produces the same
output for a given key.
Limitation: If the data size grows significantly, the fixed number of buckets leads to frequent collisions and
performance degradation.
Collision Resolution Techniques:
o Chaining (Open Hashing): If a bucket is full, the system allocates an overflow bucket and links it to
the original, creating a chain of buckets.
o Open Addressing (Closed Hashing): If a collision occurs, the system linearly or quadratically searches
for the next available empty bucket (Linear Probing/Quadratic Probing/Double Hashing).
91
52 62
83
24
67
48
In case of Linear probing, instead of making chain just we have to go for the next available index to
store the value. If the immediate next index is already filled then go for next available index to store
the value and so on up to last index. In this case the index 3 and index 4 is already filled. So, the value
62 will store at index 5, as index 5 is empty.
In case of Quadratic probing, we have to use the formula (h + i2 Mod n). Here, h is the hash value
and i is the probe no. (no. of times attempt) to store the value in hash table.
2 + 12 = 3 Mod 10 = 3. As the index 3 is already filled we have to go for next attempt. Here the hash
value is fix (i.e. 2) but the no. of attempt is increased to 2. So, the calculation is:
2 + 22 = 6 Mod 10 = 6. As the index 6 is empty, we will store the value 62 at index 6.
In case of Double hashing, just we have to use two hash functions to store the value at proper index.
If the first hash function is not suitable that means if collision occurs then we have to use another hash
function to store the value at proper index.
2. Dynamic Hashing
Dynamic hashing (also called Extendible Hashing) is designed to handle changing data volumes by
allowing the number of buckets to grow or shrink on demand.
Mechanism: It uses a directory of pointers to buckets. When a bucket overflows, it is split,
and the directory may be updated or expanded to accommodate the new structure.
Advantage: It avoids the "overflow chain" performance issues of static hashing and provides
better space utilization for evolving datasets.
Comparison: Static Hashing vs. Dynamic Hashing:
Indexing structures for files: Indexing structures in a Database Management System (DBMS)
are specialized data structures like B-trees or Hash tables used to locate data in a file quickly without having
to scan the entire table.
Indexing Data Structures:
These are the underlying data structures used to store and organize the "index table":
B-Tree / B+ Tree: These are the most common structures. They are self-balancing trees that keep
data sorted and allow for efficient searching, insertion, and deletion.
o Best for: General-purpose indexing, especially when you need to perform range queries
(e.g., WHERE age BETWEEN 20 AND 30) or ORDER BY operations.
Hash Index: These use a hash function to map search keys to specific "buckets" where data is stored.
o Best for: Extremely fast exact-match lookups (e.g., WHERE id = 500). They do not support
range queries because they do not maintain a sorted order.
Bitmap Index: These use bit arrays (strings of 0s and 1s) to represent the presence or absence of a
value.
o Best for: Columns with low cardinality (few unique values, like "Gender" or "Marital
Status") and data warehousing/analytical workloads.
Single-level Ordered Indexes: Single-level ordered indexes in DBMS are ordered files
comprising key-pointer pairs used to speed up search operations by acting as a pointer to the
main data file.
Single-level ordered indexing is a simple yet powerful method to speed up the searching
process while fetching data from a database.
Basics of Single-Level Ordered Indexing:
When a database file is unindexed, we will have to use linear search for retrieving a specific
record. We need to scan all records one by one until the desired one is found. This approach is
slow, when the databases are so large.
Single-level ordered indexing addresses this problem by organizing records in an auxiliary file
called the index file.
An index file contains two main components −
Index Key − A field from the original file (e.g., a name or ID) that is used to organize
and locate data.
Pointer − A reference to the location of the record in the original file.
The values in the index are stored in a specific order, so we can use efficient searching
algorithms like the binary search. Since the index file is much smaller than the original data
file, searches are faster.
Types of Single-Level Ordered Indexes
Primary Index (Sparse): Created on an ordered data file where the index field is the
primary key (ordered key field). It is usually a sparse index, meaning it contains one
entry for each disk block rather than every record.
Secondary Index (Dense): Created on a non-ordering field (non-key or non-unique
field). To enable fast searching of unordered data, it is typically a dense index,
containing an index entry for every record in the table.
Clustered/Clustering Index (Sparse): Applied when the data file is ordered, but the
index field is not a unique key (non-unique ordering field). Multiple records with the
same field value are grouped together (clustered).
Dense Index: A type of ordered index where an entry exists for every search key value,
guaranteeing fast lookup.
Sparse Index: An index where entries exist for only some of the search values (e.g.,
one entry per disk block), requiring less storage space than a dense index.
Lecture Note: 32 09-04-2026
Dynamic Multilevel Indexes using B Trees and B+ Trees: In Database Management
Systems (DBMS), dynamic multilevel indexing uses self-balancing tree structures, primarily
B Trees and B+ Trees, to maintain efficient data access as the database grows or shrinks.
These indexes automatically adjust by splitting or merging nodes during insertions and
deletions.
Overview of B-Trees and B+ Trees in Multilevel Indexing:
Feature B Tree B+ Tree
Data Storage Stores both keys and data (or data Stores data pointers only in leaf nodes;
pointers) in all nodes (internal and internal nodes store only search keys.
leaf).
Tree Height Generally deeper because internal Typically, shallower because more keys
nodes take up more space per fit into internal nodes, increasing "fan-
entry. out".
Search Time Variable: A search might end early Consistent: Every search must traverse
if the key is found in an internal from the root down to a leaf node.
node.
Range Slower: Requires multiple tree Highly Efficient: Leaf nodes are linked
Queries traversals as keys are spread across together, allowing fast sequential
levels. scanning.
Deletion Complex: Deleting from internal Simpler: Deletions only occur at the
nodes requires intricate leaf level, though they may trigger
rebalancing. parent updates.
WHERE Clause → Selection (σ): Filters rows based on the provided condition.
SELECT Clause → Projection (π): Selects specific columns from the filtered results.
Core Algebraic Operators:
SQL Keyword Relational Algebra Operator Description
JOIN / FROM R, S Join / Cross Product (X) Combines rows from multiple relations.
Sorting Phase (Run Generation): The large dataset is divided into "runs"—chunks small enough to fit in
RAM. Each chunk is read into memory, sorted using a standard Internal Sorting algorithm
(like Quicksort or Heapsort), and written back to disk as a sorted temporary file.
Merge Phase: The sorted runs are combined into a single larger file. This is often done using a multi-way
merge. For example, a 10-way merge would use 10 input buffers (one for each run) and one output buffer, using
a Min-Heap to efficiently select the smallest element among all current buffers.
Common Algorithms & Variations:
K-way Merge Sort: Generalizes the merge process by merging sorted runs at once. Using a larger reduces the
total number of merge passes required.
Replacement Selection: A more efficient run generation method that uses a Priority Queue to produce initial
runs that are, on average, twice the size of available memory.
Polyphase Merge Sort: An optimized merge strategy originally designed for tape drives that reduces the
number of merges passes and output files by using a Fibonacci-like distribution of runs.
Balanced Multiway Merge Sort: Distributes runs evenly across multiple storage units to perform merging in
parallel, improving throughput.
Algorithms for SELECT and JOIN Operations: Algorithms for SELECT and JOIN operations
are fundamental to query processing in dbms. They determine how data are retrieved and combined, with the choice of
algorithm, impacting performance based on data size, available memory, and indexing.
SELECT Operations: The SELECT operation (or "selection") filters records from a single table based on specific
criteria. The DBMS chooses an algorithm based on file organization and existing indexes.
Linear Search (Brute Force): Scans every record in the table to find those that satisfy the condition. It is slow
for large tables but works regardless of indexing or sorting.
Binary Search: Used when the data is physically sorted on the selection attribute. It repeatedly halves the search
space, offering much faster performance than linear search.
Primary/Clustering Index Search: Uses a B-tree or similar structure to jump directly to the relevant record(s).
This is the most efficient method for unique key lookups.
Secondary Index Search: Used for searches on non-unique or non-key attributes that have an index. The
DBMS uses the index to retrieve pointers to the matching data blocks.
Hash Key Search: Retrieves records instantly by applying a hash function to the search key to find its exact
disk location.
JOIN Operations: JOIN operations combine rows from two or more tables based on a related column. They are
among the most resource-intensive operations in a database.
Nested-Loop Join (NLJ): The most basic join. It compares every row of the "outer" table with every row of
the "inner" table. Its complexity is, making it inefficient for large datasets.
Block Nested-Loop Join (BNLJ): An optimized version of NLJ that processes data in blocks rather than
individual rows, significantly reducing disk I/O.
Indexed Nested-Loop Join (INLJ): If the inner table has an index on the join attribute, the DBMS uses it to
find matches directly instead of performing a full scan for every outer row.
Sort-Merge Join: Both tables are first sorted on the join attribute and then merged in a single pass. This is
highly efficient for large, sorted datasets or when pre-existing clustered indexes exist.
Hash Join: Uses a hash function to partition rows from both tables into buckets based on the join key. It then
joins the buckets. It is often the fastest choice for large-scale equi-joins that don't have indexes.
Lecture Note: 34 13-04-2026
Algorithms for PROJECT and SET Operations: Algorithms for PROJECT and SET operations in
databases, efficiently handling duplicates and managing data-intensive operations through sorting, hashing, or indexing.
The Project (π) operation is implemented by sorting or hashing to remove duplicates, while UNION (∪),
INTERSECTION (∩), and SET DIFFERENCE (-) use sort-merge or hash-based algorithms to compare and process
union-compatible relations.
Implementing Aggregate Operations and OUTER JOINs: Outer joins (LEFT, RIGHT, FULL) preserve
unmatched rows from tables, filling missing data with NULL, and are implemented by modifying algorithms like
nested-loop or hash-join. When combined with aggregate functions (e.g., SUM, AVG, COUNT), they provide insights
into data sets, such as calculating totals for each group, even when no matching record exists in the secondary table.
Implementing Outer Joins and Aggregates:
Outer Join Implementation:
o Nested-Loop Join: The outer relation retains all its tuples. If a match is found in the inner table, the
joined row is added; otherwise, it is added with NULL for the inner table's columns.
o Sort-Merge/Hash-Join: These algorithms can be extended to retain unmatched rows.
o Relational Algebra: Computed by performing an inner join, identifying missing tuples, padding them
with NULL, and unioning them back.
Aggregate Operations:
o Functions like AVG, SUM, MIN, MAX, and COUNT often operate within GROUP BY clauses to
provide analytical insights.
o When an aggregate function is used with an outer join, it is crucial to understand that the aggregate can
be applied to rows that may have NULL values from the non-matching side.
Lecture Note: 35 15-04-2026
Combining Operations Using Pipelining: Pipelining in database systems combines multiple
relational operations (SELECT, JOIN, PROJECT) into a single execution step. It passes data directly between
operators without creating, writing, and reading temporary files on disk. This technique significantly reduces
disk I/O, improves performance, and increases throughput, effectively allowing query processing in parallel.
For example, rather than being implemented separately, a JOIN can be combined with
two SELECT operations on the input files and a final PROJECT operation on the resulting file; all this is
implemented by one algorithm with two input files and a single output file. Rather than creating four temporary
files, we apply the algorithm directly and get just one result file.
Key Aspects of Pipelining in Query Processing
Reduced Disk Access: By avoiding the materialization of intermediate results in temporary files,
pipelining reduces disk I/O costs.
Stream-based Evaluation: The results of one operation are immediately fed as input to the next, often
called stream-based processing.
Faster Response Time: Pipelining allows the database to start producing output tuples much faster
than if the entire query was materialized.
Example:
Suppose we want to perform combined multiply and add operation with a stream of nos.
Ai * Bi + Ci for i= 1, 2, 3……7
R1← Ai, R2←Bi
R3←R1 * R2, R4←Ci
R5←R3 + R4
Ai Bi Ci
R1 R2
Multiplier
R3 R4
Adder
R5