Indexes In SQL Server

Introduction

In this post, we will discuss how to work with indexes in SQL and explain the concepts with an example in a simple way. I hope this is very useful for beginners and intermediates to help them understand the basic concept.

Why do we need indexes?

We use indexes to increase the performance of the application and fetch the data very quickly from the table. Normally, after project deployment, we are facing some performance issues, usually in reporting if there are lakhs of records. We have to re-write the queries to increase the performance. The SQL server uses the index and views (selected and required columns) to find the data quickly. Indexes are applied to the column or view.

Whenever you create indexes in a table, internally, B-Tree is created like the below image.

(Image Reference: Google) 
 
If we don't use an index, when we have a huge amount of data in order to search a record, each row will be scanned in the table and this affects the overall performance of the application. Hence, the use of an index is encouraged in such a scenario and by using indexes, we can save time and can improve the performance of database queries and applications.

Index in SQL is a similar index that we find in the book. Indexes are created on table and views and indexes can help the query to find the data quickly.

Table basically can contain four types of indexes.

  1. Clustered
  2. Composite Index
  3. Non- Clustered
  4. Unique Index

Advantages

  • To improve the performance of the query.
  • Indexes are used to fast retrieve the data result set from table

Disadvantages

  • Non clustered index is stored separately from the table, so it requires additional disk space.

Cluster index

The cluster index determines the physical order of the data in a table and stored or sequenced the data in ascending order by default. For this reason, a table can have only one clustered index per table. Whenever you are creating a table with primary key automatically, the clustered index is created.

Hence, a clustered index should be applied to a column, having unique values. Also,  much less modification is done on these values. Usually, it is applied to the primary key column of the table.

A table can have only one clustered index, however, the index can contain multiple columns. The way telephone directory is organized is by the last name and the first name.

Syntax

  1. CREATE CLUSTERED INDEX IndexName ON TableName(ColumnName)  
Let’s take an example. We have a customer table without a primary key.

Example

  1. CREATE TABLE Customer  
  2. (  
  3.    Id int,  
  4.    Name varchar(50),  
  5.    Balance money  
  6. )  
Now, we are going to insert the record and after executing the below statements, we get the following output.
  1. INSERT INTO Customer values(5,'A1',25000), (3,'B1',25000), (4,'C1',25000), (1,'D1',25000), (2,'E1',25000)  
  2. Select * from Customer  

Output

 

We are checking if there are any indexes created or not with the below sp_helpindex procedure.

Explicitly create a cluster index using the command

We are going to explicitly create a clustered index on customer table. After executing the query, you will get the result in ascending order and see the output below.

  1. CREATE CLUSTERED INDEX PK_Customer_CLUSTERED_INDEX ON Customer(ID)  
  2. Select * from Customer  
  3. Exec sp_helpindex 'Customer'  

Output

 

Primary Key with Cluster Index

Whenever we are creating a primary key, the clustered index is automatically created.

Let’s take another example. We are creating a customer table with primary key. Please ensure that you drop the created customer table first.

Example

  1. Drop table Customer  
  2. CREATE TABLE Customer  
  3. (  
  4.    Id int primary key,  
  5.    Name varchar(50),  
  6.    Balance money  
  7. )  
Then again, we are going to the insert the same record.
  1. INSERT INTO Customer values(5,'A1',25000), (3,'B1',25000), (4,'C1',25000), (1,'D1',25000), (2,'E1',25000)  

Output

We are trying to create one more cluster index on customer table but we cannot create more than one cluster index in a table.

Example

  1. CREATE CLUSTERED INDEX PK_Customer_Balance_CLUSTERED_INDEX ON Customer(Balance)  

Output



According to this error, we cannot create more than one clustered index in a table.

With Primary Key Table

Example

  1. CREATE TABLE Example  
  2. (  
  3.    Id int primary key identity,  
  4.    Example varchar(50),  
  5.    Description varchar(50)  
  6. )  
  7. EXECSP_HELPINDEXExample  

Output

 

In a similar way, we have created Employee and Department tables as output shown in the below screen.

Syntax

  1. EXEC sp_helpindex 'TableName'  

Example

  1. EXEC sp_helpindex 'Employee'  
  2. EXEC sp_helpindex 'Department'  

 Output

Index_Name

Syntax Name of the index or on which table index is created

Index_Description

What type of index it is and its description

Index_Keys

Field or a column on which index is created.

Without Primary Key Table and Indexes

  1. CREATE TABLE Example  
  2. (  
  3.    Id int ,  
  4.    Example varchar(50),  
  5.    Description varchar(50)  
  6. )  

Output

 

Composite index or Composite Clustered Index

We can create a composite clustered index on Name and Balance Column. We have to delete the previously created index.

 

And now we are again creating a composite primary key constraint.

  1. CREATE CLUSTERED INDEX Composite_PK_Customer_Balance_CLUSTERED_INDEX ON Customer(Name, Balance)  

Output



To find specific database in all indexes.
  1. select * from [DatabaseName].SYS.INDEXES  

Example

  1. Select * from ExampleDB.sys.indexes  

To find the indexes of specific table in database,

Syntax

  1. select * from sys.indexes 
  2. where object_id = (select object_id from sys.objects where name = 'TABLENAME')  

Example

  1. select * from sys.indexes  
  2. where object_id = (select object_id from sys.objects where name = 'Employee')  

Non Clustered Index

Whenever we are dealing with large tables, Non-cCustered Indexes are very useful. Assume the above table has millions of records and you want to read the data; in that case the Non-Clustered Index will help in increasing the performance. The Non-Clustered index is stored separately from actual data, a table can have more than one non clustered index that can accept duplicate values in the column. The data is stored in one place and the index is stored in another place. The indexes have pointers to the storage location of data. A table can have multiple Non-Cluster indexes on it.

Syntax

  1. CREATE NONCLUSTERED INDEX Index_Name ON Customer(column_name)  

We have deleted all the indexes from a table before creating non-clustered indexes output like below.

Output

 

 

We are going to create a non-clustered index on ‘Name’ column.

Example

  1. CREATE NONCLUSTERED INDEX Non_Clustered_Index ON Customer(Name)  

Output

We can create more than one non-clustered index on a table. We can store duplicate values as well in Non-Clustered index column.

Output

Unique Index

We can create a unique index using the below commands for uniqueness or records in the table.

Syntax

  1. CREATE UNIQUE INDEX IndexName ON TableName(Conumn Name

Example 

  1. CREATE UNIQUE INDEX UQ_Customer_Name_INDEX ON Customer(Name 
Output
 

We create a Unique Index to provide Uniqueness of Index Column values. When a Primary Key or Unique Constraint is used on a table row, a Unique Index will get created automatically to avoid Duplicity.

Drop Indexes

We can drop indexes two ways,

Using command

Syntax

  1. DROP INDEX IndexName ON TableName  

Example

  1. DROP INDEX UQ_Customer_Name_INDEX ON Customer  
  2. Directly delete index from index folder from object explorer  

 

Difference between cluster Index and Non – cluster Index

  1. We can have only one cluster index per table but we can have more than one non clustered index on a table.
  2. As a clustered index refers back to the table, Cluster index is faster than a non-clustered index, non-clustered index is stored in another place.
  3. Non - clustered index is stored separately from the table, so it requires additional disk space and Cluster index doesn’t require additional disk space. It determines the storage order in the table.