13
Unique Identifiers - GUIDs Presentation by Aaron Buma Development DBA

SQL Server GUIDS (Globally Unique Identifiers)

Embed Size (px)

Citation preview

Page 1: SQL Server GUIDS (Globally Unique Identifiers)

Unique Identifiers - GUIDs

Presentation by Aaron BumaDevelopment DBA

Page 2: SQL Server GUIDS (Globally Unique Identifiers)

Session Overview

• Overview• Generation• Comparison and Non-Comparison Operations• Advantages and Disadvantages

Page 3: SQL Server GUIDS (Globally Unique Identifiers)

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+)

Page 4: SQL Server GUIDS (Globally Unique Identifiers)

GUID Generation

• On the fly:

• From a String (too long of string will be trimmed):

• Auto-Generate:

GUIDS

Page 5: SQL Server GUIDS (Globally Unique Identifiers)

GUID Generation

• NEWID:– A random 16 byte identifier

• NEWSEQUENTIALID:– Can only be a DEFAULT when record is created

GUIDS

Page 6: SQL Server GUIDS (Globally Unique Identifiers)

Comparison (and non) OperationsCan Use Can’t Use

=, !=, <> +, -, *, /

<, <=, >, >=

LIKE

BETWEEN

IS NULL and IS NOT NULL

Page 7: SQL Server GUIDS (Globally Unique Identifiers)

GUIDs and ORDER BY• When using “ORDER BY” on a GUID column, the data

is sorted on the last section of the GUID

Page 8: SQL Server GUIDS (Globally Unique Identifiers)

DEMOS

Page 9: SQL Server GUIDS (Globally Unique Identifiers)

Advantages of GUIDs

• Identifying data from multiple sources– An advantage over IDENTITY

• Can be generated offline

Page 10: SQL Server GUIDS (Globally Unique Identifiers)

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

Page 11: SQL Server GUIDS (Globally Unique Identifiers)

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

Page 12: SQL Server GUIDS (Globally Unique Identifiers)

Advantage vs Disadvantages

<

Page 13: SQL Server GUIDS (Globally Unique Identifiers)

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/