Data / Contributed

The Difference between Clustered and Non-Clustered SQL Indexes

1 Oct 2019 10:25am, by

Vikash Kumar
Vikash Kumar writes articles on technical topics while working in a software development company Tatvasoft.com. You can follow him on Twitter and LinkedIn.

Indexes are used to speed the query process in the SQL, which results in higher performance. They are similar to the textbook indexes where you need to go to a specific chapter, find the page number of the chapter and directly visit that page. It can be troublesome to find the desired chapter without indexes and the process could be much slower. The relational database management system (RDBMS) has to go through all the records in the table to get the desired results if there are no indexes. This method is called table-scanning.

An index is a key that helps a database like MySQL, Oracle and SQL Server to find the associated row with the accurate key values in real-time. Basically, there are two different types of indexes used in the SQL servers, clustered and non-clustered.

In this article, we will take a look at the difference between these two indexes in a relational database.

What Is a Clustered Index?

A clustered index is a kind of file that sorts the information in the table on their key qualities. There is just one bunched list for every table in the database. Consequently, there is just a solitary grouped record for each table. Here are a few characteristics of the clustered index:

  • Default and sorted data storage
  • Helps to store Data and index at the same time
  • Clustered index scan and index seek
  • Key Lookup

Illustrative B-Tree Structure added below on how clustered Indexes work:

Image source: red-gate

Advantages:

  • Clustered indexes are an ideal option for range or group that get min, max or count type queries.
  • Any relevant search goes directly to a particular point in the information with the goal that you can continue perusing successively from the table.
  • It utilizes the location mechanism to find the file section toward the start of the range.
  • A compelling technique for range searches when a hunt key qualities are mentioned.
  • Minimize the page transfers and maximize the cache hits.

Disadvantages:

  • Limit the page moves and augment the reserve searches.
  • Different methods for non-successive request
  • Makes a lot of consistent pages parts including information page just as list pages.
  • Additional work for inserts, updates, and deletes in SQL.
  • Sets aside longer effort to refresh the records.
  • Almost all the data is contained in the lead nodes of the index.

What Is a Non-Clustered Index?

A non-clustered index stores the meta-information in one area and files in another area. The file comprises of the pointers to the location of that information. A solitary table can have numerous non-bunched records as a file. A non-clustering index is defined in the non-ordering field of the table and this type of method improves the performance of the queries as they use keys that are not assigned as a primary key. Here are a few characteristics of the clustered index:

  • Stores only key values
  • Pointers to Heap/Clustered Index rows
  • Allows to access Secondary data
  • Bridging data
  • Operations of Index Scan and Index Seek
  • Create a nonclustered index for a table or view

Advantages:

  • It helps to retrieve the data on a quick basis from the database table.
  • Overhead costs can be avoided as compared to the clustered index.
  • It can be used to create more than one index as multiple non-clustered indexes are there in RDBMS.

Disadvantages:

  • Store the data in the most logical order without permitting to sort the data rows physically.
  • Lookup process on the non-clustered index is a bit expensive.
  • A corresponding update is required every time the clustering key is updates so as to store it on the non-clustered index.

Illustrative B-Tree Structure added below on how non-clustered indexes work:

Non-Clustered Indexes store the clustering key for data Lookup in Clustered Index.

What’s the Difference Between these Indexes?

Specifications Clustered
Non-Clustered
Storage Stores the data pages of the index in the leaf nodes. It does not permit storing data pages of the index in the leaf nodes.
Size Quite large Small
Access to Data Quick and Faster Slower
Extra Disk Storage Not needed Required to separately store the data.
Key types The primary key of the table is a clustered index by default. Used with a unique constraint acting as a composite key on the table.
Attractive feature Performance of data retrieval can be improved. Creates the columns that are used in joins.

 

Feature image via Pixabay.

A newsletter digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.