Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Best code for generating sequence numbers Expand / Collapse
Author
Message
Posted Saturday, February 6, 2010 9:24 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:19 PM
Points: 35,595, Visits: 32,189
peter-970097 (2/6/2010)
Thanks Jeff for the informative reply.
Independently of this blog, we very quickly canned any further thought on ISOLATION LEVEL et aliter
All the tests we did with this were catastrophic!

We will work on your suggestion and blog the outcome.
Rgds
Peter


Thanks for the feedback, Peter. I look forward to your follow up especially since I went through it once myself. Let us know if we can be of any further assistance.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #861254
Posted Saturday, February 27, 2010 11:02 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 9,928, Visits: 11,196
CirquedeSQLeil (2/6/2010)
Would a sequential guid work in place of an int?

Yes, but they have problems of their own. Quite apart from the issues of key width and getting the darn things in some sort of order so your system doesn't disappear in a puff of page-splits, they're horrible to work with. I mean that in a practical sense. Any DBA that has had to use GUIDs a lot gets plenty of practice with copy and paste It's also tedious typing UNIQUEIDENTIFIER all the time - how much easier is 'INT'?

These may sound like small objections, and they probably are. I could go on about the technical weaknesses of 'ordered' GUIDs for a while, but I will try to resist the temptation. The everyday irritations are the thing that finally turned me off to GUIDs.

Jeff knows what's coming next.

Sequence Tables are a pretty perfect solution for the sort of problem that prompted this thread - if implemented correctly. This was nigh-on impossible to do in SQL Server 2000, but we've had SQL Server 2005 for a while now

I have written an article on Sequence Tables which I hope will be published here on SSC shortly. Jeff is one of the technical reviewers

BTW Jason, the second link in your post is now broken

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #874122
Posted Sunday, February 28, 2010 12:06 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 11:54 AM
Points: 9,294, Visits: 9,492
Paul: So on RollBacks, will it leave permanent gaps, try to recycle the gaps, or does it guarantee no gaps, ever? (I am pretty sure that this last choice cannot be done, by anything, without some kind of frequent deadlocking or severe blocking).

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #874134
Posted Sunday, February 28, 2010 12:15 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 9,928, Visits: 11,196
RBarryYoung (2/28/2010)
Paul: So on RollBacks, will it leave permanent gaps, try to recycle the gaps, or does it guarantee no gaps, ever? (I am pretty sure that this last choice cannot be done, by anything, without some kind of frequent deadlocking or severe blocking).

Like IDENTITY, it leaves gaps on ROLLBACK - as you say, all non-blocking methods do.
It is, however, possible to avoid gaps if a 'pool' of pre-allocated keys is cached and managed by a separate component.
That is an implementation detail to be handled by the application.

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #874137
Posted Sunday, February 28, 2010 1:10 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 11:54 AM
Points: 9,294, Visits: 9,492
Cool. That would be my choice, simpler.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #874142
Posted Sunday, February 28, 2010 1:25 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 9,928, Visits: 11,196
RBarryYoung (2/28/2010)
Cool. That would be my choice, simpler.

Quite. The pooled keys would be allocated from a Sequence Table, naturally




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #874144
Posted Monday, March 1, 2010 1:39 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, November 14, 2014 9:55 AM
Points: 436, Visits: 2,311
If I am not misreading the original request, I think you are trying to get multiple pseudo-identity columns in a single table.

This is a somewhat squirrely approach but it'd probably work for you as long as possible holes in your sequences are not a problem.
This code is based on a discussion some time back (it may have been here at SSC; does anyone still have the link?) about doing pretty much what you wanted-- except for the "multiple columns" requirement. Each column that needs its own pseudo identity value needs its own SneakyIdentity table... Ideally you'd lock it down and document the bejeebers out of it so nobody stumbles in and tries to figure out what an empty table is doing in the system. (and potentially mungs the identity value) Add appropriate error trapping. You may want to put the code shown in a USP.


-- create the table to start
-- create table SneakyIdentity(rowid bigint identity PRIMARY KEY)
-- drop table SneakyIdentity

-- insert bumps "next identity row" but the rollback prevents the row from getting saved
begin transaction
insert into SneakyIdentity DEFAULT VALUES
rollback transaction

-- IN USP, this would actually be an OUTPUT parm so wouldn't need to declare here...
declare @uid bigint
select @uid = Scope_Identity()
select @uid Retval

-- the table stays empty
select * from SneakyIdentity




Cursors are useful if you don't know SQL
Post #874682
Posted Monday, March 1, 2010 3:26 PM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 16, 2014 4:41 PM
Points: 20, Visits: 106
That is sneaky! How would you manage the locking and multi-user aspects of this, ie to prevent thousands of calls per second retrieving the same ID?
Post #874760
Posted Monday, March 1, 2010 3:53 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:19 PM
Points: 35,595, Visits: 32,189
peter-970097 (3/1/2010)
That is sneaky! How would you manage the locking and multi-user aspects of this, ie to prevent thousands of calls per second retrieving the same ID?


The value of the identity property doesn't rollback with the transaction and the insert can happen many times within the same table. It's no where's near as contentious as an UPDATE. It would be interesting to see if deadlocks occur or not on this.

It would be diffcult to reserve a range of ID's with because IDENTITIES are not guaranteed to be sequential if other people are also inserting into the table.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #874771
Posted Monday, March 1, 2010 4:12 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 21, 2014 11:27 AM
Points: 21, Visits: 96
From NOT a DBA ...

sounds like what you really need is an Oracle "sequence". I have implemented similar as follows:
CREATE PROCEDURE dbo.nextval @sequence varchar(28) AS
SET NOCOUNT ON
DECLARE @sql nvarchar(100), @scope_identity int
SET @sql = 'INSERT ' + @sequence + ' default values SELECT @scope_identity=SCOPE_IDENTITY()'
BEGIN TRANSACTION NEXTVAL
SAVE TRANSACTION SEQUENCER
EXEC sp_executesql @sql, N'@scope_identity INTEGER OUT', @scope_identity OUT
ROLLBACK TRANSACTION SEQUENCER
COMMIT TRANSACTION NEXTVAL
SET NOCOUNT OFF
SELECT @scope_identity;
GO
with a created table (@sequence) that contains only an identity column, this concept allows any number of "sequences".

The upside: locking is minimized or eliminated, the table size never changes (more or less)
The downside: there maybe gaps in the sequence

SQL Server has this "useful" feature, it never rolls back an identity value assigned, so even though the transaction is rolled back the identity value is not :)

Phil
Post #874779
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse