2011-03-10 2 views
2

현재 데이터 마이그레이션 프로젝트를 진행 중입니다. 성능 관련 문제에 대해 ID를 미리 정의하고 테이블을 생성하도록하고 싶습니다.SQL : IDENTITY_INSERT를 ON으로 설정하면 테이블의 ID 테이블 업데이트가 비활성화됩니까?

열에 identity 속성을 추가하는 것이 쉽지 않기 때문에 IDENTITY_INSERT ON 문을 사용하고 싶습니다.

질문 : 성능에 영향을주는 테이블의 ID 테이블에 대한 업데이트를 사용하지 않거나 열의 identity 속성을 제거해야합니까?

SET IDENTITY_INSERT [MyTable] ON 
INSERT INTO [MyTable] ... 
INSERT INTO [MyTable] ... 
INSERT INTO [MyTable] ... 
... 
SET IDENTITY_INSERT [MyTable] OFF 

동안 다른 삽입을위한 필드는하지 않습니다 자동 증가를 사용 : 데이터 마이그레이션 스크립트와 같은 뭔가를 할

+2

신원을 없애기 위해 열을 변경할 수없는 경우 정확히 동일한 열은 있지만 신원이없는 두 번째 테이블을 작성한 다음 기존 데이터를 모두 삽입 한 다음 이전 데이터를 삭제하십시오. 이 모든 것은 코드 조각에 의해 자동으로 수행 될 수도 있습니다. – Krumelur

+0

Microsoft SQLServer를 사용하고있는 것으로 보입니다. 그 맞습니까? –

+0

@jon_darkstar, 맞습니다. :) @Krumelur, 마이 그 레이션 된 데이터에 대한 ID가 필요하기 때문에 테이블에 의해 생성되는 것을 원하지 않습니다. – thomaux

답변

10

은 매우 일반적입니다.

IDENTITY_INSERT에는 세션 범위가 있으므로 세션 만 ID 행에 명시 적으로 삽입 할 수 있습니다. 세션의 한 테이블 만 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 

내가 이것을 실행할 수있는 편리한 SQL Server를, 그래서 내가 어떻게되는지 알려하지 않습니다 내가 here을 발견 무언가의 수정 된 버전입니다. 희망이 도움이됩니다.

+0

고맙다 Xcaliburp, 나의 늦은 대답을위한 sory! – thomaux

+0

@Anzeo - 내가 궁금한 점은 신원 삽입이 진행되는 동안 신원 값이 계속 증가 했습니까? – sheikhjabootie

+0

필자는 (아직) 필자의 접근 방식을 약간 변경 했으므로 테스트하지는 못했지만, 증가하지는 않을 것이므로 마지막에 ID 컬럼을 다시 채워야 할 것이라고 생각한다. – thomaux

관련 문제