SQL:设置IDENTITY_INSERT ON会禁用更新表的标识表吗?

tho*_*aux 2 sql sql-server performance identity-column

我目前正在研究数据迁移项目和与性能相关的问题,我想预定义一组身份,而不是让表自己生成它们.

我发现将identity属性添加到列并不容易,所以我想使用IDENTITY_INSERT ONstatement.

我的问题是:这会禁用对表的标识表的更新(这会影响性能),还是我需要真正删除identity列的属性?

she*_*tie 10

数据迁移脚本通常具有以下特征:

SET IDENTITY_INSERT [MyTable] ON
INSERT INTO [MyTable] ...
INSERT INTO [MyTable] ...
INSERT INTO [MyTable] ...
...
SET IDENTITY_INSERT [MyTable] OFF
Run Code Online (Sandbox Code Playgroud)

启用后,该字段不会自动增加其他插入.

IDENTITY_INSERT具有会话范围,因此只有您的会话才能显式插入标识行.并且会话中只有一个表可以一次打开IDENTITY_INSERT.

性能呢?我实际上没有回答你的问题,但我有一些代码可以给你一个答案.这是我在这里找到的东西的修改版本:

/* Create a table with an identity value */
CREATE TABLE test_table
  (
     auto_id  INT IDENTITY(1, 1),
     somedata VARCHAR(50)
  )
GO 

/* Insert 10 sample rows */
INSERT INTO test_table
SELECT 'x'
GO 10

/* Get the current identity value (10) */
SELECT Ident_current('test_table') AS IdentityValueAfterTenInserts

GO

/* Disable the identity column, insert a row, enable the identity column. */
SET identity_insert test_table ON
INSERT INTO test_table(auto_id, somedata)
SELECT 50, 'x'
SET identity_insert test_table OFF 

GO

/* Get the current identity value (50) */
SELECT Ident_current('test_table') AS IdentityValueAfterIdentityInsertWithIdentityEnabled

GO

/* Disable the identity column, insert a row, check the value, then enable the identity column. */
SET identity_insert test_table ON
INSERT INTO test_table(auto_id, somedata)
SELECT 100, 'x'

/* 
   Get the current identity value (?) 
   If the value is 50, then the identity column is only recalculated when a call is made to:
       SET identity_insert test_table OFF
   Else if the value is 100, then the identity column is recalculated constantly and your 
   performance problems remain.
*/
SELECT Ident_current('test_table') AS IdentityValueAfterIdentityInsertWithIdentityDisabled


SET identity_insert test_table OFF 

GO
/* Get the current identity value (100) */
SELECT Ident_current('test_table') AS IdentityValueAfterIdentityInsertWithIdentityEnabled

GO

DROP TABLE test_table
Run Code Online (Sandbox Code Playgroud)

我没有一个SQL SERVER方便运行它,所以让我知道它是怎么回事.希望能帮助到你.