Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should primary keys be always assigned as clustered index

I have a SQLServer table that stores employee details, the column ID is of GUID type while the column EmployeeNumber of INT type. Most of the time I will be dealing with EmployeeNumber while doing joins and select criteria's.

My question is, whether is it sensible to assign PrimaryKey to ID column while ClusteredIndex to EmployeeNumber?

like image 721
AbrahamJP Avatar asked Jan 04 '11 19:01

AbrahamJP


People also ask

Should primary key always be clustered index?

Nope, it can be nonclustered.

Are Primary Keys clustered indexes?

The primary key is the default clustered index in SQL Server and MySQL. This implies a 'clustered index penalty' on all non-clustered indexes.

Can you create a table with primary key not as the clustered index?

Yes you can, by specifying the primary key be nonclustered. ALTER TABLE TableName ADD CONSTRAINT PK_name PRIMARY KEY NONCLUSTERED (ID); You make another index clustered by specifying a clustered index.

Is primary key clustered or non-clustered?

If you configure a PRIMARY KEY, Database Engine automatically creates a clustered index, unless a clustered index already exists. When you try to enforce a PRIMARY KEY constraint on an existing table and a clustered index already exists on that table, SQL Server enforces the primary key using a nonclustered index.


1 Answers

Yes, it is possible to have a non-clustered primary key, and it is possible to have a clustered key that is completely unrelated to the primary key. By default a primary keys gets to be the clustered index key too, but this is not a requirement.

The primary key is a logical concept: is the key used in your data model to reference entities.
The clustered index key is a physical concept: is the order in which you want the rows to be stored on disk.

Choosing a different clustered key is driven by a variety of factors, like key width when you desire a narrower clustered key than the primary key (because the clustered key gets replicated in every non-clustered index. Or support for frequent range scans (common in time series) when the data is frequently accessed with queries like date between '20100101' and '20100201' (a clustered index key on date would be appropriate).

This subject has been discussed here ad nauseam before, see also What column should the clustered index be put on?.

like image 150
Remus Rusanu Avatar answered Nov 14 '22 16:11

Remus Rusanu