Clustered Index
What is a Clustered Index?
A Clustered Index determines the physical order in which data rows are stored in a table.
The data rows of the table are stored according to the clustered index key. Because the actual table data is arranged according to the clustered index, a table can have only one clustered index.
How Does a Clustered Index Work?
Suppose we have a Students table containing the following records:
StudentId StudentName City
--------------------------------
103 Rahul Pune
101 Amit Mumbai
105 Neha Pune
102 Priya Delhi
104 Rohit Nashik
If we create a clustered index on StudentId, SQL Server organizes the table data according to the StudentId values.
StudentId StudentName City
--------------------------------
101 Amit Mumbai
102 Priya Delhi
103 Rahul Pune
104 Rohit Nashik
105 Neha Pune
Creating a Clustered Index
A clustered index can be created using the CREATE CLUSTERED INDEX statement.
CREATE CLUSTERED INDEX IX_Students_StudentId
ON Students(StudentId);
Here:
- IX_Students_StudentId is the index name.
- Students is the table name.
- StudentId is the indexed column.
Clustered Index with Primary Key
When a primary key is created, SQL Server generally creates a clustered index for the primary key by default, unless another index option is specified.
CREATE TABLE Students
(
StudentId INT PRIMARY KEY,
StudentName VARCHAR(100),
City VARCHAR(50),
Age INT
);
In this example, the primary key on StudentId can have a clustered index associated with it.
Only One Clustered Index
A table can have only one clustered index because the actual data rows can be physically ordered in only one way.
For example, we can create a clustered index on StudentId:
CREATE CLUSTERED INDEX IX_Students_StudentId
ON Students(StudentId);
We cannot create another clustered index on the same table while the existing clustered index is present.
Example Query
Consider the following query:
SELECT *
FROM Students
WHERE StudentId = 103;
If StudentId is the clustered index key, SQL Server can efficiently locate the required row using the index structure.
Advantages of Clustered Index
- Improves data retrieval performance.
- Data is physically organized according to the clustered index key.
- Useful for range-based queries.
- Can improve ORDER BY operations on the indexed key.
- Useful when records are frequently searched using the indexed column.
Disadvantages of Clustered Index
- A table can have only one clustered index.
- Insert and update operations may require data reorganization.
- Creating and maintaining indexes requires additional database resources.
Complete Example
Create a Students table:
CREATE TABLE Students
(
StudentId INT,
StudentName VARCHAR(100),
City VARCHAR(50),
Age INT
);
Insert sample records:
INSERT INTO Students
VALUES
(103, 'Rahul', 'Pune', 22),
(101, 'Amit', 'Mumbai', 21),
(105, 'Neha', 'Pune', 23),
(102, 'Priya', 'Delhi', 22),
(104, 'Rohit', 'Nashik', 24);
Create a clustered index:
CREATE CLUSTERED INDEX IX_Students_StudentId
ON Students(StudentId);
Now SQL Server can organize the table data according to StudentId.
Important
A clustered index is closely related to the physical organization of table data. Since the actual data rows can have only one physical order, a table can have only one clustered index.
Important Points
- A clustered index determines the physical order of data rows.
- A table can have only one clustered index.
- Clustered indexes are useful for fast data retrieval.
- Primary keys are commonly associated with clustered indexes by default.
- Clustered indexes are useful for range queries.
Summary
A Clustered Index determines the physical order of data rows in a SQL Server table. The data is organized according to the clustered index key.
A table can have only one clustered index because the table data can have only one physical order.
Clustered indexes are useful for improving data retrieval performance, especially when searching and retrieving data using the indexed key.