Confronto tra GUID e valori uniqueidentifierComparing GUID and uniqueidentifier Values

Il tipo di dati identificatore univoco globale (GUID, Globally Unique IDentifier) in SQL Server è rappresentato dal tipo di dati uniqueidentifier, il quale archivia un valore binario di 16 byte.The globally unique identifier (GUID) data type in SQL Server is represented by the uniqueidentifier data type, which stores a 16-byte binary value. Un GUID è un numero binario usato principalmente come identificatore univoco in una rete di più computer su più siti.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. I GUID possono essere generati chiamando la funzione NEWID Transact-SQL e sono assolutamente univoci.GUIDs can be generated by calling the Transact-SQL NEWID function, and is guaranteed to be unique throughout the world. Per ulteriori informazioni, vedere uniqueidentifier (Transact-SQL).For more information, see uniqueidentifier (Transact-SQL).

Uso di valori SqlGuidWorking with SqlGuid Values

I valori GUID sono lunghi e poco chiari, di conseguenza il significato per l'utente resta vago.Because GUIDs values are long and obscure, they are not meaningful for users. Se si usano GUID generati in modo casuale per valori chiave e se si inserisce un numero elevato di righe, si otterranno I/O casuali negli indici che potrebbero compromettere le prestazioni.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. I GUID, inoltre, sono di dimensioni relativamente grandi rispetto ad altri tipi di dati.GUIDs are also relatively large when compared to other data types. In generale, si consiglia di usare i GUID solo per scenari molto ristretti per i quali non è adatto alcun altro tipo di dati.In general we recommend using GUIDs only for very narrow scenarios for which no other data type is suitable.

Confronto di valori GUIDComparing GUID Values

Con i valori uniqueidentifier è possibile usare operatori di confronto.Comparison operators can be used with uniqueidentifier values. Tuttavia, l'ordinamento non viene implementato confrontando gli schemi di bit dei due valori.However, ordering is not implemented by comparing the bit patterns of the two values. Le uniche operazioni consentite rispetto a un valore uniqueidentifier sono i confronti (=, < >, <, >, <=, > =) e la verifica della presenza di valori NULL (IS NULL e 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). Non è consentito alcun altro operatore aritmetico.No other arithmetic operators are allowed.

Sia il tipo Guid che il tipo SqlGuid dispongono di un metodo CompareTo per confrontare valori GUID diversi.Both Guid and SqlGuid have a CompareTo method for comparing different GUID values. Tuttavia, System.Guid.CompareTo e SqlTypes.SqlGuid.CompareTo sono implementati in modo diverso.However, System.Guid.CompareTo and SqlTypes.SqlGuid.CompareTo are implemented differently. SqlGuid implementa CompareTo usando il comportamento di SQL Server in cui gli ultimi sei byte di un valore sono i più importanti.SqlGuid implements CompareTo using SQL Server behavior, in the last six bytes of a value are most significant. Guid valuta tutti i 16 byte.Guid evaluates all 16 bytes. Nell'esempio seguente è illustrata questa differenza di comportamento.The following example demonstrates this behavioral difference. La prima sezione del codice visualizza valori Guid non ordinati, mentre la seconda sezione mostra i valori Guid ordinati.The first section of code displays unsorted Guid values, and the second section of code shows the sorted Guid values. La terza sezione visualizza i valori SqlGuid ordinati.The third section shows the sorted SqlGuid values. L'output viene visualizzato sotto il listato di codice.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

I risultati ottenuti dall'esempio sono seguenti.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  

Vedere ancheSee also