聚簇索引的GUID的替代方法

时间:2013-08-23 13:05:45

标签: sql sql-server indexing guid

我有以下情况:我正在创建一个可在本地运行的数据库,但可以迁移到具有相同模式并且已经有一些数据的在线数据库。出于这个原因,我打算使用GUID作为其表的主键,因为它可以避免更改主键值(并将其级联到引用表),如果我使用任何标识,则会使迁移更容易专栏(我是对的?)。该数据库基于SQL Server 2012构建。

尽管如此,我希望有一个聚簇索引来加速对数据库表的查询,当我阅读许多文章和Stack Overflow答案时,我确信在GUID主键上有一个聚簇索引不是一个好主意。所以,问题是:在这个数据库表上有一个聚簇索引有什么好的解决方案吗?我愿意删除GUID并添加其他数据类型。有一秒我想到了使用HiLo方法,但我认为它会使迁移变得更难,这是一个选项,但我需要一个非常好的理由来选择它(如果没有好的方法来使用GUID,那就是名字广告PK并加快查询速度。)

到现在为止,我想到了这些解决方案:

  • 使用newsequentialid()生成新的GUID,使它们成为顺序,更好地作为聚簇索引。这里的问题是:这真的比使用int主键更好吗?这里使用GUID的主要原因是为了帮助最终会发生的任何迁移,我认为将它们设置为有序可能无济于事。另外,我必须检索数据库生成的GUID,而不是在客户端创建它。
  • 使用COMB GUID并使其成为聚集索引。它似乎解决了前一种方法中存在的一些问题,同时保持了较高的“随机因素”,但我不知道是否有任何结果。有吗?
  • 添加identity int列并将其用作聚簇索引。我的问题是它是否对任何事情都有帮助,因为无论如何都会使用PK值进行查询,这是一个GUID。
  • 稍微有可能使用上一个方法,将identity int转换为主键值和聚簇索引,并添加一个GUID collumn,我将使用它进行查询。我在Adventure Works 2012上看到这是选择,但我真的不明白它有什么帮助...如果我使用GUID查询值,聚合索引会有帮助吗,这不是PK也不是群集?

我认为这就是我能提出的所有内容,我真的倾向于选择COMB方法,并考虑一些实验来验证它是否以及为什么更好。但在这种情况下是否有更好的解决方案?

1 个答案:

答案 0 :(得分:1)

//另外,我必须检索数据库生成的GUID,而不是在客户端创建它.//

<(>((注意,我想把它作为评论,但“代码”对于简单的评论来说太大了)。 但这〜可能是一个答案。 )))

你可以创建一个客户端(主要是)顺序guid ..... 作为一名C#开发人员而不是DBA,我喜欢在数据库之外创建我的所有关系,然后将所有内容作为Xml发送,让TSQL将其粉碎,然后在一次数据库命中中突出我需要的所有内容(偶尔也会删除)我猜)。 但这种方法存在缺陷。我认为int和bigint通常会“赢得”性能竞赛......但是........大部分时间.........使用guid与客户端“顺序”足以满足我的需求。特别是,正如您所建议的那样......您必须在某个时刻移动该数据。使用IDENTITY()将PK / FK改为“阵容”是可行的,但使用GUID(恕我直言)更容易。现在,如果我正在写T~ick3t M @ ster或Am @ zing在线零售网站,我不会选择GUID。

祝你好运。

using System;
using System.Runtime.InteropServices;


namespace MyCompany.MyTechnology.Framework.CrossDomain.GuidExtend
{
    public static class Guid
    {

        /*

        Original Reference for Code:
        http://www.pinvoke.net/default.aspx/rpcrt4/UuidCreateSequential.html

        */


        [DllImport("rpcrt4.dll", SetLastError = true)]
        static extern int UuidCreateSequential(out System.Guid guid);

        public static System.Guid NewGuid()
        {
            return CreateSequentialUuid();
        }


        public static System.Guid CreateSequentialUuid()
        {
            const int RPC_S_OK = 0;
            System.Guid g;
            int hr = UuidCreateSequential(out g);
            if (hr != RPC_S_OK)
                throw new ApplicationException("UuidCreateSequential failed: " + hr);
            return g;
        }


        /*

        Text From URL above:

        UuidCreateSequential (rpcrt4)

        Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
        To create a page in a module other than rpcrt4, prefix the name with the module name and a period.
        . Summary
        Creates a new UUID 
        C# Signature:
        [DllImport("rpcrt4.dll", SetLastError=true)]
        static extern int UuidCreateSequential(out Guid guid);


        VB Signature:
        Declare Function UuidCreateSequential Lib "rpcrt4.dll" (ByRef id As Guid) As Integer


        User-Defined Types:
        None.

        Notes:
        Microsoft changed the UuidCreate function so it no longer uses the machine's MAC address as part of the UUID. Since CoCreateGuid calls UuidCreate to get its GUID, its output also changed. If you still like the GUIDs to be generated in sequential order (helpful for keeping a related group of GUIDs together in the system registry), you can use the UuidCreateSequential function.

        CoCreateGuid generates random-looking GUIDs like these:

        92E60A8A-2A99-4F53-9A71-AC69BD7E4D75
        BB88FD63-DAC2-4B15-8ADF-1D502E64B92F
        28F8800C-C804-4F0F-B6F1-24BFC4D4EE80
        EBD133A6-6CF3-4ADA-B723-A8177B70D268
        B10A35C0-F012-4EC1-9D24-3CC91D2B7122



        UuidCreateSequential generates sequential GUIDs like these:

        19F287B4-8830-11D9-8BFC-000CF1ADC5B7
        19F287B5-8830-11D9-8BFC-000CF1ADC5B7
        19F287B6-8830-11D9-8BFC-000CF1ADC5B7
        19F287B7-8830-11D9-8BFC-000CF1ADC5B7
        19F287B8-8830-11D9-8BFC-000CF1ADC5B7



        Here is a summary of the differences in the output of UuidCreateSequential:

        The last six bytes reveal your MAC address 
        Several GUIDs generated in a row are sequential 
        Tips & Tricks:
        Please add some!

        Sample Code in C#:
        static Guid UuidCreateSequential()
        {
           const int RPC_S_OK = 0;
           Guid g;
           int hr = UuidCreateSequential(out g);
           if (hr != RPC_S_OK)
             throw new ApplicationException
               ("UuidCreateSequential failed: " + hr);
           return g;
        }



        Sample Code in VB:
        Sub Main()
           Dim myId As Guid
           Dim code As Integer
           code = UuidCreateSequential(myId)
           If code <> 0 Then
             Console.WriteLine("UuidCreateSequential failed: {0}", code)
           Else
             Console.WriteLine(myId)
           End If
        End Sub




        */








    }
}