Non-Clustered Index
A Non-Clustered Index is a separate structure that helps SQL Server find data quickly without changing the physical order of the table.
What is a Non-Clustered Index?
A Non-Clustered Index is an index structure that is stored separately from the actual table data.
It contains the indexed column values along with a reference that allows SQL Server to locate the actual rows in the table.
Unlike a clustered index, a non-clustered index does not determine the physical order of the table data.
Important Difference
A clustered index determines the physical order of table data, whereas a non-clustered index is a separate structure that contains indexed values and row references.
Syntax
CREATE NONCLUSTERED INDEX index_name
ON table_name(column_name);
Example Table
Consider the following Students table:
CREATE TABLE Students
(
StudentId INT,
StudentName VARCHAR(100),
Course VARCHAR(100),
City VARCHAR(50),
Age INT
);
Creating a Non-Clustered Index
Suppose we frequently search students based on the City column.
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 index name.
- Students is the table name.
- City is the indexed column.
Sample Data
INSERT INTO Students
VALUES
(101, 'Rahul', 'Dot Net', 'Pune', 22),
(102, 'Priya', 'Java', 'Mumbai', 21),
(103, 'Amit', 'Python', 'Pune', 23),
(104, 'Neha', 'Testing', 'Delhi', 22),
(105, 'Rohit', 'Dot Net', 'Pune', 24);
Using the Non-Clustered Index
Now suppose we want to find all students from Pune:
SELECT *
FROM Students
WHERE City = 'Pune';
If SQL Server determines that the non-clustered index is useful for the query, it can use the index to locate the required rows efficiently.
Multiple Non-Clustered Indexes
Unlike a clustered index, a table can have multiple non-clustered indexes.
For example, we can create indexes on both City and Course.
CREATE NONCLUSTERED INDEX IX_Students_City
ON Students(City);
CREATE NONCLUSTERED INDEX IX_Students_Course
ON Students(Course);
Both indexes can exist on the same table.
Non-Clustered Index on Multiple Columns
A non-clustered index can also contain more than one column. Such an index is called a composite or multi-column index.
CREATE NONCLUSTERED INDEX IX_Students_City_Course
ON Students(City, Course);
This index contains both City and Course columns.
Including Additional Columns
SQL Server also allows additional non-key columns to be included in a non-clustered index.
CREATE NONCLUSTERED INDEX IX_Students_City
ON Students(City)
INCLUDE(StudentName, Course);
Here, City is the index key and StudentName and Course are included columns.
Advantages of Non-Clustered Index
- A table can have multiple non-clustered indexes.
- They can improve search performance.
- They are useful for frequently searched columns.
- They can improve queries using WHERE conditions.
- They can be useful for JOIN and ORDER BY operations.
- Included columns can help cover frequently executed queries.
Disadvantages of Non-Clustered Index
- Non-clustered indexes require additional storage.
- Too many indexes can slow down INSERT operations.
- UPDATE and DELETE operations may require index maintenance.
- Indexes need to be maintained as table data changes.
Clustered Index vs Non-Clustered Index
| Clustered Index | Non-Clustered Index |
|---|---|
| Determines the physical order of table data. | Does not determine the physical order of table data. |
| Only one clustered index can exist on a table. | Multiple non-clustered indexes can exist on a table. |
| Data rows are associated directly with the clustered index order. | The index contains keys and references to the actual rows. |
| Useful for range-based retrieval. | Useful for targeted searches and filtering. |
Important Points
- A table can have multiple non-clustered indexes.
- A non-clustered index is stored separately from the actual table data.
- Non-clustered indexes can improve SELECT query performance.
- Indexes require additional storage space.
- Too many indexes can negatively affect INSERT, UPDATE and DELETE operations.
Summary
A Non-Clustered Index is a separate index structure that helps SQL Server locate rows efficiently. It does not change the physical order of data in the table.
Unlike a clustered index, a table can contain multiple non-clustered indexes.
Non-clustered indexes are useful for frequently searched, filtered, joined and sorted columns, but they should be created carefully because they require additional storage and maintenance.