Introduction
This article will discuss Cluster Index and Non-Cluster Index in SQL. Database performance is very important in development, and Indexes play a very important role.
Let us start with,
What are Indexes in SQL?
Have you ever used Indexes in a book? Just imagining, if you do not have indexes in the book and want to search the topic in the book, how difficult would it be? To search topic or chapter in the book, we open indexes of the book, find the page number of a particular chapter or topic, and go directly to that topic.
SQL Indexes work like a book index. If you do not have indexes in SQL, searching data would take more time which hampers database performance.
How to find which indexes are created on the table?
Assume that we have a table called "Member" and are trying to find Indexes created on the table using Command and SQL Server Management Studio.
CREATE TABLE Member
(
id INT PRIMARY KEY,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(50) NOT NULL,
gender VARCHAR(50) NOT NULL,
address VARCHAR(50) NOT NULL,
city VARCHAR(50) NOT NULL
)
The first way is the Using Commands,
You can use the below command to find which indexes were created on the table.
exec sp_helpindex [TableName]

This is the Inbuild store procedure that helps us to find Indexes created on the table.
Another way is - Using SQL Server Management Studio,
- Select table from SQL Server Management Studio
- Click on the Indexed folder
- All available indexes are visible.
Eg.

Types of Indexes in SQL?
Below are a few essential Indexes available in SQL,
- Clustered
- Non-clustered
- Unique
- Column store
- Index with included columns
- Full-text
- XML
- Spatial etc
The Scope of this article is limited to Cluster and Non-Cluster indexes.
Cluster Index
A Cluster Index determines the order of physical data of the table; hence, the table can have only one cluster index in SQL.
In the Cluster index, data can be reordered or sorted in one way.
How to Create Cluster Index?
Cluster Index will automatically be created when we apply primary key constrain on the table column.
Let us follow the below steps to justify this statement.
First, create a member table with Primary constraint on the ID column using the below script.
CREATE TABLE Member
(
id INT PRIMARY KEY,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(50) NOT NULL,
gender VARCHAR(50) NOT NULL,
address VARCHAR(50) NOT NULL,
city VARCHAR(50) NOT NULL
)
We have set the primary key on the Id column, which will automatically create a clustered index on the Id column. You can check created index using the Sp_helpindexs command or SQL Studio Management Studio. I have covered that in the above topic.
Now we will insert data in the member table using the below script.
INSERT INTO [dbo].[Member] VALUES (5,'Kirtesh','Shah','Male','Vadodara','Vadodara')
INSERT INTO [dbo].[Member] VALUES (2,'Raj','Shah','Male','Surat','Surat')
INSERT INTO [dbo].[Member] VALUES (3,'Sweta','Shah','FeMale','Mumbai','Mumbai')
INSERT INTO [dbo].[Member] VALUES (1,'Nitya','Shah','Male','Dabhoi','Dabhoi')
INSERT INTO [dbo].[Member] VALUES (4,'Hansa','Shah','FeMale','Vadodara','Vadodara')
GO 


Phaser PhalanxPosted Aug 17, 2021, 11:36 AM
Where do indexes with date and time in the name come from: NonClusteredIndex-20210322-162430