2 out of 3 rated this helpful - Rate this topic

NEWSEQUENTIALID() (Transact-SQL)

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.

Important noteImportant

If privacy is a concern, do not use this function. It is possible to guess the value of the next generated GUID and, therefore, access data associated with that GUID.

NEWSEQUENTIALID is a wrapper over the Windows UuidCreateSequential function.

Topic link iconTransact-SQL Syntax Conventions


NEWSEQUENTIALID ( )

uniqueidentifier

NEWSEQUENTIALID() can only be used with DEFAULT constraints on table columns of type uniqueidentifier. For example:

CREATE TABLE myTable (ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID()) 

When NEWSEQUENTIALID() is used in DEFAULT expressions, it cannot be combined with other scalar operators. For example, you cannot execute the following:

CREATE TABLE myTable (ColumnA uniqueidentifier DEFAULT dbo.myfunction(NEWSEQUENTIALID())) 

In the previous example, myfunction() is a scalar user-defined scalar function that accepts and returns a uniqueidentifier value.

NEWSEQUENTIALID() cannot be referenced in queries.

You can use NEWSEQUENTIALID() to generate GUIDs to reduce page contention at the leaf level of indexes.

Each GUID generated by using NEWSEQUENTIALID() is unique on that computer. GUIDs generated by using NEWSEQUENTIALID() are unique across multiple computers only if the source computer has a network card. For more information about GUIDs, see Using uniqueidentifier Data.

Updated content

Added a description about the effect that restarting Windows can have on GUIDs.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Not sure to understand why

NEWSEQUENTIALID is a wrapper over the Windows UuidCreateSequential function.
So I perform a loop creating 10 000 000 guids using UuidCreateSequential. I perform another loop using UuidCreate.
Performance are nearly the same.

Creating 100 000 000 guids take 20 seconds using UuidCreate and 12 seconds using UuidCreateSequential.
So, I agree : UuidCreateSequential is faster. But the difference is not so important.

I think the big deal is managing correctly the clustered index created by default by SQL Server on primary keys.
If you define your primary key on a uniqueidentifier field : You have to remove the clustered index or better : to set it on the field that are part of the "logical/functional" primary key.

Uniqueidentifier field data it not meaningful : the only goal is to ensure values are unique
Creating a clustered index on uniqueidentifier is simply a nonsense.

Not always sequential (or not always larger than the previous value)
Restarting the OS can setup a situation where a new GUID value generated by NEWSEQUENTIALID() is less than the previous maximum value it generated. See https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=475131 for an example.

So just be aware - it is not necessarily sequential.