Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to use rowguid as unique key in database design?

SQL Server provides the type [rowguid]. I like to use this as unique primary key, to identify a row for update. The benefit shows up if you dump the table and reload it, no mess with SerialNo (identity) columns.

In the special case of distributed databases like offline copies on notebooks or something like that, nothing else works.

What do you think? Too much overhead?

like image 298
Ice Avatar asked Oct 30 '09 18:10

Ice


People also ask

Can I use GUID as primary key?

GUIDs can be considered as global primary keys. Local primary keys are used to uniquely identify records within a table. On the other hand, GUIDs can be used to uniquely identify records across tables, databases, and servers.

What is RowGUID in SQL?

RowGuids (uniqueidentifier) are large fields (16 bytes) and are basically going to ALWAYS be unique. SQL Server adds a RowGUID column to all tables if you are using Merge Replication (but doesn't add an index). RowGuids in general slow things down. Some people may consider using a RowGuid as their primary key.

What data type is Uniqueidentifier SQL Server?

The globally unique identifier (GUID) data type in SQL Server is represented by the uniqueidentifier data type, which stores a 16-byte binary value. A GUID is a binary number, and its main use is as an identifier that must be unique in a network that has many computers at many sites.

How do I create a unique identifier in SQL?

SQL Server NEWID to Generate GUID Type the below code in SSMS and execute. DECLARE @guid uniqueidentifier = NEWID(); SELECT @guid as 'GUID'; Here we created a variable named guid of data type uniqueidentifier.


2 Answers

As a primary key in the logical sense (uniquely identifying your rows) - yes, absolutely, makes total sense.

BUT: in SQL Server, the primary key is by default also the clustering key on your table, and using a ROWGUID as the clustering key is a really really bad idea. See Kimberly Tripp's excellent GUIDs as a PRIMARY and/or the clustering key article for in-depth reasons why not to use GUIDs for clustering.

Since the GUID is by definition random, you'll have a horrible index fragmentation and thus really really bad performance on insert, update, delete and select statements.

Also, since the clustering key is being added to each and every field of each and every non-clustered index on your table, you're wasting a lot of space - both on disk as well as in server RAM - when using 16-byte GUID vs. 4-byte INT.

So: yes, as a primary key, a ROWGUID has its merits - but if you do use it, definitely avoid using that column as your clustering key in the table! Use a INT IDENTITY() or something similar for that.

For a clustering key, ideally you should look for four features:

  • stable (never changing)
  • unique
  • as small as possible
  • ever-increasing

INT IDENTITY() ideally suits that need. And yes - the clustering key must be unique since it's used to physically locate a row in the table - if you pick a column that can't be guaranteed to be unique, SQL Server will actually add a four-byte uniqueifier to your clustering key - again, not something you want to have....

Check out The Clustered Index Debate Continues - another wonderful and insightful article by Kim Tripp (the "Queen of SQL Server Indexing") in which she explains all these requirements very nicely and thoroughly.

MArc

like image 161
marc_s Avatar answered Sep 30 '22 16:09

marc_s


The problem with rowguid is that if you use it for your clustered index you end up constantly re-calculating your table pages for record inserts. A sequential guid ( NEWSEQUENTIALID() ) often works better.

like image 41
Joel Coehoorn Avatar answered Sep 30 '22 16:09

Joel Coehoorn