X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 51EF89FA0EC for ; Thu, 6 Jul 2006 18:50:13 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 82183-06 for ; Thu, 6 Jul 2006 18:50:09 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from exchange.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by postgresql.org (Postfix) with ESMTP id 574EA9F94F3 for ; Thu, 6 Jul 2006 18:50:08 -0300 (ADT) Received: from 10.10.1.37 ([10.10.1.37]) by exchange.g2switchworks.com ([10.10.1.2]) with Microsoft Exchange Server HTTP-DAV ; Thu, 6 Jul 2006 21:50:07 +0000 Received: from state.g2switchworks.com by mail.g2switchworks.com; 06 Jul 2006 16:50:07 -0500 Subject: Re: Alternative to serial primary key From: Scott Marlowe To: Aaron Bono Cc: David Clarke , "operationsengineer1@yahoo.com" , pgsql-sql@postgresql.org In-Reply-To: References: <20060706183920.46008.qmail@web33314.mail.mud.yahoo.com> <12b7ac1e0607061330o34adbd6ds9bd624d3889c7fe7@mail.gmail.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1152222607.13851.280.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Thu, 06 Jul 2006 16:50:07 -0500 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/74 X-Sequence-Number: 25648 On Thu, 2006-07-06 at 16:43, Aaron Bono wrote: > On 7/6/06, David Clarke wrote: > To recap, yes there is only a single column, yes it is > varchar. I need > to do a lookup on the address column which is unique and use > it as a > foreign key in other tables. Using a serial id would obviously > work > and has been recommended. But having a hash function over the > address > column as the primary key means I can always regenerate my > primary key > from the data which is impossible with a serial key. I believe > the > risk of collision using md5 is effectively zero on this data > and I can > put a unique index over it. I'll repeat my previous statement that this is premature optimization, and the hash is kind the wrong direction. If you store an int and the 1 to 100 characters in a varchar, you'll have about 4 to 8 bytes for the int (6 I think, but it's been a while) plus 1 to 200 or possibly more for the characters in the address. If you use C local with ASCII encoding, you can get single byte. If you switch to an md5 hash, you'll need ~50 bytes (average address about 1/2 max length, just a guess) plus 32 bytes, plus the extra bytes to keep track of the length of the fields. The table now becomes wider itself, and the md5 is generally about as big as the address, or fairly close to it. And you've got the possibility of md5 collisions to deal with. I'd say just FK off of the address field. It's a natural key, fairly small (100 bytes ain't really that big) and your primary key never needs any kind of regenerating or anything, because it's already there. Just set it up with cascading updates and deletes in case you need to edit it in the future. The first rule of optimization: Don't