Clustered vs Non-clustered Index: Key Differences with Example

What is an Index?

An Index is a key built from one or more columns in the database that speeds up fetching rows from the table or view. This key helps a Database like Oracle, SQL Server, MySQL, etc. to find the row associated with key values quickly.

Two types of Indexes are:

In this tutorial, you will learn:

What is a Clustered index?

Cluster index is a type of index which sorts the data rows in the table on their key values. In the Database, there is only one clustered index per table.

A clustered index defines the order in which data is stored in the table which can be sorted in only one way. So, there can be an only a single clustered index for every table. In an RDBMS, usually, the primary key allows you to create a clustered index based on that specific column.

What is Non-clustered index?

A Non-clustered index stores the data at one location and indices at another location. The index contains pointers to the location of that data. A single table can have many non-clustered indexes as an index in the non-clustered index is stored in different places.

For example, a book can have more than one index, one at the beginning which displays the contents of a book unit wise while the second index shows the index of terms in alphabetical order.

A non-clustering index is defined in the non-ordering field of the table. This type of indexing method helps you to improve the performance of queries that use keys which are not assigned as a primary key. A non-clustered index allows you to add a unique key for a table.

KEY DIFFERENCE

  • Cluster index is a type of index that sorts the data rows in the table on their key values whereas the Non-clustered index stores the data at one location and indices at another location.
  • Clustered index stores data pages in the leaf nodes of the index while Non-clustered index method never stores data pages in the leaf nodes of the index.
  • Cluster index doesn’t require additional disk space whereas the Non-clustered index requires additional disk space.
  • Cluster index offers faster data accessing, on the other hand, Non-clustered index is slower.

Characteristic of Clustered Index

Characteristics of Non-clustered Indexes

An example of a clustered index

In the example below, SalesOrderDetailID is the clustered index. Sample query to retrieve data

SELECT CarrierTrackingNumber, UnitPrice
FROM SalesData
WHERE SalesOrderDetailID = 6

An example of a non-clustered index

In the below example, a non-clusted index is created on OrderQty and ProductID as follows

CREATE INDEX myIndex ON
SalesData (ProductID, OrderQty)

The following query will be retrieved faster compared to the clustered index.

SELECT Product ID, OrderQty
FROM SalesData
WHERE ProductID = 714

Differences between Clustered Index and NonClustered Index

Parameters Clustered Non-clustered
Use for You can sort the records and store clustered index physically in memory as per the order. A non-clustered index helps you to creates a logical order for data rows and uses pointers for physical data files.
Storing method Allows you to stores data pages in the leaf nodes of the index. This indexing method never stores data pages in the leaf nodes of the index.
Size The size of the clustered index is quite large. The size of the non-clustered index is small compared to the clustered index.
Data accessing Faster Slower compared to the clustered index
Additional disk space Not Required Required to store the index separately
Type of key By Default Primary Keys Of The Table is a Clustered Index. It can be used with unique constraint on the table which acts as a composite key.
Main feature A clustered index can improve the performance of data retrieval. It should be created on columns which are used in joins.

Advantages of Clustered Index

The pros/benefits of the clustered index are:

Advantages of Non-clustered index

Pros of using non-clustered index are:

Disadvantages of Clustered Index

Here, are cons/drawbacks of using clustered index:

Disadvantages of Non-clustered index

Here, are cons/drawbacks of using non-clustered index:

 

YOU MIGHT LIKE: