0
0
DbmsConceptBeginner · 3 min read

What is Indexing in DBMS: Definition, Example, and Uses

In a DBMS, indexing is a technique to speed up data retrieval by creating a data structure that allows quick search of records. It works like an index in a book, pointing directly to the location of data without scanning the entire table.
⚙️

How It Works

Indexing in a DBMS works like the index at the back of a book. Instead of reading every page to find a topic, you look at the index to find the exact page number. Similarly, a database index stores pointers to data rows based on key values, so the system can jump directly to the data without scanning the whole table.

When you create an index on a column, the DBMS builds a special data structure (often a B-tree or hash) that keeps the column values sorted or organized for fast searching. This reduces the time to find records from scanning all rows (which is slow) to a quick lookup.

💻

Example

This example shows how creating an index on a column speeds up searching for a value in a SQL table.

sql
CREATE TABLE Employees (
  ID INT PRIMARY KEY,
  Name VARCHAR(100),
  Department VARCHAR(50)
);

-- Insert sample data
INSERT INTO Employees VALUES (1, 'Alice', 'HR');
INSERT INTO Employees VALUES (2, 'Bob', 'IT');
INSERT INTO Employees VALUES (3, 'Charlie', 'Finance');

-- Create an index on Department
CREATE INDEX idx_department ON Employees(Department);

-- Query using the index
SELECT * FROM Employees WHERE Department = 'IT';
Output
ID | Name | Department 2 | Bob | IT
🎯

When to Use

Use indexing when you have large tables and need to search or filter data quickly by certain columns. It is especially helpful for columns used often in WHERE clauses, joins, or sorting.

For example, an online store database can index product IDs or customer emails to quickly find orders. However, indexing adds some overhead when inserting or updating data, so it is best used on columns that are read frequently but changed less often.

Key Points

  • Indexing improves query speed by avoiding full table scans.
  • Indexes are special data structures like B-trees or hashes.
  • They work like a book's index, pointing to data locations.
  • Indexes add overhead on data modification operations.
  • Choose columns for indexing based on query patterns.

Key Takeaways

Indexing in DBMS speeds up data retrieval by creating quick lookup structures.
It works like a book index, pointing directly to data locations.
Indexes are best for columns frequently used in search or sorting.
They improve read performance but can slow down writes.
Use indexing wisely to balance query speed and update cost.