Creating & Dropping Indexes
Learn how to create, view and remove indexes in SQL Server.
Introduction
Indexes are database objects used to improve the performance of data retrieval operations.
SQL Server provides the CREATE INDEX statement to create indexes and the DROP INDEX statement to remove indexes that are no longer required.
Example Table
First, create a Students table:
CREATE TABLE Students
(
StudentId INT,
StudentName VARCHAR(100),
Course VARCHAR(100),
City VARCHAR(50),
Age INT
);
Creating a Non-Clustered Index
We can create a non-clustered index on the City column.
CREATE NONCLUSTERED INDEX IX_Students_City
ON Students(City);
Here, IX_Students_City is the name of the index and City is the indexed column.
CREATE INDEX Syntax
CREATE INDEX index_name
ON table_name(column_name);
If the index type is not specified, SQL Server creates a non-clustered index by default.
Creating a Clustered Index
A clustered index can be created using:
CREATE CLUSTERED INDEX IX_Students_StudentId
ON Students(StudentId);
Creating a Unique Index
A unique index prevents duplicate values in the indexed column or combination of columns.
CREATE UNIQUE INDEX IX_Students_UniqueId
ON Students(StudentId);
This ensures that duplicate StudentId values are not allowed through this unique index.
Creating an Index on Multiple Columns
We can create an index using more than one column.
CREATE INDEX IX_Students_City_Course
ON Students(City, Course);
This is called a composite or multi-column index.
Viewing Indexes
SQL Server provides system catalog views that can be used to view information about indexes.
SELECT
name AS IndexName,
type_desc AS IndexType
FROM sys.indexes
WHERE object_id = OBJECT_ID('Students');
This query displays the indexes associated with the Students table.
Dropping an Index
When an index is no longer required, it can be removed using the DROP INDEX statement.
DROP INDEX IX_Students_City
ON Students;
Here, IX_Students_City is the index that will be removed from the Students table.
Removing Unused Indexes
Unused indexes can consume storage and require maintenance when table data changes.
Therefore, indexes that are no longer useful should be reviewed and removed carefully.
DROP INDEX IX_Students_City
ON Students;
Checking Before Dropping
Before dropping an index, it is useful to verify that the index exists.
IF EXISTS
(
SELECT 1
FROM sys.indexes
WHERE name = 'IX_Students_City'
AND object_id = OBJECT_ID('Students')
)
BEGIN
DROP INDEX IX_Students_City
ON Students;
END;
Why Should Indexes Be Managed Carefully?
Although indexes improve SELECT query performance, every index also requires storage and maintenance.
- Indexes consume additional storage.
- INSERT operations may require index updates.
- UPDATE operations may require index maintenance.
- DELETE operations may require index maintenance.
- Too many unnecessary indexes can affect write performance.
Complete Example
Create the table:
CREATE TABLE Students
(
StudentId INT,
StudentName VARCHAR(100),
Course VARCHAR(100),
City VARCHAR(50)
);
Create an index:
CREATE INDEX IX_Students_City
ON Students(City);
Use the table:
SELECT *
FROM Students
WHERE City = 'Pune';
Remove the index when it is no longer required:
DROP INDEX IX_Students_City
ON Students;
Important Points
- Use CREATE INDEX to create an index.
- Use CREATE CLUSTERED INDEX for a clustered index.
- Use CREATE NONCLUSTERED INDEX for a non-clustered index.
- A table can have only one clustered index.
- A table can have multiple non-clustered indexes.
- Use DROP INDEX to remove an index.
- Unnecessary indexes should be removed carefully.
Summary
SQL Server provides commands for creating and removing indexes. The CREATE INDEX statement is used to create an index, while the DROP INDEX statement is used to remove it.
Indexes can significantly improve data retrieval performance, but they also require storage and maintenance.
Therefore, indexes should be created based on actual query requirements and unnecessary indexes should be avoided.