From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f7344,a08d3b69b39ca00b X-Google-Attributes: gidf7344,public X-Google-Thread: 1164f8,a08d3b69b39ca00b X-Google-Attributes: gid1164f8,public X-Google-Thread: f8dc0,a08d3b69b39ca00b X-Google-Attributes: gidf8dc0,public From: godfrind@heron.enet.dec.com (Albert Godfrind) Subject: Re: Generating unique numbers Date: 1995/12/14 Message-ID: <4apfa5$j5f@sousa.amt.tay1.dec.com>#1/1 X-Deja-AN: 121826551 references: <4aic03$3u1@cantua.canterbury.ac.nz> <4al3k8$f80@zeus.tcp.co.uk> organization: Oracle Corporation newsgroups: comp.databases.rdb,comp.sys.dec,comp.os.vms 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