- Advanced SQL
-
Overview
- Indexing for Performance Optimization
- Transactions and ACID Properties
- Triggers and Event Scheduling in SQL
- Window Functions and Ranking
- Pivoting Data in SQL
- JSON and XML Data Handling
- Recursive Queries in SQL
- Performance Tuning and Query Optimization
- Role-Based Access Control in Databases
- Handling Large Datasets in SQL
Indexing for Performance Optimization
Add to BookmarkIntroduction
As databases grow, query performance becomes a critical factor. Without optimization, queries can become slow and inefficient. Indexing is one of the most effective ways to speed up data retrieval in SQL databases. In this tutorial, we will explore how indexes work, different types of indexes, and best practices for using them to optimize performance.
What is an Index in SQL?
An index is a data structure that improves the speed of data retrieval operations on a database table. It works like an index in a book, allowing the database to quickly locate the required rows instead of scanning the entire table.
How Indexing Works
- When a table has no index, the database engine performs a full table scan to find the required data.
- When an index is added, the database can use the index to find the data faster, reducing the number of rows it needs to search.
Types of Indexes in SQL
1. Primary Index
A primary index is automatically created when a PRIMARY KEY is defined on a table. It ensures uniqueness and is stored as a clustered index in many databases.
Example:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50)
);
Here, an index is created on the id
column automatically.
2. Unique Index
A unique index ensures that the indexed column contains only unique values.
Example:
CREATE UNIQUE INDEX idx_employee_email ON employees(email);
This ensures that duplicate emails cannot be inserted into the table.
3. Clustered Index
A clustered index determines the physical order of data in a table. Each table can have only one clustered index.
Example (In SQL Server, MySQL, PostgreSQL):
CREATE CLUSTERED INDEX idx_employee_id ON employees(id);
Here, the rows in the table will be physically stored in the order of id
.
4. Non-Clustered Index
A non-clustered index creates a separate structure that stores pointers to the actual data. A table can have multiple non-clustered indexes.
Example:
CREATE INDEX idx_employee_name ON employees(name);
This index helps in quickly searching for employees by name.
5. Composite Index
A composite index is an index on multiple columns. It improves queries that filter on multiple fields.
Example:
CREATE INDEX idx_department_salary ON employees(department, salary);
This index speeds up queries filtering by department
and salary
together.
6. Full-Text Index
A full-text index is used for searching text-based data efficiently.
Example (MySQL):
CREATE FULLTEXT INDEX idx_article_content ON articles(content);
This is useful for searching within large text fields.
Best Practices for Using Indexes
- Use Indexes on Frequently Queried Columns
- If a column is used frequently in WHERE, JOIN, ORDER BY, or GROUP BY, indexing it can improve performance.
- Avoid Indexing Small Tables
- If a table has very few rows, indexing might not provide significant benefits.
- Use Composite Indexes for Multi-Column Searches
- If a query searches by multiple columns together, a composite index is more efficient than separate indexes.
- Avoid Over-Indexing
- Too many indexes can slow down INSERT, UPDATE, and DELETE operations because the database needs to update all indexes.
- Regularly Monitor and Optimize Indexes
- Use database tools like EXPLAIN (MySQL, PostgreSQL), EXECUTION PLAN (SQL Server) to analyze query performance.
Example: Query Performance Before and After Indexing
Without Index (Slow Query)
SELECT * FROM employees WHERE name = 'Sumit Kumar';
If there’s no index, the database will scan all rows to find Sumit Kumar’.
With Index (Fast Query)
CREATE INDEX idx_employee_name ON employees(name);
SELECT * FROM employees WHERE name = 'Sumit Kumar';
With the index, the query runs much faster.
Conclusion
Indexes significantly improve query performance by allowing the database to retrieve data quickly. However, overuse of indexes can slow down write operations. By following best practices, you can balance performance and efficiency in your SQL database.
Prepare for Interview
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
Random Blogs
- AI Agents & Autonomous Systems – The Future of Self-Driven Intelligence
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- The Ultimate Guide to Machine Learning (ML) for Beginners
- Deep Learning (DL): The Core of Modern AI
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- The Ultimate Guide to Data Science: Everything You Need to Know
- Time Series Analysis on Air Passenger Data
- Where to Find Free Datasets for Your Next Machine Learning & Data Science Project
- Best Platform to Learn Digital Marketing in Free
- How to Start Your Career as a DevOps Engineer
- Datasets for Speech Recognition Analysis
- Government Datasets from 50 Countries for Machine Learning Training
- Types of Numbers in Python
- Understanding Data Lake, Data Warehouse, Data Mart, and Data Lakehouse – And Why We Need Them
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset