Guid as PK of Sql Server Table

75 views Asked by At

By default, a SQLServer table clustered index is the PK. If I define that:

  • such key is a GUID and
  • I will generate at random a.k.a Guid.NewGuid() and
  • forget to set the clustered index to a more meaningful column

will SQLServer reorder the pages as a new record enters the table or will it just "ignore" the clustered part of the index?

2

There are 2 answers

0
Code Different On

I think you got it reversed: by default, the PK is also the clustered index. You must choose the data type and what columns to include in the PK. SQL Server won't set a default PK for you. Without a PK, a table is a heap.

Using GUID as a PK is bad practice. You will cause unnecessary page splits upon INSERT. If the data does not have a natural key, use an IDENTITY column instead.

1
paparazzo On

So you create PK and it defaults to a clustered. You change it to a non clustered index. What makes you think there is a clustered index hanging around? There is no clustered part to the index if you changed it to a non clustered.