12

It is connected to BI and merging of data from different data sources and would make that process more smooth.

And is there an optimal migration strategy from a database without Guids to a version with Guids without information losses?

8 Answers 8

20

Keep in mind that GUID's (or 'unique_identifier') for PK's is a bad choice, as many PK's have a clustered index (so all rows are stored on disk in the indexed order). As GUID's are random, it's not certain a new row will be appended at the end of the index, but could be inserted in the middle of the index. This causes disk trashing as the rows have to be moved.

IF you consider guid's, at least use sqlserver 2005 or up and NEWSEQUENTIALID() for the PK value, to get sequential guid's which are always bigger than the last one, so are always appended at the end of the index. If you're not using sqlserver (but for example postgresql or you're using oracle and use CHAR(32) or other type), consider COMB's (see: http://www.informit.com/articles/article.aspx?p=25862 )

1
  • If you have a table which is rarely inserted in to, then GUIDs can be really good. Its a tradeoff thing. May 3, 2009 at 11:05
14

Edited after reading Frans Bouma's answer, since my answer has been accepted and therefore moved to the top. Thanks, Frans.

GUIDs do make a good unique value, however due to their complex nature they're not really human-readable, which can make support difficult. If you're going to use GUIDs you might want to consider doing some performance analysis on bulk data operations before you make your choice. Take into account that if your primary key is "clustered" then GUIDs are not appropriate.

This is because a clustered index causes the rows to be physically re-ordered in the table on inserts/updates. Since GUIDs are random, every insert would require actual rows in the table to be moved to make way for the new row.

Personally I like to have two "keys" on my data:

1) Primary key
Unique, numeric values with a clustered primary key. This is my system's internal ID for each row, and is used to uniquely identify a row and in foreign keys.

Identity can cause trouble if you're using database replication (SQL Server will add a "rowguid" column automatically for merge-replicated tables) because the identity seed is maintained per server instance, and you'd get duplicates.

2) External Key/External ID/Business ID
Often it is also preferable to have the additional concept of an "external ID". This is often a character field with a unique constraint (possibly including another column e.g. customer identifier).

This would be the value used by external interfaces and would be exposed to customers (who do not recognise your internal values). This "business ID" allows customers to refer to your data using values that mean something to them.

2
  • 2
    "Take into account that if your primary key is "clustered" then GUIDs are not appropriate" - they are if you use newsequentialid() in SQL Server (see Frans' answer)
    – Dunc
    Dec 6, 2013 at 14:50
  • Clustering on GUIDs make very little sense, usually. Clustering is good when you are retrieving multiple rows with sequential, Clustered, values. Records have very seldom meaningful sequential GUIDs... May 29, 2017 at 12:17
4

You will probably need the facility to track back to source for auditing purposes, especially on financial data.

Even if you use synthetic keys in your warehouse system (which you almost certainly want to do if you have multiple data sources) you will still need to support auditing. Put a 'Data Source' and 'Natural Key' column on the tables in your system and populate them with a code for the source and a representation of whatever uniquely identifies the record at source.

If you do this the synthetic keys need only be ints or numerics wide enough to store enough values (ints if <4b rows, numerics if over). This means they will be much more readable than a GUID.

2

The following project may be of some use or at least inspire you to solve this problem.

https://github.com/twitter/snowflake

1

Anything that can uniquely identify the record is a good identity data type. GUID is generally good, but it's not the optimum identity if you actually have a unique id coming from the source data. GUID is a random integer value that's guaranteed to be unique; however, in an integration situation, you often want to detect duplicates of information, not just match up the records.

1

There is no "best" identity datatype. The various options have different strengths and weaknesses. I use GUIDs more often than not, but I have to deal regularly with disconnected clients and merge replication, so the choice is appropriate. If you don't have to deal with replication (i.e. the situation where a user adds new records while disconnected from the central database), an auto-incrementing int field is the better choice.

1

GUID are better in data replication scenarios, with the "identity" approach you have to be careful about not cause collisions between the data being replicated between Databases. Hope this helps.

0

I used to not like GUID at all, but I've grown to love it. I love it because it is relatively uniform and adopted, and I end up writing less code by using it, and maintaining that code, than I would normally write and maintain.

It is especially useful for storage of files, where you need to guarantee that a filename is unique, in a directory with a potentially large number of files, including pre-existing files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.