<p>I've always preferred to use long integers as primary keys in databases, for simplicity and (assumed) speed. But when using a <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" rel="noreferrer">REST</a> or Rails-like URL scheme for object instances, I'd then end up with URLs like this:</p>

<pre><code>http://example.com/user/783
</code></pre>

<p>And then the assumption is that there are also users with IDs of 782, 781, ..., 2, and 1. Assuming that the web app in question is secure enough to prevent people entering other numbers to view other users without authorization, a simple sequentially-assigned surrogate key also "leaks" the total number of instances (older than this one), in this case users, which might be privileged information. (For instance, I am user #726 in stackoverflow.)</p>

<p>Would a <a href="http://en.wikipedia.org/wiki/Uuid" rel="noreferrer">UUID</a>/GUID be a better solution? Then I could set up URLs like this:</p>

<pre><code>http://example.com/user/035a46e0-6550-11dd-ad8b-0800200c9a66
</code></pre>

<p>Not exactly succinct, but there's less implied information about users on display. Sure, it smacks of "security through obscurity" which is no substitute for proper security, but it seems at least a little more secure.</p>

<p>Is that benefit worth the cost and complexity of implementing UUIDs for web-addressable object instances? I think that I'd still want to use integer columns as database PKs just to speed up joins.</p>

<p>There's also the question of in-database representation of UUIDs. I know MySQL stores them as 36-character strings. Postgres seems to have a more efficient internal representation (128 bits?) but I haven't tried it myself. Anyone have any experience with this?</p>

<hr>

<p>Update: for those who asked about just using the user name in the URL (e.g., <a href="http://example.com/user/yukondude" rel="noreferrer">http://example.com/user/yukondude</a>), that works fine for object instances with names that are unique, but what about the zillions of web app objects that can really only be identified by number? Orders, transactions, invoices, duplicate image names, stackoverflow questions, ...</p>

[{'answer_id': 5958, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 2, 'selected': False, 'text': "<p>I work with a student management system which uses UUID's in the form of an integer. They have a table which hold the next unique ID.</p>\n\n<p>Although this is probably a good idea for an architectural point of view, it makes working with on a daily basis difficult. Sometimes there is a need to do bulk inserts and having a UUID makes this very difficult, usually requiring writing a cursor instead of a simple SELECT INTO statement.</p>\n"}, {'answer_id': 5960, 'author': 'Bryan Roth', 'author_id': 299, 'author_profile': 'https://Stackoverflow.com/users/299', 'pm_score': 1, 'selected': False, 'text': "<p>I think using a GUID would be the better choice in your situation.  It takes up more space but it's more secure.</p>\n"}, {'answer_id': 5963, 'author': 'Douglas Tosi', 'author_id': 626, 'author_profile': 'https://Stackoverflow.com/users/626', 'pm_score': 6, 'selected': True, 'text': "<p>I can't say about the web side of your question. But uuids are great for n-tier applications. PK generation can be decentralized: each client generates it's own pk without risk of collision. \nAnd the speed difference is generally small.</p>\n\n<p>Make sure your database supports an efficient storage datatype (16 bytes, 128 bits).\nAt the very least you can encode the uuid string in base64 and use char(22).</p>\n\n<p>I've used them extensively with Firebird and do recommend.</p>\n"}, {'answer_id': 5967, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 5, 'selected': False, 'text': '<p>I can answer you that in SQL server if you use a uniqueidentifier (GUID) datatype and use the NEWID() function to create values you will get horrible fragmentation because of page splits. The reason is that when using NEWID() the value generated is not sequential. SQL 2005 added the NEWSEQUANTIAL() function to remedy that</p>\n\n<p>One way to still use GUID and int is to have a guid and an int in a table so that the guid maps to the int. the guid is used externally but the int internally in the DB</p>\n\n<p>for example</p>\n\n<pre><code>457180FB-C2EA-48DF-8BEF-458573DA1C10    1\n9A70FF3C-B7DA-4593-93AE-4A8945943C8A    2\n</code></pre>\n\n<p>1 and 2 will be used in joins and the guids in the web app. This table will be pretty narrow and should be pretty fast to query</p>\n'}, {'answer_id': 6052, 'author': 'Brian Lyttle', 'author_id': 636, 'author_profile': 'https://Stackoverflow.com/users/636', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t think a GUID gives you many benefits. Users hate long, incomprehensible URLs.</p>\n\n<p>Create a shorter ID that you can map to the URL, or enforce a unique user name convention (<a href="http://example.com/user/brianly" rel="nofollow noreferrer">http://example.com/user/brianly</a>). The guys at <a href="http://gettingreal.37signals.com/" rel="nofollow noreferrer">37Signals</a> would probably mock you for worrying about something like this when it comes to a web app.</p>\n\n<p>Incidentally you can force your database to start creating integer IDs from a base value.</p>\n'}, {'answer_id': 6062, 'author': 'Adam Tuttle', 'author_id': 751, 'author_profile': 'https://Stackoverflow.com/users/751', 'pm_score': 5, 'selected': False, 'text': "<p>For what it's worth, I've seen a long running stored procedure (9+ seconds) drop to just a few hundred milliseconds of run time simply by switching from GUID primary keys to integers. That's not to say <em>displaying</em> a GUID is a bad idea, but as others have pointed out, joining on them, and indexing them, by definition, is not going to be anywhere near as fast as with integers.</p>\n"}, {'answer_id': 6227, 'author': 'Josh', 'author_id': 257, 'author_profile': 'https://Stackoverflow.com/users/257', 'pm_score': 2, 'selected': False, 'text': "<p>Rather than URLs like this:</p>\n\n<pre><code>http://example.com/user/783\n</code></pre>\n\n<p>Why not have:</p>\n\n<pre><code>http://example.com/user/yukondude\n</code></pre>\n\n<p>Which is friendlier to humans and doesn't leak that tiny bit of information?</p>\n"}, {'answer_id': 9098, 'author': 'Andrea Bertani', 'author_id': 1005, 'author_profile': 'https://Stackoverflow.com/users/1005', 'pm_score': 2, 'selected': False, 'text': '<p>You could use an integer which is related to the row number but is not sequential. For example, you could take the 32 bits of the sequential ID and rearrange them with a fixed scheme (for example, bit 1 becomes bit 6, bit 2 becomes bit 15, etc..).<br>\nThis will be a bidirectional encryption, and you will be sure that two different IDs will always have different encryptions.<br>\nIt would obviously be easy to decode, if one takes the time to generate enough IDs and get the schema, but, if I understand correctly your problem, you just want to not give away information too easily.</p>\n'}, {'answer_id': 9164, 'author': 'Dan', 'author_id': 230, 'author_profile': 'https://Stackoverflow.com/users/230', 'pm_score': 1, 'selected': False, 'text': '<p>I think that this is one of these issues that cause quasi-religious debates, and its almost futile to talk about. I would just say use what you prefer. In 99% of systems it will no matter which type of key you use, so the benefits (stated in the other posts) of using one sort over the other will never be an issue. </p>\n'}, {'answer_id': 9236, 'author': 'Marius', 'author_id': 1008, 'author_profile': 'https://Stackoverflow.com/users/1008', 'pm_score': 2, 'selected': False, 'text': '<p>We use GUIDs as primary keys for all our tables as it doubles as the RowGUID for MS SQL Server Replication. Makes it very easy when the client suddenly opens an office in another part of the world...</p>\n'}, {'answer_id': 68255, 'author': 'Michael Barker', 'author_id': 6365, 'author_profile': 'https://Stackoverflow.com/users/6365', 'pm_score': 2, 'selected': False, 'text': "<p>It also depends on what you care about for your application.  For n-tier apps GUIDs/UUIDs are simpler to implement and are easier to port between different databases.  To produce Integer keys some database support a sequence object natively and some require custom construction of a sequence table.</p>\n\n<p>Integer keys probably (I don't have numbers) provide an advantage for query and indexing performance as well as space usage.  Direct DB querying is also much easier using numeric keys, less copy/paste as they are easier to remember.</p>\n"}, {'answer_id': 69586, 'author': 'Jonathan Arkell', 'author_id': 11052, 'author_profile': 'https://Stackoverflow.com/users/11052', 'pm_score': 3, 'selected': False, 'text': "<p>Why couple your primary key with your URI?</p>\n\n<p>Why not have your URI key be human readable (or unguessable, depending on your needs), and your primary index integer based, that way you get the best of both worlds.  A lot of blog software does that, where the exposed id of the entry is identified by a 'slug', and the numeric id is hidden away inside of the system.</p>\n\n<p>The added benefit here is that you now have a really nice URL structure, which is good for SEO.  Obviously for a transaction this is not a good thing, but for something like stackoverflow, it is important (see URL up top...).  Getting uniqueness isn't that difficult.  If you are really concerned, store a hash of the slug inside a table somewhere, and do a lookup before insertion.</p>\n\n<p><strong>edit:</strong> Stackoverflow doesn't quite use the system I describe, see Guy's comment below.</p>\n"}, {'answer_id': 15065086, 'author': 'user2106945', 'author_id': 2106945, 'author_profile': 'https://Stackoverflow.com/users/2106945', 'pm_score': -1, 'selected': False, 'text': "<p>As long as you use a DB system with efficient storage, HDD is cheap these days anyway...</p>\n\n<p>I know GUID's can be a b*tch to work with some times and come with some query overhead however from a security perspective they are a savior. </p>\n\n<p>Thinking security by obscurity they fit well when forming obscure URI's and building normalised DB's with Table, Record and Column defined security you cant go wrong with GUID's, try doing that with integer based id's.</p>\n"}, {'answer_id': 19129647, 'author': 'Daniel Alexiuc', 'author_id': 34553, 'author_profile': 'https://Stackoverflow.com/users/34553', 'pm_score': 2, 'selected': False, 'text': "<p>I've tried both in real web apps.</p>\n<p>My opinion is that it is preferable to use integers and have short, comprehensible URLs.</p>\n<p>As a developer, it feels a little bit awful seeing sequential integers and knowing that some information about total record count is leaking out, but honestly - most people probably don't care, and that information has never really been critical to my businesses.</p>\n<p>Having long ugly UUID URLs seems to me like much more of a turn off to normal users.</p>\n"}, {'answer_id': 51471301, 'author': 'Sousaplex', 'author_id': 5371123, 'author_profile': 'https://Stackoverflow.com/users/5371123', 'pm_score': 0, 'selected': False, 'text': '<p>YouTube uses 11 characters with base64 encoding which offers 11^64 possibilities, and they are usually pretty manageable to write. I wonder if that would offer better performance than a full on UUID. UUID converted to base 64 would be double the size I believe.</p>\n<p>More information can be found here: <a href="https://www.youtube.com/watch?v=gocwRvLhDf8" rel="nofollow noreferrer">https://www.youtube.com/watch?v=gocwRvLhDf8</a></p>\n'}, {'answer_id': 72440275, 'author': 'ZiiMakc', 'author_id': 10188661, 'author_profile': 'https://Stackoverflow.com/users/10188661', 'pm_score': 0, 'selected': False, 'text': '<h1>Pros and Cons of UUID</h1>\n<blockquote>\n<p>Note: <a href="https://github.com/kripod/uuidv7" rel="nofollow noreferrer">uuid_v7</a> is time based uuid instead of random. So you can\nuse it to order by creation date and solve <a href="https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-03.html#section-1-4" rel="nofollow noreferrer">some performance issues\nwith db inserts</a> if you do really many of them.</p>\n</blockquote>\n<p><strong>Pros</strong>:</p>\n<ul>\n<li>can be generated on api level (good for distributed systems)</li>\n<li>hides count information about entity</li>\n<li>doesn\'t have limit 2,147,483,647 as 32-bit int</li>\n<li>removes layer of errors related to passing one entity id <code>userId: 25</code> to get another <code>bookId: 25</code> accidently</li>\n<li>more friendly graphql usage as <code>ID</code> key</li>\n</ul>\n<p><strong>Cons</strong>:</p>\n<ul>\n<li>128-bit instead 32-bit int (slightly bigger size in db and ~40% bigger index, around ~30MB for 1 million rows), should be a minor concern</li>\n<li>can\'t be sorted by creation (<strong>can be solved with uuid_v7</strong>)</li>\n<li><a href="https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-03.html#name-introduction-5" rel="nofollow noreferrer">non-time-ordered UUID versions such as UUIDv4 have poor database index locality</a> (<strong>can be solved with uuid_v7</strong>)</li>\n</ul>\n<hr />\n<h1>URL usage</h1>\n<p>Depending on app you may care or not care about url. If you don\'t care, just use <code>uuid</code> as is, it\'s fine.</p>\n<p>If you care, then you will need to decide on url format.</p>\n<p>Best case scenario is a use of unique slug if you ok with never changing it:</p>\n<pre><code>http://example.com/sale/super-duper-phone\n</code></pre>\n<p>If your url is generated from title and you want to change slug on title change there is a few options. Use it as is and query by uuid (slug is just decoration):</p>\n<pre><code>http://example.com/book/035a46e0-6550-11dd-ad8b-0800200c9a66/new-title\n</code></pre>\n<p>Convert it to base64url:</p>\n<ul>\n<li>you can get uuid back from <code>AYEWXcsicACGA6PT7v_h3A</code></li>\n<li><code>AYEWXcsicACGA6PT7v_h3A</code> - 22 characters</li>\n<li><code>035a46e0-6550-11dd-ad8b-0800200c9a66</code> - 36 characters</li>\n</ul>\n<pre><code>http://example.com/book/AYEWXcsicACGA6PT7v_h3A/new-title\n</code></pre>\n<p>Generate a unique <a href="https://github.com/ai/nanoid" rel="nofollow noreferrer">short 11 chars</a> length string just for slug usage:</p>\n<pre><code>http://example.com/book/icACEWXcsAY-new-title\nhttp://example.com/book/icACEWXcsAY/new-title\n</code></pre>\n<p>If you don\'t want uuid or short id in url and want only slug, but do care about seo and user bookmarks, you will need to redirect all request from</p>\n<pre><code>http://example.com/sale/phone-1-title\n</code></pre>\n<p>to</p>\n<pre><code>http://example.com/sale/phone-1-title-updated\n</code></pre>\n<p>this will add additional complexity of managing slug history, adding fallback to history for all queries where slug is used and redirects if slugs doesn\'t match</p>\n'}]
['https://Stackoverflow.com/questions/5949', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/726/']