Clustered vs Non-Clustered Index: What is the simple way?

What are the major differences between Clustered Indexes and Non-Clustered Indexes, with examples, that you must know in SQL.

Indexes in relational databases help quickly retrieve data from a Relational Table. Querying on indexes helps you optimize the time taken for a query to retrieve results from the tables – Indexes can significantly improve the speed of query execution.

CREATE INDEX index_name
ON table_name (column1, column2, ...);

Technically, you can create two types of Indexes in a SQL database –

  • Clustered Indexes
  • Non-Clustered Indexes

You can create an Index over a Column as below –

What are Clustered Indexes?

A Clustered Index sorts the column into order and maintains a hashed index for every row. This approach is similar to a Dictionary where the values point to actual data. By default, a Clustered Index is created for a PRIMARY KEY column. Hence you can create only a single CLUSTERED INDEX per table.

What are Non-Clustered Indexes?

A Non-Clustered Index is created on non-key columns in the table. These are maintained in the form of a TREE with leaves pointing to actual data rows. Hence you need additional storage for maintaining these references and links. A table can contain more than one Non-Clustered Indexes. These are also comparatively slower when compared to a Clustered Index, because you’re actually traversing a tree instead of looking up in a Dictionary.

To Summarize –

  1. A CLUSTERED INDEX sorts the column and indexes similar to a DICTIONARY
  2. Here the values point to actual data
  3. By default, a CLUSTERED INDEX is created for a PRIMARY KEY column
  4. A table can contain only a single CLUSTERED INDEX
  5. A NON-CLUSTERED INDEX is created on non-key columns in the table, without any Sorting
  6. These are maintained in the form of a TREE with leaves pointing to actual data rows
  7. NON-CLUSTERED INDEXES take more space to maintain
  8. A table can contain more than one NON-CLUSTERED INDEXES
Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Privacy Overview
Referbruv

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

3rd Party Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.