比较 GUID 和 uniqueidentifier 值 (ADO.NET)
更新:November 2007
SQL Server 中的全局唯一标识符 (GUID) 数据类型由 uniqueidentifier 数据类型表示,用于存储 16 字节的二进制值。GUID 是一个二进制数字,其主要用途是作为标识符,该标识符在拥有位于许多地点的许多计算机的网络中必须是唯一的。 GUID 可以通过调用 Transact-SQL NEWID 函数生成,保证在全局是唯一的。 有关更多信息,请参见“SQL Server 联机图书”中的“使用 uniqueidentifier 数据”。
使用 SqlGuid 值
因为 GUID 值很长并且不明确,所以对用户没有意义。 如果键值使用随机生成的 GUID,并且您插入了许多行,将随机 I/O 编入索引可能会对性能造成负面影响。 与其他数据类型相比,GUID 也相对较大。 通常,我们建议只对没有其他适用的数据类型的范围非常窄的方案使用 GUID。
比较 GUID 值
uniqueidentifier 值可以使用比较运算符。但是,没有通过比较两个值的位模式实现排序。 允许对 uniqueidentifier 值进行的唯一操作是比较(=、<>、<、>、<=、>=)和 NULL 校验(IS NULL 和 IS NOT NULL)。不允许使用任何其他算术运算符。
Guid 和 SqlGuid 都具有 CompareTo 方法,用于比较不同的 GUID 值。 但是,System.Guid.CompareTo 和 SqlTypes.SqlGuid.CompareTo 的实现方式有所不同。SqlGuid 使用 SQL Server 行为实现 CompareTo,该行为只计算值的最后 6 个字节。Guid 计算全部 16 个字节。以下示例演示这种行为的差异。 代码的第一部分显示未排序的 Guid 值,代码的第二部分显示排序的 Guid 值。 第三部分显示排序的 SqlGuid 值。 输出显示在代码列表下。
Private Sub WorkWithGuids()
' Create an ArrayList and fill it with Guid values.
Dim guidList As New ArrayList()
guidList.Add(New Guid("3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE"))
guidList.Add(New Guid("2AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE"))
guidList.Add(New Guid("1AAAAAAA-BBBB-CCCC-DDDD-3EEEEEEEEEEE"))
' Display the unsorted Guid values.
Console.WriteLine("Unsorted Guids:")
For Each guidValue As Guid In guidList
Console.WriteLine("{0}", guidValue)
Next
Console.WriteLine()
' Sort the Guids.
guidList.Sort()
' Display the sorted Guid values.
Console.WriteLine("Sorted Guids:")
For Each guidSorted As Guid In guidList
Console.WriteLine("{0}", guidSorted)
Next
Console.WriteLine()
' Create an ArrayList of SqlGuids.
Dim sqlGuidList As New ArrayList()
sqlGuidList.Add(New SqlGuid("3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE"))
sqlGuidList.Add(New SqlGuid("2AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE"))
sqlGuidList.Add(New SqlGuid("1AAAAAAA-BBBB-CCCC-DDDD-3EEEEEEEEEEE"))
' Sort the SqlGuids. The unsorted SqlGuids are in the same order
' as the unsorted Guid values.
sqlGuidList.Sort()
' Display the sorted SqlGuids. The sorted SqlGuid values are
' ordered differently than the Guid values.
Console.WriteLine("Sorted SqlGuids:")
For Each sqlGuidValue As SqlGuid In sqlGuidList
Console.WriteLine("{0}", sqlGuidValue)
Next
End Sub
private static void WorkWithGuids()
{
// Create an ArrayList and fill it with Guid values.
ArrayList guidList = new ArrayList();
guidList.Add(new Guid("3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE"));
guidList.Add(new Guid("2AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE"));
guidList.Add(new Guid("1AAAAAAA-BBBB-CCCC-DDDD-3EEEEEEEEEEE"));
// Display the unsorted Guid values.
Console.WriteLine("Unsorted Guids:");
foreach (Guid guidValue in guidList)
{
Console.WriteLine(" {0}", guidValue);
}
Console.WriteLine("");
// Sort the Guids.
guidList.Sort();
// Display the sorted Guid values.
Console.WriteLine("Sorted Guids:");
foreach (Guid guidSorted in guidList)
{
Console.WriteLine(" {0}", guidSorted);
}
Console.WriteLine("");
// Create an ArrayList of SqlGuids.
ArrayList sqlGuidList = new ArrayList();
sqlGuidList.Add(new SqlGuid("3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE"));
sqlGuidList.Add(new SqlGuid("2AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE"));
sqlGuidList.Add(new SqlGuid("1AAAAAAA-BBBB-CCCC-DDDD-3EEEEEEEEEEE"));
// Sort the SqlGuids. The unsorted SqlGuids are in the same order
// as the unsorted Guid values.
sqlGuidList.Sort();
// Display the sorted SqlGuids. The sorted SqlGuid values are ordered
// differently than the Guid values.
Console.WriteLine("Sorted SqlGuids:");
foreach (SqlGuid sqlGuidValue in sqlGuidList)
{
Console.WriteLine(" {0}", sqlGuidValue);
}
}
此示例将生成下列结果。
Unsorted Guids:
3aaaaaaa-bbbb-cccc-dddd-2eeeeeeeeeee
2aaaaaaa-bbbb-cccc-dddd-1eeeeeeeeeee
1aaaaaaa-bbbb-cccc-dddd-3eeeeeeeeeee
Sorted Guids:
1aaaaaaa-bbbb-cccc-dddd-3eeeeeeeeeee
2aaaaaaa-bbbb-cccc-dddd-1eeeeeeeeeee
3aaaaaaa-bbbb-cccc-dddd-2eeeeeeeeeee
Sorted SqlGuids:
2aaaaaaa-bbbb-cccc-dddd-1eeeeeeeeeee
3aaaaaaa-bbbb-cccc-dddd-2eeeeeeeeeee
1aaaaaaa-bbbb-cccc-dddd-3eeeeeeeeeee