Difference Between Clustered and Non-Clustered Index - GeeksforGeeks (2024)

In SQL Server, indexing plays a crucial role in enhancing data retrieval efficiency. Indexes are disk-based structures linked to tables or views, aiding in quicker row retrieval by organizing data in a structured manner. Two primary types of indexes in SQL Server are Clustered and Non-Clustered Indexes.

An index is a disk-based structure linked to a table or view that facilitates quicker row retrieval. A table or view’s table or view’s columns are used to create keys in an index. These keys are kept in a structure (B-tree) that enables SQL Server to quickly and effectively locate the row or rows that correspond to the key values.

Clustered Index

A clustered index is created only when both the following conditions are satisfied:

  • The data or file, that you are moving into secondary memory should be in sequential or sorted order.
  • There should be a key value, meaning it can not have repeated values.

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 a primary key. A clustered index is as same as a dictionary where the data is arranged in alphabetical order.

In a clustered index, the index contains a pointer to block but not direct data.

Difference Between Clustered and Non-Clustered Index - GeeksforGeeks (1)

Example of Clustered Index

Example of Clustered Index

If you apply the primary key to any column, then automatically it will become a clustered index.

Create Table

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.

Output

Difference Between Clustered and Non-Clustered Index - GeeksforGeeks (2)

Student table

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 a composite index.

Here, the Roll_No column serves as the primary key, automatically becoming the clustered index. The output of querying this table will present data in ascending order of Roll_No.

Key Differences:

Only one clustered index is allowed per table.

The clustered index directly affects the physical ordering of data.

It offers faster retrieval but may slow down insert and update operations.

Non-Clustered Index

The 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 the index of that book. No need to go through each and every page of a book.

The data is stored in one place, and the index is stored in another place. Since the data and non-clustered index is stored separately, then you can have multiple non-clustered indexes in a table.

In a non-clustered index, the index contains the pointer to data.

Difference Between Clustered and Non-Clustered Index - GeeksforGeeks (3)

Example of Non Clustered Index

Example of Non-Clustered Index

The given code creates a table “Student” with columns “Roll_No”, “Name”, “Gender”, and “Mob_No”. The primary key is defined on the “Roll_No” column. Three rows are inserted into the “Student” table with different values for the columns. Finally, a nonclustered index “NIX_FTE_Name” is created on the “Name” column in ascending order.

The “CREATE TABLE” statement is used to create a new table “Student” with four columns “Roll_No”, “Name”, “Gender”, and “Mob_No”. The “Roll_No” column is defined as the primary key of the table.

The “INSERT INTO” statements are used to insert three rows of data into the “Student” table. Each row contains values for all the columns of the table. The first row has a roll number of 4, the name of “Afzal”, the gender of “male”, and a mobile number of 9876543210. The second row has a roll number of 3, the name of “Sudhir”, the gender of “male”, and a mobile number of 9675432890. The third row has a roll number of 5, name of “zoya”, the gender of “female”, and a mobile number of 8976453201.

Query:

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 a non-clustered index in the NAME column (in ascending order), then a new table will be created for that column.

Output:

Difference Between Clustered and Non-Clustered Index - GeeksforGeeks (4)

Student table

The 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.

In this example, the Name column is indexed in ascending order, allowing for efficient retrieval based on names.

Key Differences:

Multiple non-clustered indexes are allowed per table.

Non-clustered indexes store data pointers, not the data itself.

They offer flexibility but may result in slower retrieval compared to clustered indexes.

Difference Between Clustered and Non-Clustered Index

CLUSTERED INDEXNON-CLUSTERED INDEX
A clustered index is faster.A non-clustered index is slower.
The clustered index requires less memory for operations.A non-Clustered index requires more memory for operations.
In a clustered index, the clustered index is the main data.In the Non-Clustered index, the index is the copy of data.
A table can have only one clustered index.A table can have multiple non-clustered indexes.
The clustered index has the inherent ability to store data on the disk.A non-Clustered index does not have the inherent ability to store data on the disk.
Clustered index store pointers to block not data.The non-clustered index stores both the value and a pointer to the actual row that holds the 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 contain included columns.
In a Clustered index, Clustered key defines the order of data within a table.In a Non-Clustered index, the index key defines the order of data within the 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 the logical order of the index does not match the physical stored order of the rows on the disk.
The size of The primary clustered index is large.The size of the non-clustered index is compared relativelyThe composite is smaller.
Primary Keys of the table by default are clustered indexes.The composite key when used with unique constraints of the table act as the non-clustered index.

Conclusion

In Conclusion, the decision between clustered and non-clustered indexes primarily depends on the particular requirements of your database.Choosing between clustered and non-clustered indexes depends on the specific requirements of your database:

Clustered indexes are ideal for range queries and static data.

Non-clustered indexes are suitable for optimizing various query types and dynamic data environments.

Understanding these indexing techniques empowers database administrators to optimize data retrieval and performance effectively.

Index Clustering

  • Perfect for tables where range queries, in particular, place a high value on data retrieval efficiency.
  • Ideal for tables with few updates or relatively static data because moving data around can slow down insert and update operations.

Non-Clustered index

  • allows for the optimization of various query types without changing the data’s physical order on disk.
  • Ideal for tables where data changes often since inserts and updates are typically quicker than with clustered indexes.


snigdha_yambadwar

Difference Between Clustered and Non-Clustered Index - GeeksforGeeks (6)

Improve

Previous Article

MySQL Unique Index

Next Article

Different types of MySQL Triggers (with examples)

Please Login to comment...

Difference Between Clustered and Non-Clustered Index - GeeksforGeeks (2024)
Top Articles
Germany Personal Savings Ratio
How to insert multiple rows in Google Sheets
Room Background For Zepeto
Danatar Gym
Pga Scores Cbs
Booknet.com Contract Marriage 2
The Realcaca Girl Leaked
Craigslist Pet Phoenix
Sinai Web Scheduler
83600 Block Of 11Th Street East Palmdale Ca
Hardly Antonyms
All Obituaries | Ashley's J H Williams & Sons, Inc. | Selma AL funeral home and cremation
Moe Gangat Age
4302024447
Pwc Transparency Report
Dignity Nfuse
Curry Ford Accident Today
Kayky Fifa 22 Potential
Aps Day Spa Evesham
Ein Blutbad wie kein anderes: Evil Dead Rise ist der Horrorfilm des Jahres
Between Friends Comic Strip Today
Highmark Wholecare Otc Store
Garnish For Shrimp Taco Nyt
Target Minute Clinic Hours
Mikayla Campinos: Unveiling The Truth Behind The Leaked Content
R Baldurs Gate 3
Table To Formula Calculator
Section 408 Allegiant Stadium
Federal Express Drop Off Center Near Me
Ewg Eucerin
Eegees Gift Card Balance
Planned re-opening of Interchange welcomed - but questions still remain
Boneyard Barbers
Gideon Nicole Riddley Read Online Free
Marine Forecast Sandy Hook To Manasquan Inlet
SOC 100 ONL Syllabus
10 games with New Game Plus modes so good you simply have to play them twice
Lyca Shop Near Me
Worcester County Circuit Court
Jetblue 1919
Kutty Movie Net
Fedex Passport Locations Near Me
Searsport Maine Tide Chart
UWPD investigating sharing of 'sensitive' photos, video of Wisconsin volleyball team
Large Pawn Shops Near Me
Huntsville Body Rubs
Yosemite Sam Hood Ornament
Bradshaw And Range Obituaries
Julies Freebies Instant Win
Is TinyZone TV Safe?
Generator für Fantasie-Ortsnamen: Finden Sie den perfekten Namen
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 6404

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.