Re: Data type to use for primary key

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: pgsql-performance(at)postgresql(dot)org, Alexandre Leclerc <alexandre(dot)leclerc(at)gmail(dot)com>
Subject: Re: Data type to use for primary key
Date: 2004-11-23 00:54:56
Message-ID: 200411221654.56673.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

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

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Rod Taylor 2004-11-23 01:03:11 Re: Data type to use for primary key
Previous Message Alexandre Leclerc 2004-11-22 23:26:00 Re: Data type to use for primary key