We use NEWSEQUENTIALID
to generate uniqueidentifier
as table ID.
Issue is raised after server reset as the seed of it is modified and sequentiality breaks.
Is there another suggested solution to achieve large identifier uniqueness for a table?
Thank you
Best Answer
Identity values, sequence values, and sequential uniqueidentifier values are never guaranteed to be generated without gaps.
Having said that, if you're using the
NEWSEQUENTIALID()
(orIDENTITY
, or even aSEQUENCE
) for a primary key column, why do you care if the values generated are sequential. As long as they are unique, and they are guaranteed to be, what's the problem with gaps between those values?If you're using that value in some way to confirm correct process, like an invoice number, where you must guarantee each number is sequential, then you'd probably want to use a
SEQUENCE
that uses no caching.The
NO CACHE
directive instructs SQL Server to generate numbers from the sequence one-at-a-time, which results in lower performance, but prevents the sequence from missing numbers due to server shutdown, etc.Since my sequence definition above uses an
int
data type, and starts with -2147483647, it will generate 4,294,967,296 unique numbers before an error is generated by theNO CYCLE
property. If you remove theNO CYCLE
property, the sequence will reset to -2147483647 once it hits 2147483648. It that is not a large enough numeric range, you could always use abigint
as the datatype.