From: godfrind@heron.enet.dec.com (Albert Godfrind)
Subject: Re: Generating unique numbers
Date: 1995/12/14 [thread overview]
Message-ID: <4apfa5$j5f@sousa.amt.tay1.dec.com> (raw)
In-Reply-To: 4al3k8$f80@zeus.tcp.co.uk
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
next prev parent reply other threads:[~1995-12-14 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
1995-12-14 5:00 ` Albert Godfrind [this message]
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