SQL Server GUID as Primary Key

There are a few use cases for using a GUID as a primary key on a table. If you fall into one of those categories, make sure you are optimizing storage by properly defining an incrementing clustered index.

You can solve some problems by using NEWSEQUENTIALID() to generate your key, but that actually can make things worse. For example, this forces you to use a database-generated GUID, which means your app won't know the ID until the insert is done. It also means you can't migrate your records from one database to another without paging issues.

Here's a good read on the subject: GUIDs as PRIMARY KEYs and/or the clustering key

View code on GitHub

Comments

Popular posts from this blog

C# Record Serialization

Add timestamp to photo using ImageMagick

Read/write large blob to SQL Server from C#