Lec 8 Indexing & Data Structures for Query Processing
Lec 8 Indexing & Data Structures for Query Processing
Processing
Lecture Agenda
⚫ Introduction to Query Processing & Indexing
⚫ B-Tree Indexing
⚫ Hash Indexing
⚫ Indexing Mechanisms in DBMS
⚫ Unique, Composite, and Covering Indexes
What is Indexing?
⚫ Indexing is a data structure technique used in
databases to speed up the retrieval of records.
⚫ Instead of scanning every page (or record), the
database uses the index to quickly locate the data.
Advantages of Indexing
Good for:
⚫ Searching or filtering by emp_id
⚫ Sorting by emp_id
Types of B-tree Indexes in Databases
2. Composite B-tree Index
Index on multiple columns (multi-column B-tree).
Example:
CREATE INDEX idx_dept_age ON employee(dept,
age);
Good for:
⚫ Queries filtering by both dept and age
⚫ Follows Left-to-Right Prefix Rule
Types of B-tree Indexes in Databases
3. Unique B-tree Index
A B-tree index that enforces uniqueness on a column or
a combination of columns.
Example:
CREATE UNIQUE INDEX idx_email ON users(email);
Good for:
⚫ Preventing duplicate values (e.g., duplicate emails).
Types of B-tree Indexes in Databases
4. Clustered B-tree Index
In some DBMSs (like SQL Server, MySQL):
A Clustered Index means the table rows are physically
ordered based on the index.
Example:
In MySQL , PRIMARY KEY creates clustered index
automatically
CREATE TABLE student ( student_id INT PRIMARY KEY,
name VARCHAR(50) );
Good for:
⚫ Fast range queries
⚫ Faster retrieval when ordering is needed.
B-Tree Index
B+ Tree Indexing in Databases
What is a B+ Tree?
⚫ B+ Tree is an advanced version of a B-tree.
⚫ It is widely used in database systems (MySQL, Oracle,
PostgreSQL) for indexing large amounts of data.
⚫ It keeps all the data only in the leaf nodes.
⚫ Internal nodes store only keys (used for navigation).
Structure of B+ Tree Index
⚫ Internal nodes: Only keys and pointers (no actual data).
⚫ Leaf nodes:
⚫ Store complete data records.
⚫ Are linked together (like a linked list) for fast sequential
access.
Simple B+ Tree Example
[20 | 40]
/|\
[5 10] [25 30 35] [45 50 60]
Emp ID Name
101 Alice
102 Bob
103 Charlie
104 David
Hash Function Example
Hash Function
Let’s say:
hash(EmpID) = EmpID % 3
(Meaning: remainder when divided by 3.)
Hash Computations:
⚫ 101 % 3 = 2 → Bucket 2
⚫ 102 % 3 = 0 → Bucket 0
⚫ 103 % 3 = 1 → Bucket 1
⚫ 104 % 3 = 2 → Bucket 2 (collision!)
Bucket Representation
Bucket No Entries
0 102 (Bob)
1 103 (Charlie)