Re: Data type to use for primary key - Mailing list pgsql-performance

From Josh Berkus
Subject Re: Data type to use for primary key
Date
Msg-id 200411221654.56673.josh@agliodbs.com
Whole thread Raw
In response to Data type to use for primary key  (Alexandre Leclerc <alexandre.leclerc@gmail.com>)
Responses Re: Data type to use for primary key  (Rod Taylor <pg@rbt.ca>)
Re: Data type to use for primary key  (Alexandre Leclerc <alexandre.leclerc@gmail.com>)
List 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

pgsql-performance by date:

Previous
From: Alexandre Leclerc
Date:
Subject: Re: Data type to use for primary key
Next
From: Rod Taylor
Date:
Subject: Re: Data type to use for primary key