Question

Not sure if I'm over-thinking about things here... but ..

I'm using MS Sql Server (I'm interested in answers for 2008 / 2012 / 2014).

I have a simple Table which has an Identity field. Each time a new row is inserted, it auto-gen's the Id. Started at 0 with an increment of 1. Basic stuff.

When ever I update a row, I'm doing a DELETE and then INSERT. (Yes, i know about MERGE and can use that, but I'm not).

Right now i'm at Id: 11952106.

There's lots of numbers that have been unsed. Eg. 1, 2, 3 .. etc.. I did use them but then deleted them

So .. will Sql Server ever start to use those 'gap' numbers? (Not sure of the tech term for that).

Was it helpful?

Solution

When the IDENTITY gets to the maximum value for your data type (for int that is about 2 billion values) the insert will fail with an "overflow" type message. It does not wrap around to zero (or negative 2 billion-ish) nor does it start filling in the gaps.

This happened to me a couple of months ago. It's a pain in the ass. We now have monitoring in place to predict when our IDENTITY columns will hit their limits.

There are techniques for shuffling rows along to re-use the gap values. These have to be coded by yourself, however. Be very careful about foreign keys, both explicit and implied.

OTHER TIPS

Just to expand on the comments which actually answers your question (which is No by the way).

However to provide a little more information about it, but Identity columns aren't meant as an unbroken sequence value you can use to calculate a guarenteed order by on. Although in many situations, you will get this, until you start deleting as well, but the identity does not guarantee there won't be gabs in the sequence due to failed inserts, rollbacks and even server restarts.

Its primary use is to generate a key, often used as a surrogate key because the normal key is too complex/long - and/or to create uniqueness via an index or constraint across the dataset, but you should not 'expect' the value to be an unbroken sequence of numbers.

If you want an unbroken sequence, you'll need to create it yourself in either your code layer or by making functions, but I suspect you'll only want to do this if you're aware of the performance overhead in it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top