I have a table with pretty basic design: 1 primary key (id)
, 1 unique column (filename)
(the filename of the file I am tracking)
The primary key is an identity
column (bigint
) (I had to make it bigint
recently because it's growing so fast)
There are only 82318 rows in the table but the identity for the last row I inserted is 2147891928.
Identity spec is on, starts at 1 increment by 1
If I go through the table ordered by id DESC
I can see it jumped from time to time, sometimes by 40, sometimes by 100, 1000, 10000 or more. Is this related to the SQL Server restart seed "FEATURE" in SQL Server 2012? Because it sounds slightly different
I have a script that runs and tries to insert filenames into the SQL from a folder, if the filename already exists nothing should happen because its setup as unique right? or does it use an auto increment digit?
Best Answer
Identity values are not affected by
ROLLBACK
operations, so if you insert rows in your table and then issue aROLLBACK
, the identity column will be incremented anyway. Even if yourUNIQUE
constraint causes theINSERT
commands to fail, the identity values are consumed anyway.Example:
The SQL Server 2012 jump in identity columns might be part of your issue, but I guess it has a minor impact on what you're seeing.
Identity values are not guaranteed to be contiguous by design, so if you're relying in "no holes" in your identities, you may want to reconsider your assumptions.