Database Management System (DBMS) / Importance of Indexing

Why Indexing is used?

πŸ” What is Indexing?

Indexing in a Database Management System (DBMS) is used to speed up the retrieval of data from tables without scanning the entire table every time a query is run.


πŸ” Main Purpose: Faster Data Retrieval

Imagine a huge book (like a phonebook). If you had to find a name without the index, you'd read every page β€” that’s a full table scan.

But with an index, you can jump directly to the section β€” just like in a real book. That’s what indexing does in a database.


πŸ’‘ Why Indexing is Important:

Reason

Explanation

πŸ”Ή Improves Query Speed

Fetches data faster by reducing the number of disk I/Os.

πŸ”Ή Reduces Full Table Scans

Prevents scanning all rows for common queries.

πŸ”Ή Boosts Performance in Joins

Indexes on foreign keys make joins faster.

πŸ”Ή Optimizes WHERE Conditions

Makes searches based on conditions like WHERE age > 30 more efficient.

πŸ”Ή Supports Sorting and Grouping

Helps in faster execution of ORDER BY and GROUP BY queries.


πŸ“Œ How Index Works (Basic Idea)

An index is a separate data structure (like a B-Tree or Hash Table) that holds a key (like a column value) and a pointer to the actual location of the data row.

Example:

Consider this table:

ID

Name

Age

1

Alice

24

2

Bob

30

3

Carol

27

If you create an index on Age, the index might look like:

[24] β†’ Row 1
[27] β†’ Row 3
[30] β†’ Row 2

So when you run:

SELECT * FROM students WHERE age = 27;

The DBMS goes directly to Row 3 using the index β€” instead of checking all rows.


🧱 Types of Indexes

Type

Use Case

Primary Index

Based on the primary key.

Secondary Index

Based on non-primary attributes.

Clustered Index

Sorts actual data rows. Only one per table.

Non-clustered Index

Separate from data, can be multiple per table.

Composite Index

Based on multiple columns.


❗ Drawbacks of Indexing

Drawback

Reason

🚫 Slower Insert/Update/Delete

Indexes must be updated each time data changes.

🧠 Extra Storage Required

Index files take up space.

⚠️ Over-indexing Hurts Performance

Too many indexes slow down write operations.


βœ… When to Use Indexing

Use indexing when:

  • You frequently search by a column (e.g., WHERE, JOIN, ORDER BY).

  • You work with large tables.

  • You have performance issues due to full table scans.


🧠 Summary

Feature

Role of Indexing

πŸ”Ž Query Speed

Makes data retrieval faster

πŸ“¦ Storage

Requires extra space for index files

✍️ Write Speed

May slow down INSERT/UPDATE/DELETE

βš™οΈ Structure

Based on B-Trees or Hash Tables