|
SQL Server: Uniqueidentifier Primary Keys
Published: 1/21/2009 |
|
There is a lot of debate in the database architecture world about whether or not Uniqueidentifier types should be used as primary keys. Generally you will find that people fit into one of two camps: those who never use Uniqueidentifier primary keys, and those who always do. I generally fall into the latter camp, I prefer them to auto increment numeric fields for a variety of reasons, but the bottom line is that either can be more appropriate for any given situation. What's important is to properly evaluate the requirements of your system and / or table and use the correct tool for the job.
The arguments against using Uniqueidentifiers as primary keys in SQL Server tables generally have to do with their drawbacks. Here they are, itemized:
Most people see these arguments and quickly jump to the conclusion that auto incrementing integer values are better for primary key ID fields. However, I think those people are missing quite a bit. Specifically, the benefits of using GUIDs:
Honestly the arguments against using them are simply not all that powerful as the ones for using them. Disk space? Is that really much of a problem anymore? Debugging problems? That's what copy / paste is for. Slow performance on INSERT? Buy faster hardware, and create a clustered index on a different field - like DateInserted. That's actually better in a lot of ways.
I have designed and built a ton of systems in my time, ranging from small desktop applications with one user to huge enterprise level systems that processed tens of thousands of records an hour. I always use Uniqueidentifier primary key fields, and I have had virtually no problems. There was one instance where I would have been better off using a BIGINT auto increment value, but archiving old data solved that problem (INSERTS dragged when we got to 25 million records or so), and that's something we should have been doing anyway.
My point is this: while I prefer using Uniqueidentifier fields whenever possible, the best approach is to be flexible and open minded, and evaluate each situation to determine the best data type to use in that particular instance.
Questions or Comments? .
VB to C# and C# to VB translation provided by Instant C# and Instant VB.