pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: pgsql-performance@postgresql.org, Alexandre Leclerc <alexandre.leclerc@gmail.com>
Subject: Re: Data type to use for primary key
Date: Mon, 22 Nov 2004 16:54:56 -0800
Message-ID: <200411221654.56673.josh@agliodbs.com> (raw)
In-Reply-To: <1dc7f0e30411221232603c4617@mail.gmail.com>
References: <1dc7f0e30411221232603c4617@mail.gmail.com>

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



view thread (14+ messages)  latest in thread

Message-ID: <200411221654.56673.josh@agliodbs.com>
Permalink:  ../200411221654.56673.josh@agliodbs.com/
Also on:    postgresql.org/message-id/200411221654.56673.josh@agliodbs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: josh@agliodbs.com, alexandre.leclerc@gmail.com
  Subject: Re: Data type to use for primary key
  In-Reply-To: <200411221654.56673.josh@agliodbs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox