Is it a bad idea to use GUIDs as primary keys in MS SQL?

前端 未结 2 1606
时光说笑
时光说笑 2020-12-23 07:52

We have a system that uses UniqueIdentifier as the primary key of each of the tables. It has been brought to our attention that this is a bad idea. I have seen similar post

相关标签:
2条回答
  • 2020-12-23 08:30

    I wrote a post about this last week with some code to show you what happens: Some Simple Code To Show The Difference Between Newid And Newsequentialid

    Basically if you use newid() instead of Newsequentialid() you get horrible page splits if your PK is a clustered index (which it will be by default)

    0 讨论(0)
  • 2020-12-23 08:47

    There are pros and cons:

    This article covers everything.

    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)
提交回复
热议问题