<p>I've worked on a number of database systems in the past where moving entries between databases would have been made a lot easier if all the database keys had been <a href="http://en.wikipedia.org/wiki/Globally_Unique_Identifier" rel="noreferrer">GUID / UUID</a> values. I've considered going down this path a few times, but there's always a bit of uncertainty, especially around performance and un-read-out-over-the-phone-able URLs.</p>

<p>Has anyone worked extensively with GUIDs in a database? What advantages would I get by going that way, and what are the likely pitfalls?</p>

[{'answer_id': 45418, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 4, 'selected': False, 'text': '<p>The main advantages are that you can create unique id\'s without connecting to the database. And id\'s are globally unique so you can easilly combine data from different databases. These seem like small advantages but have saved me a lot of work in the past.</p>\n\n<p>The main disadvantages are a bit more storage needed (not a problem on modern systems) and the id\'s are not really human readable. This can be a problem when debugging.</p>\n\n<p>There are some performance problems like index fragmentation. But those are easilly solvable (comb guids by jimmy nillson: <a href="http://www.informit.com/articles/article.aspx?p=25862" rel="noreferrer">http://www.informit.com/articles/article.aspx?p=25862</a> )</p>\n\n<p><em>Edit</em> merged my two answers to this question</p>\n\n<p>@Matt Sheppard I think he means that you can duplicate rows with different GUIDs as primary keys. This is an issue with any kind of surrogate key, not just GUIDs. And like he said it is easilly solved by adding meaningfull unique constraints to non-key columns. The alternative is to use a natural key and those have real problems..</p>\n'}, {'answer_id': 45433, 'author': 'Troels Arvin', 'author_id': 4462, 'author_profile': 'https://Stackoverflow.com/users/4462', 'pm_score': 4, 'selected': False, 'text': '<p>GUIDs may cause you a lot of trouble in the future if they are used as "uniqifiers", letting duplicated data get into your tables. If you want to use GUIDs, please consider still maintaining UNIQUE-constraints on other column(s).</p>\n'}, {'answer_id': 45465, 'author': 'Troels Arvin', 'author_id': 4462, 'author_profile': 'https://Stackoverflow.com/users/4462', 'pm_score': 4, 'selected': False, 'text': '<p>@Matt Sheppard:</p>\n\n<p>Say you have a table of customers. Surely you don\'t want a customer to exist in the table more than once, or lots of confusion will happen throughout your sales and logistics departments (especially if the multiple rows about the customer contain different information).</p>\n\n<p>So you have a customer identifier which uniquely identifies the customer and you make sure that the identifier is known by the customer (in invoices), so that the customer and the customer service people have a common reference in case they need to communicate. To guarantee no duplicated customer records, you add a uniqueness-constraint to the table, either through a primary key on the customer identifier or via a NOT NULL + UNIQUE constraint on the customer identifier column.</p>\n\n<p>Next, for some reason (which I can\'t think of), you are asked to add a GUID column to the customer table and make that the primary key. If the customer identifier column is now left without a uniqueness-guarantee, you are asking for future trouble throughout the organization because the GUIDs will always be unique.</p>\n\n<p>Some "architect" might tell you that "oh, but we handle the <em>real</em> customer uniqueness constraint in our app tier!". Right. Fashion regarding that general purpose programming languages and (especially) middle tier frameworks changes all the time, and will generally never out-live your database. And there is a very good chance that you will at some point need to access the database without going through the present application. == Trouble. (But fortunately, you and the "architect" are long gone, so you will not be there to clean up the mess.) In other words: Do maintain obvious constraints in the database (and in other tiers, as well, if you have the time).</p>\n\n<p>In other words: There may be good reasons to add GUID columns to tables, but please don\'t fall for the temptation to make that lower your ambitions for consistency within the <em>real</em> (==non-GUID) information.</p>\n'}, {'answer_id': 45479, 'author': 'Nic Wise', 'author_id': 2947, 'author_profile': 'https://Stackoverflow.com/users/2947', 'pm_score': 9, 'selected': True, 'text': "<p>Advantages:</p>\n<ul>\n<li>Can generate them offline.</li>\n<li>Makes replication trivial (as opposed to int's, which makes it REALLY hard)</li>\n<li>ORM's usually like them</li>\n<li>Unique across applications. So We can use the PK's from our CMS (guid) in our app (also guid) and know we are NEVER going to get a clash.</li>\n</ul>\n<p>Disadvantages:</p>\n<ul>\n<li>Larger space use, but space is cheap(er)</li>\n<li>Can't order by ID to get the insert order.</li>\n<li>Can look ugly in a URL, but really, WTF are you doing putting a REAL DB key in a URL!? (This point disputed in comments below)</li>\n<li>Harder to do manual debugging, but not that hard.</li>\n</ul>\n<p>Personally, I use them for most PK's in any system of a decent size, but I got &quot;trained&quot; on a system which was replicated all over the place, so we HAD to have them. YMMV.</p>\n<p>I think the duplicate data thing is rubbish - you can get duplicate data however you do it. Surrogate keys are usually frowned upon where ever I've been working. We DO use the WordPress-like system though:</p>\n<ul>\n<li>unique ID for the row (GUID/whatever). Never visible to the user.</li>\n<li>public ID is generated ONCE from some field (e.g. the title - make it the-title-of-the-article)</li>\n</ul>\n<p><strong>UPDATE:</strong>\nSo this one gets +1'ed a lot, and I thought I should point out a big downside of GUID PK's: Clustered Indexes.</p>\n<p>If you have a lot of records, and a clustered index on a GUID, your insert performance will SUCK, as you get inserts in random places in the list of items (that's the point), not at the end (which is quick).</p>\n<p>So if you need insert performance, maybe use a auto-inc INT, and generate a GUID if you want to share it with someone else (e.g., showing it to a user in a URL).</p>\n"}, {'answer_id': 47146, 'author': 'Andrei Rînea', 'author_id': 1796, 'author_profile': 'https://Stackoverflow.com/users/1796', 'pm_score': 4, 'selected': False, 'text': "<p>Why doesn't anyone mention performance? When you have multiple joins, all based on these nasty GUIDs the performance will go through the floor, been there :(</p>\n"}, {'answer_id': 68966, 'author': 'WIDBA', 'author_id': 10868, 'author_profile': 'https://Stackoverflow.com/users/10868', 'pm_score': 3, 'selected': False, 'text': '<p>One other small issue to consider with using GUIDS as primary keys if you are also using that column as a clustered index (a relatively common practice).  You are going to take a hit on insert because of the nature of a guid not begin sequential in anyway, thus their will be page splits, etc when you insert.  Just something to consider if the system is going to have high IO...</p>\n'}, {'answer_id': 19604487, 'author': 'wener', 'author_id': 1870054, 'author_profile': 'https://Stackoverflow.com/users/1870054', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html" rel="nofollow noreferrer">primary-keys-ids-versus-guids</a></p>\n<p><a href="http://www.informit.com/articles/printerfriendly.asp?p=25862&amp;rl=1" rel="nofollow noreferrer">The Cost of GUIDs as Primary Keys</a> (SQL Server 2000)</p>\n<p><a href="http://krow.livejournal.com/497839.html" rel="nofollow noreferrer">Myths, GUID vs. Autoincrement</a> (MySQL 5)</p>\n<p>This is realy what you want.</p>\n<p><strong>UUID Pros</strong></p>\n<ul>\n<li>Unique across every table, every database, every server</li>\n<li>Allows easy merging of records from different databases</li>\n<li>Allows easy distribution of databases across multiple servers</li>\n<li>You can generate IDs anywhere, instead of having to roundtrip to the database</li>\n<li>Most replication scenarios require GUID columns anyway</li>\n</ul>\n<p><strong>GUID Cons</strong></p>\n<ul>\n<li>It is a whopping 4 times larger than the traditional 4-byte index value; this can have serious performance and storage implications if you\'re not careful</li>\n<li>Cumbersome to debug (where userid=\'{BAE7DF4-DDF-3RG-5TY3E3RF456AS10}\')</li>\n<li>The generated GUIDs should be partially sequential for best performance (eg, newsequentialid() on SQL 2005) and to enable use of clustered indexes</li>\n</ul>\n'}, {'answer_id': 47557946, 'author': 'Antti Haapala -- Слава Україні', 'author_id': 918959, 'author_profile': 'https://Stackoverflow.com/users/918959', 'pm_score': 2, 'selected': False, 'text': '<p>There is one thing that is not really addressed, namely using <strong>random</strong> (UUIDv4) IDs as primary keys will harm the performance of the <em>primary key index</em>. It will happen whether or not your table is clustered around the key.</p>\n\n<p>RDBMs usually ensure the uniqueness of the primary keys, and ensure the lookups by a key, in a structure called BTree, which is a search tree with a large branching factor (a binary search tree has branching factor of 2). Now, a sequential integer ID would cause the inserts to occur just <em>one</em> side of the tree, leaving most of the leaf nodes untouched. Adding random UUIDs will cause the insertions to split leaf nodes all over the index. </p>\n\n<p>Likewise if the data stored is mostly temporal, it is often the case that the most recent data needs to be accessed and joined against the most. With random UUIDs the patterns will not benefit from this, and will hit more index rows, thereby needing more of the index pages in memory. With sequential IDs if the most-recent data is needed the most, the hot index pages would require less RAM.</p>\n'}, {'answer_id': 63122268, 'author': 'Md. Samim hossain', 'author_id': 10401602, 'author_profile': 'https://Stackoverflow.com/users/10401602', 'pm_score': 2, 'selected': False, 'text': "<p>Advantages:</p>\n<ul>\n<li>UUID values are unique between tables and databases. Thats why it can be merge rows between two databases or distributed databases.</li>\n<li>UUID is more safer to pass through url than integer type data.\nIf one pass UUID through url, attackers can't guess the next id.But if we pass Integer type such as 10, then attackers can guess the next id is 11 then 12 etc.</li>\n<li>UUID can generate offline.</li>\n</ul>\n"}, {'answer_id': 71069249, 'author': 'J Scott', 'author_id': 2932782, 'author_profile': 'https://Stackoverflow.com/users/2932782', 'pm_score': 1, 'selected': False, 'text': "<p>One thing not mentioned so far: UUIDs make it much harder to profile data</p>\n<p>For web apps at least, it's common to access a resource with the id in the url, like <code>stackoverflow.com/questions/45399</code>. If the id is an integer, this both</p>\n<ul>\n<li>provides information about the number of questions (ie September 5th, 2008, the 45,399th question was asked)</li>\n<li>provides a leverage point to iterate through questions (what happens when I increment that by 1? I open the next asked question)</li>\n</ul>\n<p>From the first point, I can combine the timestamp from the question and the number to profile how frequently questions are asked and how that changes over time. this matters less on a site like Stack Overflow, with publicly available information, but, depending on context, this may expose sensitive information.</p>\n<p>For example, I am a company that offers customers a permissions gated portal. the address is <code>portal.com/profile/{customerId}</code>. If the id is an integer, you could profile the number of customers regardless of being able to see their information by querying for <code>lastKnownCustomerCount + 1</code> regularly, and checking if the result is <code>404 - NotFound</code> (customer does not exist) or <code>403 - Forbidden</code> (customer does exist, but you do not have access to view).</p>\n<p>UUIDs non-sequential nature mitigate these issues. This isn't a garunted to prevent profiling, but it's a start.</p>\n"}]
['https://Stackoverflow.com/questions/45399', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/797/']