比较 GUID 和 uniqueidentifier 值

SQL Server 中的全局唯一标识符 (GUID) 数据类型由 uniqueidentifier 数据类型表示,该数据类型存储 16 字节的二进制值。 GUID 是一个二进制数,它主要用作一个标识符,此标识符必须在一个在多个站点上具有多台计算机的网络中是惟一的。 可以通过调用 Transact-SQL NEWID 函数来生成 GUID,并保证其在全球是唯一的。 有关详细信息,请参阅 uniqueidentifier (Transact-SQL)

使用 SqlGuid 值

由于 GUID 值很长且不明确,因此它们对用户没有意义。 如果随机生成的 GUID 用于键值,并且插入大量的行,则会在索引中获得随机 I/O,这会对性能产生负面影响。 与其他数据类型相比,GUID 也相对较大。 通常,我们建议仅将 GUID 用于任何其他数据类型都不适合的极窄方案。

比较 GUID 值

比较运算符可与 uniqueidentifier 值一起使用。 不过,排序不是通过比较两个值的位模式来实现的。 唯一允许对 uniqueidentifier 值执行的操作是比较(=、<>、<、>、<=、>=)和 NULL 校验(IS NULL 和 IS NOT NULL)。 不允许使用任何其他算术运算符。

GuidSqlGuid 都具有用于比较不同 GUID 值的 CompareTo 方法。 但 System.Guid.CompareToSqlTypes.SqlGuid.CompareTo 的实现方式不同。 SqlGuid 使用 SQL Server 行为实现 CompareTo,一个值的最后 6 个字节是最重要的。 Guid 计算全部 16 个字节。 下面的示例对这种行为差异进行了演示。 代码的第一部分显示未排序的 Guid 值,第二部分显示已排序的 Guid 值。 第三部分显示已排序的 SqlGuid 值。 输出显示在代码清单下方。

static void WorkWithGuids()
{
    // Create an ArrayList and fill it with Guid values.
    ArrayList guidList = new()
    {
        new Guid("3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE"),
        new Guid("2AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE"),
        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()
    {
        new SqlGuid("3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE"),
        new SqlGuid("2AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE"),
        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

此示例生成以下结果。

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  

另请参阅