Using GUIDs in Primary Keys / Clusted Indexes

不想你离开。 提交于 2019-11-30 20:04:02

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.

Drawbacks of using GUID as primary key:

  • No meaningful ordering, means indexing doesn't give performance boost as it does with an integer.
  • Size of a GUID 16 bytes, versus 2, 4 or 8 bytes for an integer.
  • Very difficult for humans to remember, so no good as a reference id.

Advantages:

  • Allow non-guessable primary keys that can therefore be less dangerous when displayed in a web page query string or in the application.
  • Useful in Databases that don't provide an auto increment or identity data type.
  • Useful when you need to join data between two disparate data sources across platforms or environments.

I thought the decision as to whether to use GUIDs was pretty simple, but maybe I'm unaware of other issues.

With such a low inserts per day, I doubt that page splitting should be a significant factor. The real question is how does 5,000 compares with the existing row count, as this would be the main information needed to decide on an appropriate initial fill factor to deffer splits.

This said, I'm personally not a big fan of GUIDs. I understand that they can serve well in some contexts but in many cases they are just "in the way" [of efficiency, of ease of use, of ...]

I find the following questions useful to narrow down on deciding whether GUID should be used or not.

  • Will the PK be shared/published ? (i.e. will it be used beyond its internal use within SQL, will applications need these keys in a somewhat persistent fashion? Will users somehow see these keys?
  • Could the PK be used to help merge disparate data sources ?
  • Does the table have a primary -possibly composite- made from column(s) in the data ? What is the size of this possible this key
  • How do the primary keys sort? If composite, are the first few columns selective ?

Using a guid (unless it is a sequential GUID) as a clustered index is going to kill insert performance. Since the physical table layout is aligned according to the clustered index, using a guid which has a random sequencing order will cause serious table fragmentation. If you want to use a guid as a PK/Clustered index it must be a sequential guid using the newsequentialid() function in sql server. This will guarantee that the generated guids are ordered sequentially and prevent fragmentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!