Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: lmhobbs@tcp.co.uk (Lilian Hobbs)
Subject: Re: Generating unique numbers
Date: 1995/12/12	[thread overview]
Message-ID: <4al3k8$f80@zeus.tcp.co.uk> (raw)
In-Reply-To: 4aic03$3u1@cantua.canterbury.ac.nz

In article <4aic03$3u1@cantua.canterbury.ac.nz>, E.Havelaar@Regy.Canterbury.Ac.NZ 
says...
>
>We have an RdB database on top of VAX VMS (soon to migrate to Alpha) for which 
>I need to generate a unique account number whenever I create a new record.  I 
>wonder if anyone has any experience on what the best way to do this is.
>Options that have crossed my mind are:
>
>[1] Write some SQL procedure which grabs MAX(ACC_NO), adds 1, and inserts a 
>record using this value as its new ACC_NO.  Presumably locking / re-entrancy 
>problems could be taken care of by using the right type of transaction -- my 
>guess:
>   set transaction read write wait reserving <table> for exclusive write;
>I am worried that (a) this will not be very fast and (b) I may already have a 
>transaction open, (e.g. a read-only transaction on a different table) which I
>don't want to commit just yet.
>

Please don't use the MAX function unless you have only a few rows. It takes time
to compute and could end up locking your data. Unless of course you have an alpha
with the table pinned in memory.



>[2] Store the next available number in a file, and allow the RMS services to 
>handle the locking problems.  This makes it hard for something like SQL 
>services on a remote machine to use.
>
Not good, you now have data outside the database, how do you recover with the 
right values.


>[3] Volatile memory with some fancy semaphore mechanism.  Nice and fast, but 
>disastrous after a reboot, and of course, not accessible from another node let 
>alone a PC.
>
This has been done.

My personal favourite, and it depends upon your auditting requirements 
and whether you can have missing numbers. Is to have a single table
with 2 columns. 1st column is a unique integer number say 123 and the 
2nd column is your account number. Hashed index on column1 for fast access.

If you can have missing account numbers then start trans, read row 
to get next account number, add 1, update, commit 

Otherwise wait until the very end of the transaction and then read
and update by 1.  

Ofcourse, once you get to Rdb7 you may be able to use the record cache
feature to pin the row into memory.

================================================================
Dr. Lilian Hobbs
Email: lmhobbs@tcp.co.uk       WWW http://www.tcp.co.uk/~lmhobbs
                'Wishing does not make it so'
=================================================================




  reply	other threads:[~1995-12-12  5:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-12-11  5:00 Generating unique numbers Ed Havelaar
1995-12-12  5:00 ` Lilian Hobbs [this message]
1995-12-14  5:00   ` Albert Godfrind
1995-12-13  5:00 ` Stephen Hoffman
1995-12-14  5:00 ` Generating unique numbers, etc Ed Havelaar
1995-12-14  5:00   ` Norm Lastovica
1995-12-15  5:00   ` Lilian Hobbs
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox