HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Why are Denali sequences supposed to perform better than identity columns?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
whyidentitycolumnsarethandenalibettersupposedperformsequences

Problem

In his answer to Which is better: identity columns or generated unique id values? mrdenny says:


When SQL Denali comes out it will support sequences which will be more efficient than identity, but you can't create something more efficient yourself.

I'm not so sure. Knowing Oracle's sequences, I have either to create a trigger for insert, encapsulate each insert into a call of a stored procedure, or pray that I do not forget to properly use the sequence when I do an ad-hoc insert.

I doubt that the advantages of sequences are so obvious.

Solution

I'll answer here as well. It has to do with the internals of how IDENTITY and SEQUENCE work.

With IDENTITY, SQL Server pre-caches values into memory so that they are readily available. See Martin Smith's answer for the details. As values are used, a background process generates more values. As you can imagine this pool can run out pretty quickly, leaving the application at the mercy of the background process that is generating the values.

With SEQUENCE, SQL Server allows you to define how large the cache should be. While SQL Server doesn't actually keep the values in the cache, it only keeps the current value and the top end value, this will greatly reduce the amount of IO that is needed to create values.

Don't set the cache too high, as this will reduce the number of numbers which can be used: if SQL Server were to crash, any values specified in the current cache range which weren't used would be lost.

As for row insertion, just specify a default value for the column, like so:

DEFAULT (NEXT VALUE FOR Audit.EventCounter),

Code Snippets

DEFAULT (NEXT VALUE FOR Audit.EventCounter),

Context

StackExchange Database Administrators Q#1635, answer score: 38

Revisions (0)

No revisions yet.