Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my auto-incremented Id skip numbers in SQL Server? [duplicate]

And they seem to be fairly large skips, in the thousands even. Here's an example:

Exhibit A

The last Ids of this table go above the 30.000 mark even though there are less than a thousand rows in the table.

  • What is causing this?
  • How can I prevent this in the future?
  • How can I stop my current table from continuing on this dark road?

EDIT: This is happening in an old, small desktop application of mine and I have not ever deleted rows from this table nor have I used any transactions and rollbacks.

like image 470
Alternatex Avatar asked May 16 '15 13:05

Alternatex


People also ask

Why does identity column skip numbers?

Usually, it occurs when the SQL Server instance is being forced to restart. This skipped gap is particularly depending on the data type of the column, and most of the time, it is possible that column type can be INT, BIGINT, or Numeric.

Why does SQL identity jump 1000?

In SQL Server 2012 - MS had introduced 'Identity Cache'. This feature had a bug of auto-incrementing ID column by '1000'. For example, if ID columns are 1, 2 and when an ID Jump happens the next ID column is 1003, instead of '3'. There are workarounds available to fix this issue.

How do I automatically increase ID in SQL?

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5) .


2 Answers

What is causing this?

A couple of potential causes come to mind:

  • Rows were deleted?
  • The results you're looking at aren't sorted by id?
  • Identifiers were allocated in a transaction which wasn't committed?
  • The database engine allocated potential identifiers as an internal performance tuning and that allocation was lost (unexpected server re-start, for example)?

There could be more potential causes I'm not thinking of.

How can I prevent this in the future?

Depends on the cause. But it's kind of a moot point, really. Why would you need to "prevent this"? What exactly is wrong with this situation? Identifiers don't need to be consecutive, they just need to be unique. (And preferably sequential for an index, otherwise the database will have to re-build the index.)

How can I stop my current table from continuing on this dark road?

The dark road of... generating unique identifiers? Well, I guess you could manually supply unique identifiers. GUIDs are good for that sort of thing. There are pros and cons, though. (I don't know if recent implementations have improved this, but historically GUIDs don't make for a good clustered index.)

like image 187
David Avatar answered Sep 28 '22 04:09

David


This is by design in SQL server. SQL Server guarantees that the next number generated will be unique and incremental, it does not guarantee it will be in sequence.

This started in SQL 2012. Simply restart the service to repro the issue. It is well know change in behavior, it has always been documented this way, and is marked by design. Its just an unexpected change in behavior

like image 31
Greg Avatar answered Sep 28 '22 06:09

Greg