Skip to main content
2 of 5
added 74 characters in body
Randi Vertongen
  • 16.5k
  • 4
  • 35
  • 64

Over a duration of about 15 minutes, it appears as if 2798 rows have "disappeared" from the table. The primary key simply jumps from 3561297 to 3564095. During the same time period, I've got debug logs from clients of this API which indicates that something went wrong during the request. My IIS log notes that any request which causes database access during this time period had taken between 30000 and 40000 ms to complete.

I don't think the rows have disapeared.

Your debug logs show that something went wrong during requests to the database, if queries are aborted & rolled back the identity values will not roll back with it.

Another example is that the application has a timeout window of 30 seconds, resulting in the query being cancelled and the insert being rolled back.

In this case the identity value allocated will not be rolled back, the next identity value in line will be used when retrying the insert.

There could have been other reasons that the insert was failing, such as network loss, inserting values that would be truncated, wrong datatype conversions, ....

Below is a reproducible example that shows two gaps:

CREATE TABLE dbo.identitytable(id int identity(1,1), val varchar(255))
INSERT INTO dbo.identitytable(val)
VALUES('BLA1');

GO
BEGIN TRAN
INSERT INTO dbo.identitytable(val)
VALUES('BLA2')

ROLLBACK TRAN

GO

INSERT INTO dbo.identitytable(val)
VALUES('BLA3')

GO

SELECT * FROM dbo.identitytable

GO

INSERT INTO dbo.identitytable(val)
VALUES(REPLICATE('B',256));
GO

INSERT INTO dbo.identitytable(val)
VALUES('BLA5');


SELECT * FROM dbo.identitytable;

With the last select producing a result with two gaps:

id  val
1   BLA1
3   BLA3
5   BLA5

If an insert with multiple rows is rolled back then the gap will be bigger too:

BEGIN TRAN
INSERT INTO dbo.identitytable(val)
SELECT 'BLA6'
FROM master..spt_values
ROLLBACK TRAN


INSERT INTO dbo.identitytable(val)
VALUES('BLA7');



SELECT * FROM dbo.identitytable;

Result

id  val
1   BLA1
3   BLA3
5   BLA5
2546    BLA7

DB<>Fiddle

Randi Vertongen
  • 16.5k
  • 4
  • 35
  • 64