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.
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.
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.
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.
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
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.
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
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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With