Using GUIDs in Primary Keys / Clusted Indexes

后端 未结 4 1276
孤独总比滥情好
孤独总比滥情好 2021-01-05 02:57

I\'m fairly well versed in SQL server performace but I constanly have to argue down the idea that GUIDs should be used as the default type for Clusterd Primary Keys.

<
4条回答
  •  粉色の甜心
    2021-01-05 03:14

    If you are doing any kind of volume, GUIDs are extremely bad as a PK bad unless you use sequential GUIDs, for the exact reasons you describe. Page fragmentation is severe:

                     Average                    Average
                     Fragmentation  Fragment    Fragment   Page     Average 
    Type             in Percent     Count       Size       Count    Space Used
    
    id               4.35           7           16.43      115      99.89
    newidguid        98.77          162         1          162      70.90 
    newsequentualid  4.35           7           16.43      115      99.89
    

    And as this comparison between GUIDs and integers shows:

    Test1 caused a tremendous amount of page splits, and had a scan density around 12% when I ran a DBCC SHOWCONTIG after the inserts had completed. The Test2 table had a scan density around 98%

    If your volume is very low, however, it just doesn't matter that much.

    If you do really need a globally unique ID but have high volume (and can't use sequential IDs), just put the GUIDs in an indexed column.

提交回复
热议问题