What are the reasons *not* to use a GUID for a primary key?

后端 未结 8 1763
梦谈多话
梦谈多话 2020-12-24 02:24

Whenever I design a database I automatically start with an auto-generating GUID primary key for each of my tables (excepting look-up tables)

I know I\'ll never lose

相关标签:
8条回答
  • 2020-12-24 02:48

    I can see the case for a given application's or enterprise's own identifiers to be unique and be represented in a consistent way across all its own domains (i.e. because they may span more than one database) but a GUID is overkill for these purposes. I guess they are popular because they are available out of the box and designing and implementing an 'enterprise key' takes time and effort. The rule when designing an artifical identifier is to make it as simple as possible but no simpler. IDENTITY is too simple, a GUID isn't simple enough.

    Entities that exist outside of the application/enterprise usually have their own identifiers (e.g. a car has a VIN, a book has an ISBN, etc) maintained by an external trusted source and in such cases the GUID adds nothing. So I guess the philosphical argument against I'm getting at here is that using a artifical identifier on every table is unnecessary.

    0 讨论(0)
  • 2020-12-24 02:52

    Simple answer: it's not relational.

    The record (as defined by the GUID) may be unique, but none of the associated attributes can be said to be occuring uniquely with that record.

    Using a GUID (or any purely surrogate key) is no more relational than declaring a flat file to be relational, on the basis that each record can be identified by its row number.

    0 讨论(0)
  • 2020-12-24 03:04

    A potentially big reason, but one often not thought of, is if you might have to provide compatibility with an Oracle database in the future.

    Since Oracle doesn't have a uniqueid column data type, it can lead to a bit of a nightmare when you have two different data types for the same primary key across two different databases, especially when an ORM is involved.

    0 讨论(0)
  • 2020-12-24 03:07

    Adding to ewwwn:

    Pros

    • It makes it nearly impossible for developers to "accidentally" expose the surrogate key to users (unlike integers where it happens almost all the time).
    • Makes merging databases several orders of magnitude simpler than dealing with identity columns.

    Cons

    • Fatter. The real problem with it being fatter is that it eats up more space per page and more space in your indexes making them slower. The additional storage space of Guids is frankly irrelevant in today's world.
    • You absolutely must be careful about how new values are created. Truly random values do not index well. You are compelled to use a COMB guid or some variant that adds a sequential element to the guid.
    0 讨论(0)
  • 2020-12-24 03:08

    I wonder why there's no standard "miniGUID" type? It would seem that performing a decent hash on a GUID should yield a 64-bit number which would have a trivial probability of duplication in any universe which doesn't have a billion or more things in it. Since the universe in which most GUID/miniGUID identifiers are used will never grow beyond a million things, much less a billion, I would think a smaller 8-byte miniGuid would be very useful.

    That would not, of course, suggest that it should be used as a clustered index; that would greatly impede performance. Nonetheless, an 8-byte miniGUID would only waste a third the space of a full GUID (when compared to a 4-byte index).

    0 讨论(0)
  • 2020-12-24 03:12

    Jeff Atwood talks about this in great detail:
    http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html

    Guid Pros:
    Unique across every table, every database, every server
    Allows easy merging of records from different databases
    Allows easy distribution of databases across multiple servers
    You can generate IDs anywhere, instead of having to roundtrip to the database
    Most replication scenarios require GUID columns anyway

    Guid Cons:
    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
    Cumbersome to debug (where userid='{BAE7DF4-DDF-3RG-5TY3E3RF456AS10}')
    The generated GUIDs should be partially sequential for best performance (eg, newsequentialid() on SQL 2005) and to enable use of clustered indexes

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