Search code examples
sql-serveruniqueidentifiersql-server-performance

Using GUIDs in Primary Keys / Clusted Indexes


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.

Assuming that the table has a fairly low amount of inserts per day (5000 +/- rows / day), what kind of performace issues could we run into? How will page splits affect our seek performance? How often should I reindex (or should I defrag)? What should I set the fill factors to (100, 90, 80, ect)?

What if I were inserting 1,000,000 rows per day?

I apologize beforhand for all of the questions, but i'm looking to get some backup for not using GUIDs as our default for PKs. I am however completely open to having my mind changed by the overwehlming knowledge from the StackOverflow user base.


Solution

  • 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.