1

I'm working on a project that has hit a bit of a snag: I'm replacing some replication with a home built system so that if replication goes down we don't blow our SLAs with our customers and shell out hundreds of thousands of dollars.

In my research, I learned that inserting records alongside identity values can cause the next identity for the table to be reset, which is something I want to avoid due to key range management issues.

It then seems like the obvious solution would be something like this:

DECLARE @Identity VARCHAR(15), @SQL VARCHAR(MAX)
SELECT @Identity = IDENT_CURRENT( 'dbo.My_Table' )

SET IDENTITY_INSERT dbo.My_Table ON
-- Do my inserts here

SET @SQL = 'DBCC CHECKIDENT ( ''dbo.My_Table'', RESEED,' +  @Identity + ')'
EXEC (@SQL)

Would I be killing myself with locking if this happens on a highly used table? Alternatively, has anyone figured out a way to insert identities like replication does without causing the identity value to get reset?

1
  • Ignorant here, does replication not use SET IDENTITY_INSERT dbo.MyTable ON;
    – billinkc
    Commented Dec 2, 2013 at 19:53

2 Answers 2

1

Its always tricky when you try to replicate Identity columns.

You should set NOT FOR REPLICATION option on the identity column when you replicate identity field column.

This way only user inserts cause the value to be incremented at the subscriber/s.

You can even assign chunks of identity values and have them self maintained at subscribers. WHen doing this, you have to be careful as

During a large batch insert the replication trigger is fired only once, not for each row of the insert. This can lead to a failure of the insert statement if an identity range is exhausted during an large insert, such as an INSERT INTO statement.

You can see what types of locks are generated using sys.dm_tran_locks DMV. Doing a reseed is very instantaneous and is not a big overhead. You just have to thoroughly test your idea for all possible cases to avoid any surprises.

Also, out of curiosity, how is your solution more efficient than T-Rep ? As T-Rep is tested since many many years to be more efficient than any custom/home grown solution.

2
  • Kin, I appreciate your response, however we don't have the luxury of using the built in replication features of SQL Server: That's what we're trying to replace. Is there any way for a user query to run down the same path as something coming from replication where an identity column is marked as NOT FOR REPLICATION?
    – tmcg
    Commented Dec 4, 2013 at 15:18
  • 1
    In response to your question about T-Rep: We currently use both merge and transactional replication among our 5 primary servers. The problem we've encountered is when replication crashes entirely, it takes so long to spin it back up that we blow our SLAs, mostly due to the process where one server's copy of the replicated table is blown away and replaced with a copy from the other server. Even when the data is 100% in sync.
    – tmcg
    Commented Dec 4, 2013 at 15:22
0

I wrote own replication system for SQL Server and it works for 3 years. It has ~70-90 subscribers, it's faster then SQL merge replication (for my specific applications), but it has some problem with locks.
To make a long story short:
– I have own range mechanism for identity column (every subscriber has some range)
– I have some constraint for inserting rows (to make sure that inserting rows is from specyfic range)
– I put rows into table by: identity insert on and then dbcc reseed.

it wasn't as easy as it looked in the beginning, but it works ok.

dbcc reseed needs sch-m lock, so it's the biggest disadvantege of the replication mechanism. Sometimes locks wait for long transaction...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.