Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* Generating unique numbers
@ 1995-12-11  5:00 Ed Havelaar
  1995-12-12  5:00 ` Lilian Hobbs
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Ed Havelaar @ 1995-12-11  5:00 UTC (permalink / raw)


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.

[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.

[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.

Any recommendations?


- Ed.
______________________________________________________________________
Eduard E Havelaar             | The fool says in his heart,
Information Services Section  | "there is no God"...
University of Canterbury      | 
Christchurch, New Zealand     |                        - Psalm 14:1




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Generating unique numbers
  1995-12-11  5:00 Generating unique numbers Ed Havelaar
@ 1995-12-12  5:00 ` Lilian Hobbs
  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
  2 siblings, 1 reply; 7+ messages in thread
From: Lilian Hobbs @ 1995-12-12  5:00 UTC (permalink / raw)


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'
=================================================================




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Generating unique numbers
  1995-12-11  5:00 Generating unique numbers Ed Havelaar
  1995-12-12  5:00 ` Lilian Hobbs
@ 1995-12-13  5:00 ` Stephen Hoffman
  1995-12-14  5:00 ` Generating unique numbers, etc Ed Havelaar
  2 siblings, 0 replies; 7+ messages in thread
From: Stephen Hoffman @ 1995-12-13  5:00 UTC (permalink / raw)



In article <4aic03$3u1@cantua.canterbury.ac.nz>, E.Havelaar@Regy.Canterbury.Ac.NZ (Ed Havelaar) writes:
: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:

  You don't mention the frequency of this particular operation.

  I use imbedded SQL; I read the "max id" value from the database, and
  and write the (incremented) result back to the database.

    EXEC SQL
    SET TRANSACTION READ WRITE WAIT ISOLATION LEVEL SERIALIZABLE RESERVING
        xxx_ID_TABLE FOR PROTECTED WRITE;
    if (SQLCA.SQLCODE != 0) SQL$SIGNAL();

  There are various suggestions and tradeoffs on this particular topic
  discussed the Rdb manuals.

	--

  If you want to use a faster mechanism, read the ID value into a lock
  value block and maintain it there -- this does not help you from a PC,
  but it does work nicely across all nodes in a VMScluster.  You'll need
  to design the restart code to reload the value in the lock value block
  from the highest known ID value present in the database -- this logic
  is needed for the recovery after a system crash.

	--

  I don't recommend pursuing the RMS file services approach, nor the
  volatile memory mechanism, mentioned in your original posting.  Both
  can be made to work, but they do not appear to have advantages over
  storage in Rdb or storage in a lock value block.

  ------------------------------ Opinionative -------------------------------
   Stephen Hoffman      OpenVMS Engineering      hoffman@xdelta.enet.dec.com
                 Insunt interdum menda in eo quod est efficax
  ---------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Generating unique numbers, etc.
  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
  1 sibling, 0 replies; 7+ messages in thread
From: Norm Lastovica @ 1995-12-14  5:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1454 bytes --]


In article <4aq3kt$nia@cantua.canterbury.ac.nz>, E.Havelaar@Regy.Canterbury.Ac.NZ (Ed Havelaar) writes:

�Suppose I have a program that takes ages to run and keeps a read-only 
�transaction open for its duration (e.g. to perform calculations on all 100000 
�records in a table),  Every now and again, I want to assign an account number.  
�This involves starting a read-write transaction for as short a time as 
�possible.  How can I do this without terminating my hours-long 
�read-only transaction and somehow figuring out where and how to recommence it?

	what version of Rdb are you using?  I'll assume that you are
using some relatively current version (6.1).  

	how are the records in your original readonly transactions selected?
if they are based on some unique, sorted, ascending key, then you could
just keep track of the last key that you processed, perform the read-write
transaction, and then start up at the record with the key greater than your
last key.

	if you are only gonna do a peak of 400 inserts per hour, I'd not 
bother with the lock value block or any of the performance 'enhancers'.  
this is a pretty low insert rate.  the trigger would be a really nice way
to do the counter.  the folk lore of triggers being slow probably isn't
completely accurate, they'll execute at the same speed as any old sql
statement.
	

-- 
the opinions expressed here are my own and hardly ever
reflect those of Oracle Corporation.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Generating unique numbers
  1995-12-12  5:00 ` Lilian Hobbs
@ 1995-12-14  5:00   ` Albert Godfrind
  0 siblings, 0 replies; 7+ messages in thread
From: Albert Godfrind @ 1995-12-14  5:00 UTC (permalink / raw)



In article <4al3k8$f80@zeus.tcp.co.uk>, lmhobbs@tcp.co.uk (Lilian Hobbs) writes...
>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:

[...]

I'd like to add to what Lilian said.

>My personal favourite, and it depends upon your auditing 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.  

This is the solution I would recommend too. There are also some optimizations
you may be able to do:

- have each process grab a slice of numbers at a time (say 10), then use one of
  those numbers for each new account. Once the slice is exhausted, go get 10
  more (start a transaction, fetch the counter, save its value, increment by
  10, write it back, commit). 

  The benefits: less contention on the counter (it could become a real hot spot
  on insert-intensive environments). Also less overhead in getting numbers.

  The drawbacks: If an insert process crashes, you have just lost up to slice
  of 10 numbers. So, this comes back to a reservation Lilian was making - do
  your business or applications rule allow for gaps in the numbering ?
  Another drawback: the numbering of records no longer reflects the 'age' of
  records (record 121 could be inserted before [in time] record 111 is
  inserted). This may or may not be an issue.

- I suppose the numbers you get will be used as the primary key of the account
  records. There will probably be a btree index on the primary key. So, all
  new rows will cause key insertions at tyhe "right" edge of the index tree,
  causing much contention if you have many concurrent inserters.

  If numbers don't need to be consecutive, then consider "randomizing" them 
  slightly. An easy transformation is to invert the digits in the numbers.
  For example, instead of inserting number 110,111,112,113,115... you will
  insert numbers 11, 111, 211, 311, 511 etc. The keys will now be evenly
  distributed in the btree with much less contention. You can add an external
  function to the database to make this transformation fairly transparent.

  If you have only one inserting process, then you are better off with
  sequential numbers.

  Of course, this "randomizing" of numbers is only possible if you do not have
  any queries that perform range retrievals based on account numbers.

/albert

--
Albert Godfrind                    Oracle Rdb Engineering
Oracle Corporation                 INET:  agodfrin@fr.oracle.com
DEC European Technical Center             godfrind@heron.enet.dec.com
BP 027                             Voice: [+33] 92.95.51.63
F-06901 Sophia-Antipolis, France   FAX:   [+33] 92.95.50.50



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Generating unique numbers, etc.
  1995-12-11  5:00 Generating unique numbers Ed Havelaar
  1995-12-12  5:00 ` Lilian Hobbs
  1995-12-13  5:00 ` Stephen Hoffman
@ 1995-12-14  5:00 ` Ed Havelaar
  1995-12-14  5:00   ` Norm Lastovica
  1995-12-15  5:00   ` Lilian Hobbs
  2 siblings, 2 replies; 7+ messages in thread
From: Ed Havelaar @ 1995-12-14  5:00 UTC (permalink / raw)


I've had a fair number of responses to my request below, and since not many of 
them seem to have turned up in the newsgroups, I thought I'd post a quick 
summary (and another question).

First my question:

Suppose I have a program that takes ages to run and keeps a read-only 
transaction open for its duration (e.g. to perform calculations on all 100000 
records in a table),  Every now and again, I want to assign an account number.  
This involves starting a read-write transaction for as short a time as 
possible.  How can I do this without terminating my hours-long 
read-only transaction and somehow figuring out where and how to recommence it?

Now my original question, and a bit of a summary.

>
>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.

The general consensus seems to be:
(1) Triggers would work, sort of.  This doesn't really tell me what code to 
put in the trigger though -- I understand triggers are just a way of 
automatically executing some code on (say) an insert operation, so it would be 
handy for ODBC clients.  I got the impression from a number of people that 
triggers are not overly fast, and we're looking at a peak load of about 400 
inserts per hour, so I doubt this is the way to go.

(2) Getting Max(Acc_No)+1 from a table is too slow.

(3) Better to store the next available number in a table of its own, and have 
each application lock and increment it when it needs a new one.  A few 
suggested the "Jerry Leichter" approach of splitting the available numbers 
into blocks with one record governing assignments from each block, and then 
using some random number to figure out which record to lock.  We probably 
don't have enough load to justify bothering with the added complexity.

(4) Nobody seemed to think there was any great advantage in using RMS files.



______________________________________________________________________
Eduard E Havelaar             | The fool says in his heart,
Information Services Section  | "there is no God"...
University of Canterbury      | 
Christchurch, New Zealand     |                        - Psalm 14:1




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Generating unique numbers, etc.
  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
  1 sibling, 0 replies; 7+ messages in thread
From: Lilian Hobbs @ 1995-12-15  5:00 UTC (permalink / raw)


400 inserts an hour is not many. You really shouldn't have a problem
here. Go for my solution of a table with 1 row and a hashed index on
it to retrieve it very quickly and performance should not be a problem
provided you get the account number at the end of the transaction 
before you commit. 

Lilian 

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




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1995-12-15  5:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1995-12-11  5:00 Generating unique numbers Ed Havelaar
1995-12-12  5:00 ` Lilian Hobbs
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

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