Click here to Skip to main content
13,248,927 members (42,297 online)
Click here to Skip to main content
Add your own
alternative version


127 bookmarked
Posted 25 Mar 2011

Clustered and Non-Clustered Index in SQL 2005

, 4 Jun 2013
Rate this:
Please Sign up or sign in to vote.
Talks about Clustered and Non clustered index in SQL 2005 and how it works, what is the need for it.

1. Introduction

We all know that data entered in the tables are persisted in the physical drive in the form of database files. Think about a table, say Customer (For any leading bank India), that has around 16 million records. When we try to retrieve records for two or three customers based on their customer id, all 16 million records are taken and comparison is made to get a match on the supplied customer ids. Think about how much time that will take if it is a web application and there are 25 to 30 customers that want to access their data through internet. Does the database server do 16 million x 30 searches? The answer is no because all modern databases use the concept of index.

2. What is an Index

Index is a database object, which can be created on one or more columns (16 Max column combination). When creating the index will read the column(s) and forms a relevant data structure to minimize the number of data comparisons. The index will improve the performance of data retrieval and adds some overhead on data modification such as create, delete and modify. So it depends on how much data retrieval can be performed on table versus how much of DML (Insert, Delete and Update) operations.

In this article, we will see creating the Index. The below two sections are taken from my previous article as it is required here. If your database has changes for the next two sections, you can directly go to section 5.

3. First Create Two Tables

To explain these constraints, we need two tables. First, let us create these tables. Run the below scripts to create the tables. Copy paste the code on the new Query Editor window, then execute it.

CREATE TABLE Student(StudId smallint, StudName varchar(50), Class tinyint);
CREATE TABLE TotalMarks(StudentId smallint, TotalMarks smallint);

Note that there are no constraints at present on these tables. We will add the constraints one by one.

4. Primary Key Constraint

A table column with this constraint is called as the key column for the table. This constraint helps the table to make sure that the value is not repeated and also no null entries. We will mark the StudId column of the Student table as primary key. Follow these steps:

  1. Right click the student table and click on the modify button.
  2. From the displayed layout, select the StudId row by clicking the Small Square like button on the left side of the row.
  3. Click on the Set Primary Key toolbar button to set the StudId column as primary key column.


Now this column does not allow null values and duplicate values. You can try inserting values to violate these conditions and see what happens. A table can have only one Primary key. Multiple columns can participate on the primary key column. Then, the uniqueness is considered among all the participant columns by combining their values.

5. Clustered Index

The primary key created for the StudId column will create a clustered index for the Studid column. A table can have only one clustered index on it.

When creating the clustered index, SQL server 2005 reads the Studid column and forms a Binary tree on it. This binary tree information is then stored separately in the disc. Expand the table Student and then expand the Indexes. You will see the following index created for you when the primary key is created:


With the use of the binary tree, now the search for the student based on the studid decreases the number of comparisons to a large amount. Let us assume that you had entered the following data in the table student:


The index will form the below specified binary tree. Note that for a given parent, there are only one or two Childs. The left side will always have a lesser value and the right side will always have a greater value when compared to parent. The tree can be constructed in the reverse way also. That is, left side higher and right side lower.


Now let us assume that we had written a query like below:

Select * from student where studid = 103;
Select * from student where studid = 107;

Execution without index will return value for the first query after third comparison.
Execution without index will return value for the second query at eights comparison.

Execution of first query with index will return value at first comparison.
Execution of second query with index will return the value at the third comparison. Look below:

  1. Compare 107 vs 103 : Move to right node
  2. Compare 107 vs 106 : Move to right node
  3. Compare 107 vs 107 : Matched, return the record

If numbers of records are less, you cannot see a different one. Now apply this technique with a Yahoo email user accounts stored in a table called say YahooLogin. Let us assume there are 33 million users around the world that have Yahoo email id and that is stored in the YahooLogin. When a user logs in by giving the user name and password, the comparison required is 1 to 25, with the binary tree that is clustered index.

Look at the above picture and guess yourself how fast you will reach into the level 25. Without Clustered index, the comparison required is 1 to 33 millions.

The above explanation is for easy understanding. Now a days SQL server is using the B-Tree techniques to represent the clustered index. 

Got the usage of Clustered index? Let us move to Non-Clustered index. 

6. Non Clustered Index

A non-clustered index is useful for columns that have some repeated values. Say for example, AccountType column of a bank database may have 10 million rows. But, the distinct values of account type may be 10-15. A clustered index is automatically created when we create the primary key for the table. We need to take care of the creation of the non-clustered index.

