Re: Data type to use for primary key

Поиск
Список
Период
Сортировка
От Josh Berkus
Тема Re: Data type to use for primary key
Дата
Msg-id 200411221654.56673.josh@agliodbs.com
обсуждение исходный текст
Ответ на Data type to use for primary key  (Alexandre Leclerc <alexandre.leclerc@gmail.com>)
Ответы 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>)
Список 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 по дате отправления:

Предыдущее
От: Alexandre Leclerc
Дата:
Сообщение: Re: Data type to use for primary key
Следующее
От: Rod Taylor
Дата:
Сообщение: Re: Data type to use for primary key