Why do we use Indexes?

Indexes are used by queries to find data from tables quickly. Indexes are created on tables and views. Index on a table or a view, is very similar to an index that we find in a book.

If we don’t have an index, and try to look for a specific chapter in the book, then the only way that we have to find the chapter is to go through the pages one by one from the starting page. On, the other hand, if we have the index, we lookup the page number of the chapter in the index, and then we can go directly into that chapter. So, what book index is doing here is that it is drastically reducing the time that it takes for us to find the chapter.

In a similar way, Table and View Indexes, can help the query that we write to find the data quickly. In fact, the existence of the right indexes, can drastically improve the performance of the query.

If there is no index to help the query, then the query engine, checks every row in the table from the beginning to the end. This is called a Table Scan. Table Scan is very bad for performance.

Index Example

Let’s say at the moment , the Employees table does not have an index on Salary column.

ID Name Salary Gender
1 Sam 2500 Male
2 Pam 6500 Female
3 John 4500 Male
4 Sara 5500 Female
5 Todd 3100 Male
SELECT * FROM tblEmployee
WHERE Salary > 5000 and Salary <7000

Now, suppose we are supposed to find all the employees who has greater than 5000 and less than 7000, then the query engine has to check each and every row in the table, in the absence of an index on the Salary column, resulting in a table scan, which can adversely affect the performance, especially if the table is large.

Creating an Index

CREATE INDEX IX_tblEmployee_Salary
ON tblEmployee (SALARY ASC)

The above SQL query creates an index which stores salary of each employee, in the ascending order as shown below. The actual index may look slightly different.

Untitled

Now, when the query engine has to execute the same query, it has an index on the salary column to help this query. Salaries between the range of 5000 and 7000 are usually present at the bottom, since the salaries are arranged in the ascending order. The query engine picks up the row addresses from the index and directly fetch the records from the table, rather than scanning each row in the table. This is called as an Index Seek.

Dropping and Index

-- DROP INDEX table_name.index_Name
DROP INDEX tbl_Employee.IX_tblEmployee_Salary;

Types of Indexes

Different types of Indexes available in SQL Server are:

Among these the mostly used index types are Clustered and Non-Clustered

Clustered Index

A Clustered Index determines the physical order of data in the table. For this reason, a table can have only one clustered index.

Untitled

Note: In the above example, Id column is marked as primary key. A Primary Key, constraint creates a clustered index automatically if no clustered index already exists on the table. Therefore, an values inserted into the table are ordered in the sequential order by the Id column.

Although, a table can have only one clustered index. However, the index can contain multiple columns ( a composite index ).

CREATE CLUSTERED INDEX IX_tblEmployee_Gender_Salary
ON tblEmployee(GENDER DESC, Salary ASC)

Untitled

Non-Clustered Index

A Non-Clustered Index is analogous to an index in a textbook. The data is stored in one place, the index in another place. The index will have pointers to the storage location of the data.

Since, the Non-Clustered Index is stored separately from the actual data, a table can have more than one Non-Clustered Index, just like how a book can have an index by Chapters at the beginning and another index by common terms at the end.

In the index itself, the data is stored in an ascending or descending order of the index key, which doesn’t in any way influence the storage of data in the table.

CREATE NONCLUSTERED INDEX IX_tblEmployee_Name ON tblEmployee(Name)

Untitled

Difference between Clustered and Non-Clustered Index

  1. We can have only one Clustered Index per table, where as we can have more than on Non-Clustered index.
  2. Clustered Index is faster than a Non-Clustered Index, because, the Non-Clustered index has to refer back to the table, if the selected column is not present in the index.
  3. Clustered Index determines the storage order of rows in the table, and hence does not require additional disk space. But whereas a Non-Clustered Index is stored separately from the table, additional storage space is required.

Disadvantages of Indexes

  1. Additional Disk Space: Clustered Index does not require any additional storage. But every Non-Clustered Index requires additional space as it is stored separately from the table. The amount of space required will depend on the size of the table, and the number and types of columns used in the index.
  2. Insert, Update and Delete statements can become slow: When DML (Data Manipulation Language) statements (INSERT, UPDATE, DELETE) modifies data in a table, the data in all the indexes also needs to be updated. Indexes can help, to search and locate the rows, that we want to delete, but too many indexes to update can actually hurt the performance of data modifications.
  3. What is a covering query: If all the columns that we have registered in the SELECT clause of a query, are present in the index, then there is no need to lookup in the table again. The requested columns data can simple be returned from the index.
  4. A clustered index, always covers a query: Since it contains all of the data in a table. A composite index is an index on two or more columns. Both clustered and non-clustered indexes can be composite indexes. To a certain extent, a composite index, can cover a query.