February 3, 2006 at 11:16 am
We need a unique value in a table column. The developer prefers to make the column a
uniqueidentifier type and populate it via Newid(). He doesn't like to use incrementing values such as 1,2,3, etc.
His argument is shown below. Do you agree with this or not? Is there a better way?
"As for a next-number-sequence, I just hate that
approach to unique key generation, and would
rather not do it this way. Here's the problem: in
a situation where two processes try to fire that
trigger that needs unique identifiers, if one
grabs the last max id but doesn't write the new
records before another process grabs the same last
max id you are left with bad data since the new id
both processes wrote to the table are the same.
Locking could fix this, but now you are forced to
use explicit locking (not a great proposition when
such a clean option [uuid] is available). I am
not a big fan of UUID myself, but in this
situation I think its the best solution from all
angles."
TIA,
Bill
February 3, 2006 at 11:21 am
The developer doesn't seem to be arguing about IDENTITY datatypes, but instead seems to be arguing based on the notion that s/he has to SELECT MAX(ID) + 1 As NextID.
Has s/he never heard of IDENTITY datatypes ? Or the waste of 12 bytes per key in a GUID solution ? Or the issues with page splits if the GUID is in a clustered index ?
February 3, 2006 at 5:52 pm
Concur... doesn't sound like the Developer ever heard of the IDENTITY property of a column. Using the IDENTITY property does have some other uses, as well... think of it as an automatic rank (as in "seniority") of when records were entered compared to each other, for one...
UNIQUEIDENTIFIERS have the advantage of using consistent lengths and are unique across all databases (and servers, too!). They do take 16 bytes which is twice the number of an INT. Also, no one can tell how many entries you may have because they are not sequential.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply