Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identity column increment jump [duplicate]

I am experiencing something funny in my database. The primary key increases like:

1
2
3
4
5
6
7
8
1001

I'm using EntityFramework or sometimes LINQ to SQL.

like image 887
ojorma Avatar asked Aug 07 '13 12:08

ojorma


People also ask

Can identity column have duplicate values?

Yes you can insert but should be avoided by having a unique constraint or Pkey on identity column. Since, it doesn't make much sense, if you want to put duplicate value in identity column then why add identity option to column int. Just leave it as is.

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.

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.

How many identity increment is jumping in SQL Server database?

135 Identity increment is jumping in SQL Server database 5 identity jumped by 10000 in SQL Server 2012 0 Identity column not generating correct IDs 2 Identity Column Seems to Skip Values

Is your identity column value jumping or incremented by 1000?

It would be impossible to find DBA or Developer who has not faced an issue when their identity column value has jumped or incremented by 1000. It is a very common issue and today we will see the solution of this issue with the help of IDENTITY_CACHE introduced in the latest version of SQL Server.

What is the column identity increment for 2012 SQL Server 2012?

SQL Server 2012 column identity increment jumping from 6 to 1000+ on 7th entry [duplicate] Ask Question Asked8 years, 11 months ago Active3 years, 9 months ago

How to avoid gaps in the values of the identity column?

To avoid gaps in the values of the Identity column in cases where the server restarts unexpectedly or fails over to a secondary server, disable the IDENTITY_CACHE option. This option is similar to the existing SQL Server Trace Flag 272, except that it can be set at the database level rather than only at the server level.


2 Answers

It happens when SQL server 2012 loses its pre-allocated sequence numbers.

If you want to get rid of that, one option is to use traceflag:

DBCC TRACEON (272)

Another option is to use a sequence (with no caching) instead of identity:

CREATE SEQUENCE MySeq AS int
  START WITH 1
  INCREMENT BY 1
  NO CACHE;

See this: http://www.big.info/2013/01/how-to-solve-sql-server-2012-identity.html

like image 92
OzrenTkalcecKrznaric Avatar answered Sep 22 '22 16:09

OzrenTkalcecKrznaric


This is all perfectly normal. Microsoft added sequences in SQL Server 2012, Have a look here a link for some explanation.

If you want to have the old behaviour, you can:

use trace flag 272 - this will cause a log record to be generated for each generated identity value. The performance of identity generation may be impacted by turning on this trace flag. use a sequence generator with the NO CACHE setting (http://msdn.microsoft.com/en-us/library/ff878091.aspx)

like image 37
RAJESH KUMAR Avatar answered Sep 21 '22 16:09

RAJESH KUMAR