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: f8dc0,a08d3b69b39ca00b X-Google-Attributes: gidf8dc0,public X-Google-Thread: 1164f8,a08d3b69b39ca00b X-Google-Attributes: gid1164f8,public From: lmhobbs@tcp.co.uk (Lilian Hobbs) Subject: Re: Generating unique numbers Date: 1995/12/12 Message-ID: <4al3k8$f80@zeus.tcp.co.uk>#1/1 X-Deja-AN: 121564475 references: <4aic03$3u1@cantua.canterbury.ac.nz> organization: Total Connectivity Providers - Internet access for the UK mime-version: 1.0 newsgroups: comp.databases.rdb,comp.sys.dec,comp.os.vms 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 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' =================================================================