Follow the steps below to create a Non-clustered index on our table Student based on the column class.

  1. After expanding the Student table, right click on the Indexes. And click on the New Index.


  2. From the displayed dialog, type the index name as shown below and then click on the Add button to select the column(s) that participate in the index. Make sure the Index type is Non-Clustered.


  3. In the select column dialog, place a check mark for the column class. This tells that we need a non-clustered index for the column Student.Class. You can also combine more than one column to create the Index. Once the column is selected, click on the OK button. You will return the dialog shown above with the selected column marked in blue. Our index has only one column. If you selected more than one column, using the MoveUp and MoveDown button, you can change order of the indexed columns. When you are using the combination of columns, always use the highly repeated column first and more unique columns down in the list. For example, let use assume the correct order for creating the Non-clustered index is: Class, DateOfBirth, PlaceOfBirth.


  4. Click on the Index folder on the right side and you will see the non-clustered index based on the column class is created for you.


7. How Does a Non-Clustered Index Work?

A table can have more than one Non-Clustered index. But, it should have only one clustered index that works based on the Binary tree concept. Non-Clustered column always depends on the Clustered column on the database.

This can be easily explained with the concept of a book and its index page at the end. Let us assume that you are going to a bookshop and found a big 1500 pages of C# book that says all about C#. When you glanced at the book, it has all beautiful color pages and shiny papers. But, that is not only the eligibility for a good book right? One you are impressed, you want to see your favorite topic of Regular Expressions and how it is explained in the book. What will you do? I just peeped at you from behind and recorded what you did as below:

  1. You went to the Index page (it has total 25 pages). It is already sorted and hence you easily picked up Regular Expression that comes on page Number 17.
  2. Next, you noted down the number displayed next to it which is 407, 816, 1200-1220.
  3. Your first target is Page 407. You opened a page in the middle, the page is greater than 500.
  4. Then you moved to a somewhat lower page. But it still reads 310.
  5. Then you moved to a higher page. You are very lucky you exactly got page 407. [Yes man you got it. Otherwise I need to write more. OK?]
  6. That’s all, you started exploring what is written about Regular expression on that page, keeping in mind that you need to find page 816 also.

In the above scenario, the Index page is Non-Clustered index and the page numbers are clustered index arranged in a binary tree. See how you came to the page 407 very quickly. Your mind actually traversed the binary tree way left and right to reach the page 407 quickly.

Here, the class column with distinct values 1,2,3..12 will store the clustered index columns value along with it. Say for example; Let us take only class value of 1. The Index goes like this:

1: 100, 104, 105

So here, you can easily get all the records that have value for class = 1. Map this with the Book index example now. See you all in the next article.


  • 26th March, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sivaraman Dhamodharan
Software Developer iSOFT
India India
I am working as software engineer in iSOFT R&D. I have been come across C++,MFC, .net technologies. I do like playing video games, reading books.


You may also be interested in...

Comments and Discussions

