Skip to content
We areTOP 1%

What is the difference between a clustered and a non-clustered index?

MediumConceptIndexing & Query Optimization
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

A clustered index decides the physical order in which a table's rows are stored: its leaf level contains the rows themselves. A non-clustered (secondary) index is a separate structure whose leaves hold the indexed values and a pointer to each row.

  • A table can have only one clustered index, because rows can be stored in only one order, but it can have many non-clustered indexes.
  • In MySQL InnoDB, the primary key is the clustered index, and secondary indexes store the primary key as their pointer, so a secondary lookup may need a second search in the clustered index.
  • SQL Server makes the primary key clustered by default. PostgreSQL stores tables as unordered heaps, so all of its indexes are non-clustered; its CLUSTER command reorders a table only once.
  • A non-clustered index that contains every column a query needs is a covering index and avoids the extra lookup.

Example: A telephone directory sorted by surname is like a clustered index: the entries themselves are in order. The index at the back of a textbook is like a non-clustered index: it lists topics alphabetically, with page numbers that point to the actual content.

More in Indexing & Query Optimization

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account