Unique Identifiers - GUIDs
Presentation by Aaron Buma
Development DBA
Session Overview
• Overview
• Generation
• Comparison and Non-Comparison Operations
• Advantages and Disadvantages
GUID Overview
• GUID – Global Unique Identifier
• Unique – 2^128 possible values
• A unique 16-byte number
• Can be auto-generated on a column
– IDENTITY and TIMESTAMP (2008-)/ROWVERSION(2008+)
GUID Generation
• On the fly:
• From a String (too long of string will be trimmed):
• Auto-Generate:
GUID Generation
• NEWID:
– A random 16 byte identifier
• NEWSEQUENTIALID:
– Can only be a DEFAULT when record is created
Comparison (and non) Operations
Can Use Can’t Use
=, !=, <> +, -, *, /
<, <=, >, >=
LIKE
BETWEEN
IS NULL and IS NOT NULL
GUIDs and ORDER BY
• When using “ORDER BY” on a GUID column, the data
is sorted on the last section of the GUID
DEMOS
Advantages of GUIDs
• Identifying data from multiple sources
– An advantage over IDENTITY
• Can be generated offline
Disadvantages of GUIDs
• Data Size: 4x larger than INT
– Slower and Larger Indexes
– Page Splits on Indexes (fragmentation)
• Slowest JOIN performance
• Slowest INSERT/UPDATE performance
• Slowest SORT performance
Disadvantages of GUIDs
• Adding/Removing GUIDs will cause fragmentation on
indexes if GUIDs are used for:
– Primary Keys
• GUIDs have least performance on JOINS, so not only is
JOINING on a GUID slow, but joining to a fragmented
index is even slower
– Clustering
Advantage vs Disadvantages
<
Review
• Overview
• Generation
• Comparison and Non-Comparison Operations
• Advantages and Disadvantages
Presentation Recording, Slides and Scripts
– Available at:
http://www.aaronbuma.com/2015/10/guids/

SQL Server GUIDS (Globally Unique Identifiers)

  • 1.
    Unique Identifiers -GUIDs Presentation by Aaron Buma Development DBA
  • 2.
    Session Overview • Overview •Generation • Comparison and Non-Comparison Operations • Advantages and Disadvantages
  • 3.
    GUID Overview • GUID– Global Unique Identifier • Unique – 2^128 possible values • A unique 16-byte number • Can be auto-generated on a column – IDENTITY and TIMESTAMP (2008-)/ROWVERSION(2008+)
  • 4.
    GUID Generation • Onthe fly: • From a String (too long of string will be trimmed): • Auto-Generate:
  • 5.
    GUID Generation • NEWID: –A random 16 byte identifier • NEWSEQUENTIALID: – Can only be a DEFAULT when record is created
  • 6.
    Comparison (and non)Operations Can Use Can’t Use =, !=, <> +, -, *, / <, <=, >, >= LIKE BETWEEN IS NULL and IS NOT NULL
  • 7.
    GUIDs and ORDERBY • When using “ORDER BY” on a GUID column, the data is sorted on the last section of the GUID
  • 8.
  • 9.
    Advantages of GUIDs •Identifying data from multiple sources – An advantage over IDENTITY • Can be generated offline
  • 10.
    Disadvantages of GUIDs •Data Size: 4x larger than INT – Slower and Larger Indexes – Page Splits on Indexes (fragmentation) • Slowest JOIN performance • Slowest INSERT/UPDATE performance • Slowest SORT performance
  • 11.
    Disadvantages of GUIDs •Adding/Removing GUIDs will cause fragmentation on indexes if GUIDs are used for: – Primary Keys • GUIDs have least performance on JOINS, so not only is JOINING on a GUID slow, but joining to a fragmented index is even slower – Clustering
  • 12.
  • 13.
    Review • Overview • Generation •Comparison and Non-Comparison Operations • Advantages and Disadvantages Presentation Recording, Slides and Scripts – Available at: http://www.aaronbuma.com/2015/10/guids/