SQL Server - Guid VS. Long

后端 未结 7 1843
野的像风
野的像风 2020-12-29 00:06

Up until now i\'ve been using the C# \"Guid = Guid.NewGuid();\" method to generate a unique ID that can be stored as the ID field in some of my SQL Server database tables us

7条回答
  •  盖世英雄少女心
    2020-12-29 00:50

    The "Queen of Indexing" - Kim Tripp - basically says it all in her indexing blog posts:

    • GUIDs as PRIMARY KEYs and/or the clustering key
    • The clustered index debate continues...
    • Ever increasing clustering key - the Clustered Index Debate......again!

    Basically, her best practices are: an optimal clustering key should be:

    • unique
    • small
    • stable (never changing)
    • ever-increasing

    GUID's violate the "small" and "ever-increasing" and are thus not optimal.

    PLUS: all your clustering keys will be added to each and every single entry in each and every single non-clustered index (as the lookup to actually find the record in the database), thus you want to make them as small as possible (INT = 4 byte vs. GUID = 16 byte). If you have hundreds of millions of rows and several non-clustered indices, choosing an INT or BIGINT over a GUID can make a major difference - even just space-wise.

    Marc

提交回复
热议问题