Simon's SQL

SQL,DBA,tuning,Trouble Shooting,Performance

uniqueidentifier : GUID in SQL

Posted by Simon Cho on 01/02/2013

1. newsequentialid()

http://msdn.microsoft.com/en-us/library/ms189786%28v=sql.100%29.aspx

This is one of the function to generate sequence GUID to reduce fragmentation.

But, it’s not grantee uniqueness.
It’s wrapped by windows function UuidCreateSequential, http://msdn.microsoft.com/en-us/library/aa379322%28VS.85%29.aspx.

Creates a GUID that is greater than any GUID previously generated by this function on a specified computer since Windows was started.
After restarting Windows, the GUID can start again from a lower range, but is still globally unique.
When a GUID column is used as a row identifier, using NEWSEQUENTIALID can be faster than using the NEWID function.
This is because the NEWID function causes random activity and uses fewer cached data pages. Using NEWSEQUENTIALID also helps to completely fill the data and index pages.

2. uniqueidentifier sorting.

http://sqlblog.com/blogs/alberto_ferrari/archive/2007/08/31/how-are-guids-sorted-by-sql-server.aspx

3. uniqueidentifier converting

SELECT CONVERT(uniqueidentifier, 0x000102030405060708090A0B0C0D0E0F)
UNION ALL
SELECT CONVERT(uniqueidentifier, 0x00000000000000000123456789ABCDEF)
UNION ALL
SELECT CONVERT(uniqueidentifier, 0x000000000123456789ABCDEF00000000)
UNION ALL
SELECT CONVERT(UNIQUEIDENTIFIER, '00000000-0123-4567-89AB-CDEF00000000')

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s