<tfoot id='tCMig'></tfoot>

        <bdo id='tCMig'></bdo><ul id='tCMig'></ul>
    1. <i id='tCMig'><tr id='tCMig'><dt id='tCMig'><q id='tCMig'><span id='tCMig'><b id='tCMig'><form id='tCMig'><ins id='tCMig'></ins><ul id='tCMig'></ul><sub id='tCMig'></sub></form><legend id='tCMig'></legend><bdo id='tCMig'><pre id='tCMig'><center id='tCMig'></center></pre></bdo></b><th id='tCMig'></th></span></q></dt></tr></i><div id='tCMig'><tfoot id='tCMig'></tfoot><dl id='tCMig'><fieldset id='tCMig'></fieldset></dl></div>
      <legend id='tCMig'><style id='tCMig'><dir id='tCMig'><q id='tCMig'></q></dir></style></legend>

      <small id='tCMig'></small><noframes id='tCMig'>

        唯一标识符(guid)作为数据库设计中的主键

        时间:2023-11-28
        <tfoot id='ez1Ym'></tfoot>
        • <bdo id='ez1Ym'></bdo><ul id='ez1Ym'></ul>
            <tbody id='ez1Ym'></tbody>

                <i id='ez1Ym'><tr id='ez1Ym'><dt id='ez1Ym'><q id='ez1Ym'><span id='ez1Ym'><b id='ez1Ym'><form id='ez1Ym'><ins id='ez1Ym'></ins><ul id='ez1Ym'></ul><sub id='ez1Ym'></sub></form><legend id='ez1Ym'></legend><bdo id='ez1Ym'><pre id='ez1Ym'><center id='ez1Ym'></center></pre></bdo></b><th id='ez1Ym'></th></span></q></dt></tr></i><div id='ez1Ym'><tfoot id='ez1Ym'></tfoot><dl id='ez1Ym'><fieldset id='ez1Ym'></fieldset></dl></div>

                <small id='ez1Ym'></small><noframes id='ez1Ym'>

                • <legend id='ez1Ym'><style id='ez1Ym'><dir id='ez1Ym'><q id='ez1Ym'></q></dir></style></legend>
                • 本文介绍了唯一标识符(guid)作为数据库设计中的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们的数据位于 SQL Server 2008 数据库中,表之间会有很多查询和连接.我们团队内部有这个论点,有些人认为使用整数标识对性能更好,有些人争论使用 guid(唯一标识符).

                  Our data resides in a SQL Server 2008 database, there will be a lot queries and joinings between tables. We have this argument inside the team, some are arguing use of integer identity is better for performance, some are arguing use of guid (unique identifier).

                  使用 GUID 作为主键的性能真的会受到如此严重的影响吗?

                  Does the performance really suffer that badly using a GUID as a primary key?

                  推荐答案

                  128 位 GUID (uniqueidentifier) 键当然比 32 位 int 键.但是,有一些关键优势:

                  A 128-bit GUID (uniqueidentifier) key is of course 4x larger than a 32-bit int key. However, there are a few key advantages:

                  • 合并内容时没有IDENTITY INSERT"问题
                  • 如果您使用 COMB 值而不是 NEWSEQUENTIALID(),您将获得一个免费"的 INSERT 时间戳.如果您需要一些花哨的 CAST() 调用,您甚至可以根据日期/时间范围从主键中 SELECT.
                  • 它们在全球范围内都是独一无二的,结果证明它时不时地非常方便.
                  • 由于不需要跟踪高水位线,您的 BL 层可以分配值而不是 SQL Server,从而省去了 SELECT scope_identity() 在插入后获取主键的步骤.
                  • 如果您可能有超过 20 亿条记录,那么您将需要使用 bigint(64 位)而不是 int.一旦你这样做了,uniqueidentifier 只是 bigint 的两倍.
                  • 使用 GUID 可以更安全地公开 URL 等中的密钥,而不会让您遭受猜测 ID"攻击.
                  • 在 SQL Server 如何从磁盘加载页面和处理器现在大多是 64 位之间,仅仅因为数字是 128 位而不是 32 并不意味着比较需要 4 倍的时间.我看到的最后一个测试表明 GUID 几乎一样快.
                  • 索引大小取决于包含的多少列.尽管 GUID 本身更大,但与索引中的其他列相比,额外的 8 或 12 个字节可能微不足道.
                  • No "IDENTITY INSERT" issue when merging content
                  • If you use a COMB value instead of NEWSEQUENTIALID(), you get a "free" INSERT timestamp. You can even SELECT from the primary key based on a date/time range if you want with a few fancy CAST() calls.
                  • They are globally unique, which turns out to be pretty handy now and then.
                  • Since there's no need to track high-water marks, your BL layer can assign the value rather than SQL Server, thus eliminating the step of SELECT scope_identity() to get the primary key after an insert.
                  • If it's even remotely possible that you could have more than 2 billion records, you'll need to use bigint (64 bits) instead of int. Once you do that, uniqueidentifier is only twice as big as a bigint.
                  • Using GUIDs makes it safer to expose keys in URLs, etc. without exposing yourself to "guess-the-ID" attacks.
                  • Between how SQL Server loads pages from disk and how processors are now mostly 64-bit, just because a number is 128 bits instead of 32 doesn't mean it takes 4x longer to compare. The last test I saw showed that GUIDs are nearly as fast.
                  • Index size depends on how many columns are included. Even though the GUIDs themselves are larger, the extra 8 or 12 bytes may be insignificant compared to the other columns in the index.

                  最后,通过使用整数来挤出一些小的性能优势可能不值得失去 GUID 的优势.凭经验对其进行测试并自行决定.

                  In the end, squeezing out some small performance advantage by using integers may not be worth losing the advantages of a GUID. Test it empirically and decide for yourself.

                  就我个人而言,我仍然根据情况使用两者,但在我的情况下,决定性因素从未真正归结为性能.

                  Personally, I still use both, depending on the situation, but the deciding factor has never really come down to performance in my case.

                  这篇关于唯一标识符(guid)作为数据库设计中的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ON [PRIMARY] 是什么意思? 下一篇:我们可以有一个不是任何其他表中的主键的外键吗?

                  相关文章

                • <legend id='YStiu'><style id='YStiu'><dir id='YStiu'><q id='YStiu'></q></dir></style></legend>

                    <bdo id='YStiu'></bdo><ul id='YStiu'></ul>
                  <tfoot id='YStiu'></tfoot>

                  <small id='YStiu'></small><noframes id='YStiu'>

                  1. <i id='YStiu'><tr id='YStiu'><dt id='YStiu'><q id='YStiu'><span id='YStiu'><b id='YStiu'><form id='YStiu'><ins id='YStiu'></ins><ul id='YStiu'></ul><sub id='YStiu'></sub></form><legend id='YStiu'></legend><bdo id='YStiu'><pre id='YStiu'><center id='YStiu'></center></pre></bdo></b><th id='YStiu'></th></span></q></dt></tr></i><div id='YStiu'><tfoot id='YStiu'></tfoot><dl id='YStiu'><fieldset id='YStiu'></fieldset></dl></div>