SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Best code for generating sequence numbers


Best code for generating sequence numbers

Author
Message
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85951 Visits: 41091
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Paul White
Paul White
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15682 Visits: 11355
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 :-D

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 Sad

Paul



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14728 Visits: 9518
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). :-D

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Paul White
Paul White
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15682 Visits: 11355
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). :-D

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
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14728 Visits: 9518
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."
Paul White
Paul White
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15682 Visits: 11355
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
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
mstjean
mstjean
Mr or Mrs. 500
Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)

Group: General Forum Members
Points: 561 Visits: 2546
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
Tergum Rufus
Tergum Rufus
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 108
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?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85951 Visits: 41091
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
pparsons
pparsons
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 159
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 Smile

Phil
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search