比较 GUID 和 uniqueidentifier 值Comparing GUID and uniqueidentifier Values

SQL Server 中的全局唯一标识符 (GUID) 数据类型由 uniqueidentifier 数据类型表示,用于存储 16 字节的二进制值。The globally unique identifier (GUID) data type in SQL Server is represented by the uniqueidentifier data type, which stores a 16-byte binary value. GUID 是一个二进制数字,其主要用途是作为标识符,该标识符在拥有位于许多地点的许多计算机的网络中必须是唯一的。A GUID is a binary number, and its main use is as an identifier that must be unique in a network that has many computers at many sites. GUID 可以通过调用 Transact-SQL NEWID 函数生成,保证在全局是唯一的。GUIDs can be generated by calling the Transact-SQL NEWID function, and is guaranteed to be unique throughout the world. 有关详细信息,请参阅uniqueidentifier (transact-sql)For more information, see uniqueidentifier (Transact-SQL).

使用 SqlGuid 值Working with SqlGuid Values

因为 GUID 值很长并且不明确,所以对用户没有意义。Because GUIDs values are long and obscure, they are not meaningful for users. 如果键值使用随机生成的 GUID,并且您插入了许多行,将随机 I/O 编入索引可能会对性能造成负面影响。If randomly generated GUIDs are used for key values and you insert a lot of rows, you get random I/O into your indexes, which can negatively impact performance. 与其他数据类型相比,GUID 也相对较大。GUIDs are also relatively large when compared to other data types. 通常,我们建议只对没有其他适用的数据类型的范围非常窄的方案使用 GUID。In general we recommend using GUIDs only for very narrow scenarios for which no other data type is suitable.

比较 GUID 值Comparing GUID Values

uniqueidentifier 值可以使用比较运算符。Comparison operators can be used with uniqueidentifier values. 但是,没有通过比较两个值的位模式实现排序。However, ordering is not implemented by comparing the bit patterns of the two values. 允许对 uniqueidentifier 值进行的唯一操作是比较(=、< >、<、>、<=、> =)以及检查是否为 null (IS NULL 和 IS NOT NULL)。The only operations that are allowed against a uniqueidentifier value are comparisons (=, <>, <, >, <=, >=) and checking for NULL (IS NULL and IS NOT NULL). 不允许使用任何其他算术运算符。No other arithmetic operators are allowed.

GuidSqlGuid 都具有 CompareTo 方法,用于比较不同的 GUID 值。Both Guid and SqlGuid have a CompareTo method for comparing different GUID values. 但是,System.Guid.CompareToSqlTypes.SqlGuid.CompareTo 的实现方式有所不同。However, System.Guid.CompareTo and SqlTypes.SqlGuid.CompareTo are implemented differently. SqlGuid 使用 SQL Server 行为来实现 CompareTo,其中,值的最后 6 个字节是最高有效字节。SqlGuid implements CompareTo using SQL Server behavior, in the last six bytes of a value are most significant. Guid 计算全部 16 个字节。Guid evaluates all 16 bytes. 以下示例演示这种行为的差异。The following example demonstrates this behavioral difference. 代码的第一部分显示未排序的 Guid 值,代码的第二部分显示排序的 Guid 值。The first section of code displays unsorted Guid values, and the second section of code shows the sorted Guid values. 第三部分显示排序的 SqlGuid 值。The third section shows the sorted SqlGuid values. 输出显示在代码列表下。The output is displayed beneath the code listing.

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);
    }
}
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

此示例将生成下列结果。This example produces the following results.

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  

请参阅See also