比較 GUID 及 uniqueidentifier 值 (ADO.NET)
SQL Server 中的全域唯一識別碼 (GUID) 資料型別由 uniqueidentifier 資料型別所表示,其儲存 16 個位元組的二進位值。 GUID 是二進位數字,主要當成識別項使用,在許多電腦位於許多站台上的網路中,該識別項必須是唯一的。 可藉由呼叫 Transact-SQL NEWID 函式的方法來產生 GUID,並保證此 GUID 是全球唯一的。 如需詳細資訊,請參閱《SQL Server 線上叢書》的<使用 uniqueidentifier 資料>(英文)。
使用 SqlGuid 值
GUID 值又長又難以理解,對使用者而言不具任何意義。 如果將隨機產生的 GUID 用於索引鍵值,且插入大量資料列,則會使隨機 I/O 進入索引,進而對效能產生負面影響。 而且相較之下,GUID 比其他資料型別還要大。 因此,一般建議僅在其他資料型別都不適用的極少案例中,才使用 GUID。
比較 GUID 值
比較運算子可以與 uniqueidentifier 值搭配使用。 不過,比較這兩個值的位元模式並不會實作排序作業。 uniqueidentifier 值允許的作業僅有比較 (=、<>、<、>、<=、>=),以及檢查其是否為 NULL (IS NULL 及 IS NOT NULL)。 而不允許其他算術運算子。
Guid 及 SqlGuid 都具有可用來比較不同 GUID 值的 CompareTo 方法。 但是,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