What's your opinion on using UUIDs as database row identifiers, particularly in web apps?

后端 未结 15 2127
深忆病人
深忆病人 2020-12-12 12:43

I\'ve always preferred to use long integers as primary keys in databases, for simplicity and (assumed) speed. But when using a REST or Rails-like URL scheme for object insta

相关标签:
15条回答
  • 2020-12-12 13:34

    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 displaying 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.

    0 讨论(0)
  • 2020-12-12 13:34

    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.

    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.

    0 讨论(0)
  • 2020-12-12 13:35

    Youtube uses 11 characters with base64 encoding which offers 11^64 posibilities, 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.

    More information can be found here: https://www.youtube.com/watch?v=gocwRvLhDf8

    0 讨论(0)
  • 2020-12-12 13:36

    As long as you use a DB system with efficient storage, HDD is cheap these days anyway...

    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.

    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.

    0 讨论(0)
  • 2020-12-12 13:39

    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..).
    This will be a bidirectional encryption, and you will be sure that two different IDs will always have different encryptions.
    It 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.

    0 讨论(0)
  • 2020-12-12 13:39

    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...

    0 讨论(0)
提交回复
热议问题