In SQL Server, what is the difference between a clustered and a nonclustered index, and how many of each can a table have?
answer
- where the rows actually live
- only one physical order per table
- row locator: key or RID
- what PRIMARY KEY builds by default
basics
~20 sA SQL Server clustered index is the table itself: its leaf pages hold whole rows in key order, so a table has at most one. Nonclustered indexes are separate B+ trees pointing back to those rows, up to 999 per table.
solid answer
~40 sIn SQL Server the clustered index *is* the table: its leaf level holds every column of every row, ordered by the clustering key, so a table has one clustered index or none, and with none it is a **heap**. A nonclustered index is a separate B+ tree whose leaf rows hold the index key, any `INCLUDE` columns and a **row locator**: the clustering key on a clustered table, or an 8-byte RID (file, page, slot) on a heap. A table can have up to 999 nonclustered indexes, including the ones `PRIMARY KEY` and `UNIQUE` constraints create. The defaults matter: `CREATE INDEX` builds a nonclustered index unless you write `CLUSTERED`, while a `PRIMARY KEY` constraint normally becomes the clustered index, so declare it `NONCLUSTERED` when another column should order the table.
code
sql · 13 linesCREATE TABLE dbo.Customers
(
CustomerId int IDENTITY(1, 1) NOT NULL,
Email nvarchar(320) NOT NULL,
Country char(2) NOT NULL,
CONSTRAINT PK_Customers PRIMARY KEY (CustomerId) -- clustered by default
);
CREATE INDEX IX_Customers_Email ON dbo.Customers (Email); -- nonclustered by default
SELECT name, index_id, type_desc
FROM sys.indexes
WHERE object_id = OBJECT_ID('dbo.Customers');go deeper
Know that the clustered index holds the actual rows, that a table has at most one, and that CREATE INDEX makes a nonclustered index unless told otherwise.
Explain the row locator: clustering key on a clustered table, RID on a heap, and the uniqueifier on a non-unique clustered index. Mention the 999 nonclustered limit and the PRIMARY KEY default.
Show why the clustering key choice spreads to every nonclustered index on the table, and treat the clustered primary key as a choice you make on purpose rather than a default you inherit.
Frame the clustering key as a schema standard: what the team clusters on by default, when a heap or a nonclustered primary key is acceptable, and how expensive it is to change later.
## The clustered index is the table SQL Server stores ordinary disk-based tables in one of two shapes. If the table has a **clustered index**, that index is the table's storage: it is a B+ tree whose leaf pages are the data pages, holding every column of every row, ordered by the **clustering key**. Pages at each level are linked in a doubly linked list, and inside a page a *slot array* keeps rows in key order even though the bytes themselves may sit anywhere on the page. Because the rows can only be stored in one order, a table can have **only one** clustered index. If the table has no clustered index it is a **heap**: unordered data pages, each row identified by an 8-byte **RID** made of file number, page number and slot. Dropping a clustered index turns a table into a heap; the rows stay, only their organisation changes. Creating a clustered index on a heap rewrites the table in key order. The general trade-off between index-organised tables and heaps is covered with clustered versus heap storage in the engine-neutral material. What follows is how SQL Server spells and limits it. ## Nonclustered indexes and the row locator A **nonclustered index** is a second B+ tree holding a subset of the table's columns, usually in a different order. Its leaf rows contain: - the index **key columns**, which are sorted and can be sought on; - any **included columns** named in `INCLUDE (...)`, stored at the leaf only; - a **row locator** that leads back to the full row. The locator depends on what the table is: | Base table | Row locator in each nonclustered row | | --- | --- | | Heap | The RID (file:page:slot), 8 bytes | | Unique clustered index | The clustering key columns | | Non-unique clustered index | The clustering key plus a hidden 4-byte **uniqueifier** where a key value repeats | The engine never stores a column twice: if a clustering key column is already in the nonclustered key, it is not added again. Because the clustering key travels into every nonclustered index, its width multiplies across the whole table, which is why a narrow clustering key matters so much on SQL Server. ## Limits and defaults worth knowing - **One clustered index per table**, and up to **999 nonclustered indexes**, counting the indexes that `PRIMARY KEY` and `UNIQUE` constraints create. - An index key can combine up to **32 columns**; the key may be at most **900 bytes** for a clustered index and **1,700 bytes** for a nonclustered one (before SQL Server 2016 the limits were 16 columns and 900 bytes). - `CREATE INDEX` without a keyword builds a **nonclustered** index. - A `PRIMARY KEY` constraint defaults to **CLUSTERED**, unless a `UNIQUE` constraint in the same definition was declared `CLUSTERED`; a `UNIQUE` constraint defaults to **NONCLUSTERED**. - In `sys.indexes`, `index_id = 0` is the heap, `index_id = 1` is the clustered index, and nonclustered indexes have `index_id` greater than 1. That primary-key default is the one that surprises people. Teams that pick a random or wide primary key often get it as the clustering key without ever deciding to. You can write `PRIMARY KEY NONCLUSTERED` and create a separate clustered index on a better column. ## Reading it from the catalog ```sql SELECT i.name, i.index_id, i.type_desc, i.is_unique, i.is_primary_key FROM sys.indexes AS i WHERE i.object_id = OBJECT_ID('dbo.Customers'); ``` `type_desc` shows `HEAP`, `CLUSTERED`, `NONCLUSTERED` or one of the columnstore types. A table shows either one `HEAP` row or one clustered row (rowstore or columnstore) at `index_id` 0 or 1, never both, plus one row for each nonclustered index. ## Common confusions 1. **"Clustered means physically contiguous on disk."** The order is logical, kept by the page chain and each page's slot array. Pages can be out of physical order, which is what fragmentation measures. 2. **"The primary key is always clustered."** Only by default. A primary key can be nonclustered, and a clustered index does not have to be unique. 3. **"A heap can't have indexes."** It can have nonclustered indexes; they simply point at RIDs instead of clustering keys. 4. **"Several clustered indexes give several orders."** There is one clustered index per table. For a second order, use a nonclustered index, with `INCLUDE` columns if the queries need more columns. Seeing the clustered index as the table, and every nonclustered index as a pointer back into it, explains most of SQL Server's index behaviour: key lookups, the cost of a wide clustering key, and why changing the clustered index rebuilds every other index on the table.
- What does SQL Server add to a clustered index that is not declared unique?A hidden 4-byte uniqueifier, used where a key value repeats, so every row still has a unique locator. Because the clustering key is copied into every nonclustered index, the uniqueifier goes there too. That is one reason to make the clustering key unique, for example an IDENTITY column, or to add a unique column to the key.
- Does dropping the clustered index delete the table's data, and what does it cost?No. The table becomes a heap and keeps every row. But every nonclustered index must be rebuilt, because its row locators change from clustering keys to RIDs. On a large table that is a long, log-heavy operation, and creating a clustered index on a heap rebuilds them all in the same way.
A clustered index is like a dictionary: the entries are the content, kept in one alphabetical order. A nonclustered index is the index at the back of a textbook: a separate sorted list that tells you where to look, and you can have many of them.
- A table can have several clustered indexes on different columns.
- Clustered means the rows sit physically contiguous on disk in key order.
- In SQL Server every primary key is always the clustered index.
- A heap cannot have any nonclustered indexes.
- Nonclustered indexes on a clustered table store a page address, not the key.