Generalgood article and nicely explained Pin
etechnosoft9-Nov-17 6:01
professionaletechnosoft9-Nov-17 6:01 
Questionthanks Pin
Member 1336488416-Aug-17 22:07
memberMember 1336488416-Aug-17 22:07 
GeneralMy vote of 5 Pin
Santhk5-Nov-15 4:23
professionalSanthk5-Nov-15 4:23 
GeneralMy vote of 4 Pin
Member 863592914-Oct-15 1:20
memberMember 863592914-Oct-15 1:20 
GeneralThanks Pin
Member 1185479015-Sep-15 9:51
memberMember 1185479015-Sep-15 9:51 
Questioncomment Pin
Member 1192054925-Aug-15 0:43
memberMember 1192054925-Aug-15 0:43 
GeneralMy vote of 4 Pin
bhupendra849-Jul-15 22:13
memberbhupendra849-Jul-15 22:13 
Questionthanks for sharing Pin
cynix0089-Jun-15 3:24
membercynix0089-Jun-15 3:24 
QuestionGreat article,beautiful explaination Pin
Member 117373622-Jun-15 19:59
memberMember 117373622-Jun-15 19:59 
QuestionNice Article-One Question Pin
Rahul Dhammy9-May-15 22:20
professionalRahul Dhammy9-May-15 22:20 
GeneralMy vote of 5 Pin
tungnt1855-Apr-15 23:28
membertungnt1855-Apr-15 23:28 
QuestionThanks Pin
Gajanans3-Mar-15 3:30
memberGajanans3-Mar-15 3:30 
GeneralMy vote of 5 Pin
Vishal_Kumar23-Feb-15 9:18
memberVishal_Kumar23-Feb-15 9:18 
GeneralMy vote of 1 Pin
Member 1105585523-Dec-14 0:54
memberMember 1105585523-Dec-14 0:54 
GeneralMy vote of 5 Pin
Pratik Bhuva9-Dec-14 23:28
professionalPratik Bhuva9-Dec-14 23:28 
Generalarticle Pin
Kalpesh M Jain10-Nov-14 23:34
memberKalpesh M Jain10-Nov-14 23:34 
GeneralVery well explained.. Pin
Sidharth Penta26-Sep-14 21:44
memberSidharth Penta26-Sep-14 21:44 
Questionaweome article.Author explained evweything in minute detail level. Pin
Member 1109951421-Sep-14 22:05
memberMember 1109951421-Sep-14 22:05 
GeneralMy vote of 4 Pin
Member 1107414517-Sep-14 1:09
memberMember 1107414517-Sep-14 1:09 
GeneralNice Articles Pin
sathis kumar22-Aug-14 3:34
membersathis kumar22-Aug-14 3:34 
GeneralGreat Explanation Pin
sahmed319-Aug-14 5:54
membersahmed319-Aug-14 5:54 
Member 109366049-Jul-14 21:02
memberMember 109366049-Jul-14 21:02 
Generalnice work Pin
Member 1021121322-May-14 3:04
memberMember 1021121322-May-14 3:04 
QuestionSuper explanation Pin
prem151319-Apr-14 19:32
memberprem151319-Apr-14 19:32 
GeneralConcept of Index Pin
GunjanJi5-Feb-14 19:07
memberGunjanJi5-Feb-14 19:07 
Generalgood Article Pin
naina1520-Jan-14 2:28
membernaina1520-Jan-14 2:28 
AnswerMessage Closed Pin
14-Jan-14 2:17
membersreeharikatageri14-Jan-14 2:17 
QuestionExample for Non Clustered Indexing Pin
Anand Saripalli26-Nov-13 2:08
memberAnand Saripalli26-Nov-13 2:08 
GeneralThnx Pin
Birendra Singh21-Nov-13 2:00
memberBirendra Singh21-Nov-13 2:00 
QuestionSimple and the examples made it quite easier to understand, Pin
Member 1024322730-Aug-13 10:03
memberMember 1024322730-Aug-13 10:03 
QuestionSimple and very useful explaination Pin
Parag Deshmukh12-Jul-13 3:23
memberParag Deshmukh12-Jul-13 3:23 
QuestionThanks Pin
Raja Ravi varaman9-Jul-13 3:18
memberRaja Ravi varaman9-Jul-13 3:18 
GeneralMy vote of 4 Pin
VikashPatel981111-Jun-13 9:09
memberVikashPatel981111-Jun-13 9:09 
GeneralMy vote of 5 Pin
Amir Mehrabi-Jorshari10-Jun-13 10:22
memberAmir Mehrabi-Jorshari10-Jun-13 10:22 
QuestionWhat is B Tree in Indexing Pin
Suchi Banerjee, Pune10-Jun-13 2:53
memberSuchi Banerjee, Pune10-Jun-13 2:53 
QuestionExcellent Pin
tusharkadu30-May-13 22:22
membertusharkadu30-May-13 22:22 
QuestionNon-Clustered works on report Pin
Sweetynewb29-May-13 20:53
memberSweetynewb29-May-13 20:53 
GeneralNice One Pin
Mayank Pratap Singh29-May-13 11:17
memberMayank Pratap Singh29-May-13 11:17 
GeneralMy vote of 5 Pin
Kunwar Saurabh27-May-13 22:38
memberKunwar Saurabh27-May-13 22:38 
GeneralMy vote of 5 Pin
sagarraut000726-May-13 21:06
membersagarraut000726-May-13 21:06 
GeneralMy vote of 5 Pin
pen56025-May-13 4:53
memberpen56025-May-13 4:53 
QuestionExcellent Article Pin
Member 1001759529-Apr-13 5:11
memberMember 1001759529-Apr-13 5:11 
GeneralMy vote of 5 Pin
Member 78375577-Apr-13 8:02
memberMember 78375577-Apr-13 8:02 
easy and interesting explanation
GeneralMy vote of 5 Pin
vikram0016-Apr-13 0:10
membervikram0016-Apr-13 0:10 
GeneralThanks Pin
Saurabh Verma22-Mar-13 3:08
memberSaurabh Verma22-Mar-13 3:08 
GeneralMy vote of 5 Pin
khalleo16-Mar-13 13:05
memberkhalleo16-Mar-13 13:05 
GeneralMy vote of 5 Pin
indurotech6-Mar-13 21:25
memberindurotech6-Mar-13 21:25 
GeneralMy vote of 4 Pin
kiran shettar21-Feb-13 0:29
memberkiran shettar21-Feb-13 0:29 
GeneralMy vote of 5 Pin
Tuyen Nguyen GB13-Feb-13 5:19
memberTuyen Nguyen GB13-Feb-13 5:19 
GeneralMy vote of 5 Pin
mumthajamal8-Jan-13 20:30
membermumthajamal8-Jan-13 20:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 5 Jun 2013
Article Copyright 2011 by Sivaraman Dhamodharan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid