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

后端 未结 15 2197
深忆病人
深忆病人 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:44

    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

    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

    for example

    457180FB-C2EA-48DF-8BEF-458573DA1C10    1
    9A70FF3C-B7DA-4593-93AE-4A8945943C8A    2
    

    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

提交回复
热议问题