Tuesday, April 10, 2007

GUID vs Identity as primary key (SQL server)*

Tuesday, April 10, 2007 12:34:03 PM (GMT Standard Time, UTC+00:00)

While building a database lately, I was called to make the decision on a primary key field.. now the way I usually do database implementation is to use an Identity and let the auto-increment take care of it. This time though while playing around a bit before heading to that solution I came across the GUID field type (a type that is also used for primary key or.. unique key requirements). To give you a little background on it though, I've included the snippet below..

"An alternative to using an auto incrementing value is to use the NewGuid method of a Guid object to generate a GUID, or globally unique identifier... The NewGuid method generates a 16-byte binary value that is created using an algorithm that provides a high probability that no value will be duplicated. In a SQL Server database, a GUID is stored in a uniqueidentifier column..." (MSDN)

Now, let's look at this excerpt properly:

1. "generates a 16-byte binary value" .. this usually is in the format of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. Yea.. pretty long eh? That will mean some overhead will be involved in the lookups. Not exactly a feature you would like in searching, sorting, joining, etc. Also.. remember, computers work better with numbers, this GUID seems just like a long string.

2. "an algorithm that provides a high probability that no value will be duplicated" .. a.. high probability.. well, that's good .. but with Identity (auto-increment), there will be no duplications period.

Those are what I consider the top two reasons to probably stick with an Identity ( i.e. autoincrement value).

One of my key sources for insight into this topic was here. Even though there's no Author name (hence some might say it is not that 'credible'), at least he/she put links for where they sourced their information from.

Related posts:
Select a random row in MS SQL...
Regular expressions
VS2005, ASP.NET 2 & DLLs, DLLs..
MS's ASP.NET and.. PHP
2-way databinding cascading drop down lists within a FormView
ApplicationName Property when customising providers

Tuesday, April 10, 2007 3:54:08 PM (GMT Standard Time, UTC+00:00)
I'm not sure why this would be used instead of an auto incrementing value. I've personally never had any problems using that method, and this one just seems needlessly complex, and somewhat unnecessary. But, I'm sure it must serve some useful purpose (perhaps in some instances an auto incrementing id field is problematic?) otherwise I doubt they would've gone to all this trouble.
Tuesday, April 10, 2007 4:13:31 PM (GMT Standard Time, UTC+00:00)
After reading a bit more.. I found the following advantages for using GUID:

# Global databases that must guarantee unique key values across many servers
# Replicated databases that must guarantee unique key values across many servers
# Large databases that have outgrown incremental primary key values
# Companies faced with combining database files where primary key values are duplicated

And also, in the event that the users guessing the (autoincremented) PK could be a security issue.

This information was taken from: http://www.sitepoint.com/forums/showthread.php?t=468741
Jason Nurse
Comments are closed.