Database Design Primay Key, ID vs String

前端 未结 5 1664
执笔经年
执笔经年 2021-02-01 16:11

I am currently planning to develop a music streaming application. And i am wondering what would be better as a primary key in my tables on the server. An ID int or a Unique Stri

5条回答
  •  天命终不由人
    2021-02-01 16:45

    From the software perspective the GUID is better as its unique globally.

    Quotes from: Primary Keys: IDs versus GUIDs

    Using a GUID as a row identity value feels more natural-- and certainly more truly unique-- than a 32-bit integer. Database guru Joe Celko seems to agree. GUID primary keys are a natural fit for many development scenarios, such as replication, or when you need to generate primary keys outside the database. But it's still a question of balancing the tradeoffs between traditional 4-byte integer IDs and 16-byte GUIDs:

    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

提交回复
热议问题