我有以下情况:我正在创建一个可在本地运行的数据库,但可以迁移到具有相同模式并且已经有一些数据的在线数据库。出于这个原因,我打算使用GUID作为其表的主键,因为它可以避免更改主键值(并将其级联到引用表),如果我使用任何标识,则会使迁移更容易专栏(我是对的?)。该数据库基于SQL Server 2012构建。
尽管如此,我希望有一个聚簇索引来加速对数据库表的查询,当我阅读许多文章和Stack Overflow答案时,我确信在GUID主键上有一个聚簇索引不是一个好主意。所以,问题是:在这个数据库表上有一个聚簇索引有什么好的解决方案吗?我愿意删除GUID并添加其他数据类型。有一秒我想到了使用HiLo方法,但我认为它会使迁移变得更难,这是一个选项,但我需要一个非常好的理由来选择它(如果没有好的方法来使用GUID,那就是名字广告PK并加快查询速度。)
到现在为止,我想到了这些解决方案:
我认为这就是我能提出的所有内容,我真的倾向于选择COMB方法,并考虑一些实验来验证它是否以及为什么更好。但在这种情况下是否有更好的解决方案?
答案 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
*/
}
}