Careeroza
Clustered vs Non-clustered
Clustered Index
A clustered index decides the physical order of the rows: the table data is the index. There can be only one per table, usually the primary key.
- SQL Server: the primary key is clustered by default.
- MySQL InnoDB: rows are always clustered by the primary key.
- PostgreSQL: tables are heaps;
CLUSTERreorders once but is not maintained.
Non-clustered Index
A non-clustered index is a separate structure holding the indexed columns plus a pointer to the row. A table can have many.
| Clustered | Non-clustered | |
|---|---|---|
| Per table | one | many |
| Stores | the full rows, in key order | key + pointer to the row |
| Best for | range scans on the key, ORDER BY key | selective lookups on other columns |