X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id D63B23A2B10 for ; Tue, 23 Nov 2004 00:52:23 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 44210-09 for ; Tue, 23 Nov 2004 00:52:11 +0000 (GMT) Received: from davinci.ethosmedia.com (server226.ethosmedia.com [209.128.84.226]) by svr1.postgresql.org (Postfix) with ESMTP id 8D4C53A1D8C for ; Tue, 23 Nov 2004 00:52:13 +0000 (GMT) Received: from [64.81.245.111] (account josh@agliodbs.com HELO temoku.sf.agliodbs.com) by davinci.ethosmedia.com (CommuniGate Pro SMTP 4.1.8) with ESMTP id 6698260; Mon, 22 Nov 2004 16:53:44 -0800 From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: pgsql-performance@postgresql.org, Alexandre Leclerc Subject: Re: Data type to use for primary key Date: Mon, 22 Nov 2004 16:54:56 -0800 User-Agent: KMail/1.6.2 References: <1dc7f0e30411221232603c4617@mail.gmail.com> In-Reply-To: <1dc7f0e30411221232603c4617@mail.gmail.com> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200411221654.56673.josh@agliodbs.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/343 X-Sequence-Number: 9297 Alexandre, > What is the common approach? Should I use directly the product_code as > my ID, or use a sequantial number for speed? (I did the same for the > company_id, this is a 'serial' and not the shor name of the customer. > I just don't know what is usually done. Don't use SERIAL just because it's there. Ideally, you *want* to use the product_code if you can. It's your natural key and a natural key is always superior to a surrogate key all other things being equal. Unfortunately, all other things are NOT equal. Here's the reasons why you'd use a surrogate key (i.e. SERIAL): 1) because the product code is a large text string (i.e. > 10bytes) and you will have many millions of records, so having it as an FK in other tables will add significantly to the footprint of the database; 2) because product codes get blanket changes frequently, where thousands of them pet re-mapped to new codes, and the ON CASCADE UPDATE slow performance will kill your database; 3) Because every other table in the database has a SERIAL key and consistency reduces errors; 4) or because your interface programmers get annoyed with using different types of keys for different tables and multicolumn keys. If none of the above is true (and I've had it not be, in some tables and some databases) then you want to stick with your "natural key", the product_code. -- --Josh Josh Berkus Aglio Database Solutions San Francisco