Boosting SQL Performance: Effective Indexing Strategies for Faster Queries


Introduction
Slow SQL queries can severely impact application performance and degrade the user experience. Imagine using a flight booking website to search for tickets. You enter your departure and arrival cities, but the page takes several seconds to load the results. Frustrated, you might abandon the site and choose a competitor with faster response times.
This article will guide you through improving read query performance in SQL databases using indexes. You’ll learn about different types of indexes, their practical applications, and best practices for optimizing database performance while avoiding potential pitfalls.
Understanding Indexes in SQL
One of the most effective ways to enhance SQL query speed is by utilizing indexing.
An index is a data structure that accelerates read operations on a database table. Think of it like the index of a book, which helps locate content quickly without scanning the entire text. Similarly, indexes in SQL allow the database engine to find rows faster by maintaining a sorted copy of specific columns.
Creating and Deleting Indexes
To create an index, use the CREATE INDEX
statement:
CREATE INDEX index_name ON table_name(column_name);
To remove an index, use:
DROP INDEX index_name;
The examples in this article are tested in Microsoft SQL Server (MSSQL), but similar syntax applies to other SQL databases.
Types of Indexes in SQL
Most SQL databases, including MSSQL, support various indexing methods. The most commonly used types are:
Single-Column Indexes
Composite Indexes
Unique Indexes
Implicit Indexes
Filtered Indexes
Let’s explore these with examples using a flights
table linked to an airports
table.
Table Structure
CREATE TABLE airports (
id INT IDENTITY(1,1) PRIMARY KEY,
name NVARCHAR(100) NOT NULL,
city NVARCHAR(100) NOT NULL
);
CREATE TABLE flights (
id INT IDENTITY(1,1) PRIMARY KEY,
airline NVARCHAR(100) NOT NULL,
departure_airport_id INT,
arrival_airport_id INT,
status NVARCHAR(20),
FOREIGN KEY (departure_airport_id) REFERENCES airports(id),
FOREIGN KEY (arrival_airport_id) REFERENCES airports(id)
);
Types of Indexes and Their Uses
1. Single-Column Indexes
A single-column index is created on a single table column to speed up queries involving filtering or sorting.
CREATE INDEX idx_airport_city ON airports(city);
This index optimizes queries like:
SELECT * FROM airports WHERE city = 'New York';
However, it works best when combined with the AND
logical operator:
SELECT * FROM flights WHERE airline = 'Delta' AND departure_airport_id = 1;
Using an OR
condition prevents efficient index utilization:
SELECT * FROM flights WHERE airline = 'Delta' OR status = 'Delayed';
2. Unique Indexes
A unique index ensures no duplicate values in a specific column. This is useful for maintaining data integrity.
CREATE UNIQUE INDEX idx_airport_name_unique ON airports(name);
Attempting to insert duplicate airport names will result in an error:
INSERT INTO airports (name, city) VALUES ('JFK International', 'New York');
3. Composite Indexes
A composite index is created on multiple columns to optimize queries that filter or sort based on more than one column.
CREATE INDEX idx_flight_airline_departure ON flights(airline, departure_airport_id);
Now, queries filtering on both columns run faster:
SELECT * FROM flights WHERE airline = 'Delta' AND departure_airport_id = 1;
4. Implicit Indexes
Implicit indexes are automatically created when defining a PRIMARY KEY
constraint.
CREATE TABLE airports (
id INT IDENTITY(1,1) PRIMARY KEY
);
CREATE TABLE flights (
id INT IDENTITY(1,1) PRIMARY KEY
);
Since id
is a primary key, the database automatically creates an index for efficient lookups:
SELECT * FROM flights WHERE id = 1;
5. Filtered Indexes
A filtered index includes only specific rows that meet a condition, reducing index size and improving query performance for targeted searches.
Creating an index for frequently queried active flights:
CREATE INDEX idx_active_flights ON flights(id, airline) WHERE status = 'Active';
This index benefits queries like:
SELECT id, airline FROM flights WHERE status = 'Active';
Filtered indexes are also useful for soft deletion, where records are marked instead of being deleted:
CREATE INDEX idx_available_flights ON flights(id, airline) WHERE status != 'Cancelled';
SELECT id, airline FROM flights WHERE status != 'Cancelled';
Pros and Cons of Indexing
Benefits of Indexing
Faster Query Performance: Reduces the time required to execute
SELECT
statements,WHERE
clauses,JOINs
, andORDER BY
queries.Efficient Sorting: Speeds up sorting operations.
Enforced Uniqueness: Ensures data integrity when used with unique constraints.
Drawbacks of Indexing
Increased Storage Use: Indexes consume additional disk space.
Slower Write Operations:
INSERT
,UPDATE
, andDELETE
queries can become slower due to index maintenance.Index Fragmentation: Over time, indexes may need rebuilding or reorganizing for optimal performance.
However, indexed columns in WHERE
clauses can still improve update performance:
CREATE INDEX idx_flight_airline ON flights(airline);
UPDATE flights SET status = 'On Time' WHERE airline = 'Delta';
Best Practices for Indexing
Analyze Query Patterns: Identify frequently used queries before creating indexes.
Prioritize Selectivity: Index columns with highly unique values.
Balance Read and Write Performance: Excessive indexing can slow down write operations.
Monitor and Optimize Indexes: Regularly review index usage and rebuild them if needed.
Index Key Query Columns: Index columns frequently used in
WHERE
,JOIN
, andORDER BY
clauses.
Conclusion
Indexes play a crucial role in optimizing SQL performance by accelerating read queries. However, they should be carefully managed to avoid unnecessary overhead in write-heavy applications.
By analyzing query patterns, using the right index types, and maintaining indexes properly, you can achieve significant performance improvements.
I hope this guide helps you enhance your SQL skills! Happy coding!
Subscribe to my newsletter
Read articles from Ujjwal Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ujjwal Singh
Ujjwal Singh
👋 Hi, I'm Ujjwal Singh! I'm a software engineer and team lead with 10 years of expertise in .NET technologies. Over the years, I've built a solid foundation in crafting robust solutions and leading teams. While my core strength lies in .NET, I'm also deeply interested in DevOps and eager to explore how it can enhance software delivery. I’m passionate about continuous learning, sharing knowledge, and connecting with others who love technology. Let’s build and innovate together!