Prerequisite – SQL queries on clustered and non-clustered Indexes
1. Clustered Index :
Clustered index is created only when both the following conditions satisfy –
- The data or file, that you are moving into secondary memory should be in sequential or sorted order.
- There should be non key value, means the data should be unique.
Whenever you apply clustered indexing in a table, it will perform sorting in that table only. You can create only one clustered index in a table like primary key. Clustered index is as same as dictionary where the data is arranged by alphabetical order.
In clustered index, index contains pointer to block but not direct data.
Example of Clustered Index –
If you apply primary key to any column, then automatically it will become clustered index.
create table Student ( Roll_No int primary key, Name varchar(50), Gender varchar(30), Mob_No bigint ); insert into Student values (4, 'ankita', 'female', 9876543210 ); insert into Student values (3, 'anita', 'female', 9675432890 ); insert into Student values (5, 'mahima', 'female', 8976453201 );
In this example, Roll no is a primary key, it will automatically act as a clustered index.
The output of this code will produce in increasing order of roll no.
You can have only one clustered index in one table, but you can have one clustered index on multiple columns, and that type of index is called composite index.
2. Non-clustered Index :
Non-Clustered Index is similar to the index of a book. The index of a book consists of a chapter name and page number, if you want to read any topic or chapter then you can directly go to that page by using index of that book. No need to go through each and every page of a book.
The data is stored in one place, and index is stored in another place. Since, the data and non-clustered index is stored separately, then you can have multiple non-clustered index in a table.
In non-clustered index, index contains the pointer to data.
Example of Non-clustered Index –
create table Student ( Roll_No int primary key, Name varchar(50), Gender varchar(30), Mob_No bigint ); insert into Student values (4, 'afzal', 'male', 9876543210 ); insert into Student values (3, 'sudhir', 'male', 9675432890 ); insert into Student values (5, 'zoya', 'female', 8976453201 ); create nonclustered index NIX_FTE_Name on Student (Name ASC);
Here, roll no is a primary key, hence there is automatically a clustered index.
If we want to apply non-clustered index in NAME column (in ascending order), then the new table will be created for that column.
Output before applying non-clustered index :
Output after applying non-clustered index :
Row address is used because, if someone wants to search the data for sudhir, then by using the row address he/she will directly go to that row address and can fetch the data directly.
Difference between Clustered and Non-clustered index :
|CLUSTERED INDEX||NON-CLUSTERED INDEX|
|Clustered index is faster.||Non-clustered index is slower.|
|Clustered index requires less memory for operations.||Non-Clustered index requires more memory for operations.|
|In clustered index, index is the main data.||In Non-Clustered index, index is the copy of data.|
|A table can have only one clustered index.||A table can have multiple non-clustered index.|
|Clustered index has inherent ability of storing data on the disk.||Non-Clustered index does not have inherent ability of storing data on the disk.|
|Clustered index store pointers to block not data.||Non-Clustered index store both value and a pointer to actual row that holds data.|
|In Clustered index leaf nodes are actual data itself.||In Non-Clustered index leaf nodes are not the actual data itself rather they only contains included columns.|
|In Clustered index, Clustered key defines order of data within table.||In Non-Clustered index, index key defines order of data within index.|
|A Clustered index is a type of index in which table records are physically reordered to match the index.||A Non-Clustered index is a special type of index in which logical order of index does not match physical stored order of the rows on disk.|
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.