Clustered vs Non-Clustered Indexes: What's the Difference?

Clustered vs non-clustered (secondary) indexes in MySQL, SQL Server and PostgreSQL: how rows are stored, key lookups, primary key choice and performance.

Published November 27, 2026

The terms clustered and non-clustered index describe where the actual row data lives. That single storage decision affects how fast primary key lookups are, how secondary indexes behave, how big they are, and how much your choice of primary key matters. The details differ between MySQL, SQL Server and PostgreSQL, so it is worth knowing each.

The concept

A clustered index stores the table's rows in the leaf level of the index itself, physically ordered by the index key. Because rows can only be stored in one order, a table can have at most one clustered index. In MySQL InnoDB the primary key is always the clustered index. In SQL Server you choose (by default the primary key).

A non-clustered (secondary) index is a separate structure holding the indexed columns plus a reference to the row. In InnoDB that reference is the primary key value; in SQL Server it is the clustering key or a row identifier; in PostgreSQL every index is non-clustered and points to the row's physical location in the heap. Finding a full row through a secondary index therefore needs a second step, called a key lookup, bookmark lookup or heap fetch.

The two-step lookup

key lookupreturnSecondary indexemail → PK 42Clustered indexPK 42 → full rowResultname, plan, …
In InnoDB, a secondary index lookup ends with a second descent into the clustered index.

Side by side

ClusteredNon-clustered (secondary)
Leaf containsThe full rowIndexed columns + row reference
How many per tableAt most oneMany
Physical row orderFollows the index keyIndependent
Range scans on keyVery fast, rows are adjacentFast on index, then lookups per row
Size impactIs the tableExtra storage; includes PK copy in InnoDB

How each database does it

DatabaseClustered?Secondary index points to
MySQL InnoDBAlways, by primary keyPrimary key value
SQL ServerOptional, one per tableClustering key (or RID for heaps)
PostgreSQLNo (CLUSTER is a one-time reorder)Heap tuple location (ctid)
OracleOptional (index-organised tables)ROWID or primary key
SQLiteRowid tables clustered by rowidrowid

Why InnoDB primary keys should be small

Every InnoDB secondary index stores a copy of the primary key in each entry. A BIGINT primary key adds 8 bytes per entry; a 36-character UUID string adds 36 or more. With five secondary indexes on a 100-million-row table, that difference is many gigabytes of extra index data and memory. Use compact, preferably increasing primary keys, and store UUIDs as BINARY(16) if you need them.

Practical examples

Range query that benefits from clustering

Orders clustered by (customer_id, id) keep each customer's orders on adjacent pages.

CREATE TABLE orders (
  customer_id BIGINT NOT NULL,
  id          BIGINT NOT NULL AUTO_INCREMENT,
  total       DECIMAL(10,2),
  created_at  DATETIME,
  PRIMARY KEY (customer_id, id),
  KEY (id)
) ENGINE=InnoDB;
SELECT * FROM orders WHERE customer_id = 42 ORDER BY id DESC LIMIT 20;

SQL Server: choosing a clustered index

Cluster on a narrow, increasing key; keep the PK constraint separate if needed.

CREATE TABLE dbo.Events (
  EventId   UNIQUEIDENTIFIER NOT NULL CONSTRAINT PK_Events PRIMARY KEY NONCLUSTERED,
  CreatedAt DATETIME2 NOT NULL,
  Payload   NVARCHAR(MAX)
);
CREATE CLUSTERED INDEX CIX_Events_CreatedAt ON dbo.Events (CreatedAt);

PostgreSQL: CLUSTER reorders once

Useful for read-mostly tables; new rows are not kept in order.

CLUSTER measurements USING idx_measurements_sensor_time;
ANALYZE measurements;

Common mistakes

  • Using a wide random string as the InnoDB primary key, bloating every secondary index.
  • Expecting PostgreSQL CLUSTER to keep data ordered as new rows arrive.
  • Assuming secondary index lookups are free; for many rows, key lookups can make a scan cheaper.
  • Changing a primary key value frequently in InnoDB, which physically moves the row and updates every secondary index.

Key takeaways

  • A clustered index stores the rows themselves in key order; there can be only one.
  • Secondary indexes store a reference and need a lookup to fetch the full row.
  • InnoDB always clusters by primary key; PostgreSQL never clusters persistently.
  • Keep clustered keys narrow, stable and preferably increasing.

FAQ

Is a primary key always clustered?

In InnoDB yes. In SQL Server by default, but you can declare it NONCLUSTERED. In PostgreSQL, no index is clustered.

How do I avoid key lookups?

Use a covering index that includes every column the query needs, so the secondary index alone answers it.

What happens if an InnoDB table has no primary key?

InnoDB uses the first NOT NULL unique index, or creates a hidden 6-byte row ID as the clustered key. Always define an explicit primary key.

More Databases articles