简体   繁体   English

SQL Server标识列递增而不插入

[英]SQL Server identity column incrementing without insert

I have a C# console app which is using a stored procedure to insert rows into a SQL Server database. 我有一个C#控制台应用程序,它使用存储过程将行插入SQL Server数据库。 Currently the database has about 1m rows, however the ID column is up at 26m! 目前,数据库有大约1米的行,但ID列的最大长度为26米!

On the last successful insert the ID went from 16m to 26m. 在最后一次成功插入时,ID从16米到26米。 The app runs multiple threads in parallel so there is a chance it could be trying to insert two things at the same time. 该应用程序并行运行多个线程,因此有可能尝试同时插入两个东西。

Is there anything other than a transaction rollback that would cause the identity column to increment without adding a row? 除了事务回滚之外还有什么可以导致标识列在不添加行的情况下递增吗?

It seems unlikely that there were 10 million times that two threads tried to write a conflicting row at the same time. 似乎不太可能有两千个线程试图同时写一个冲突的行。

I'm using a temporary table variable which is populated by passing XML into the stored proc: 我正在使用一个临时表变量,它通过将XML传递到存储过程来填充:

-- insert data if it doesn-t already exist  
insert into DataStore (DataText, DataSourceID)
select distinct td.dataText, td.dataSource 
  from @tempData td
 where not exists ( select 'x' 
                      from DataStore ds 
                     where ds.DataText = td.dataText );

What can leave gaps? 什么可以留下空白?

I'd also check the increment value using IDENT_INCR in case it is really is 5 million or such 我还会使用IDENT_INCR检查增量值,以防它真的是500万或类似

Inserted and deleted record increments identity counter, as mentioned by @gbn - Rollbacks and c-violations also, but there is two more options: 插入和删除记录增量身份计数器,如@gbn所述 - 回滚和c违规,但还有两个选项:

  • SET IDENTITY_INSERT SET IDENTITY_INSERT
  • DBCC CHECKIDENT(..., RESEED) DBCC CHECKIDENT(...,RESEED)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL身份列(ID)在插入时未递增 - Sql identity column (ID) is not incrementing on insert 在 Sql 服务器中使用 BYTE 作为标识列类型时,EF 核心“无法为标识列插入显式值” - EF core "cannot insert explicit value for identity column" when using BYTE as identity column type in Sql Server 如何在没有身份列的SQL Server中查找最后插入的行 - How to find last inserted row in SQL server without identity column SQL Server-格式标识列 - SQL Server - formatted identity column 如何在.net核心中插入具有自动递增字段的实体而又不弄乱SQL Server'Identity_insert on'? - How to insert an entity with autoincremented field in .net core without messing with SQL Server 'Identity_insert on'? 如何插入 MS SQL 中的身份列 - How to insert into an identity column in MS SQL Sql Server Ce 3.5标识插入 - Sql Server Ce 3.5 Identity insert 主键列已设置默认标识(1, 1)时如何向sql server数据库插入数据 - How to insert data into sql server database when the primary key column already set default identity(1, 1) 在C#中使用LINQ到SQL处理自动递增的IDENTITY SQL Server字段 - Handling auto-incrementing IDENTITY SQL Server fields with LINQ to SQL in C# SQL Server中的标识列暴露了奇怪的行为 - Identity column in SQL Server exposes strange behaviour
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM