Re: indexing primary and foreign keys w/lookup table

Поиск
Список
Период
Сортировка
От Bruno Wolff III
Тема Re: indexing primary and foreign keys w/lookup table
Дата
Msg-id 20070125062626.GA1251@wolff.to
обсуждение исходный текст
Ответ на indexing primary and foreign keys w/lookup table  (Neal Clark <nclark@securescience.net>)
Список pgsql-general
On Wed, Jan 24, 2007 at 20:14:07 -0800,
  Neal Clark <nclark@securescience.net> wrote:
> I was wondering...I currently have indexes on the primary key id and
> foreign key id's for tables that resemble the following. Is this a
> good idea/when would it benefit me? I don't want waste a lot of
> unnecessary space on indexes.

Not exactly. Primary keys already result in an index being created to enforce
uniqueness, so the manually created indexes are redundant.

> CREATE TABLE stuff_by_account (
>     account_id    BIGINT REFERENCES accounts(id),
>     stuff_id    BIGINT REFERENCES stuff(id)
> );
> CREATE INDEX stuff_by_account_account_id ON stuff_by_account
> (account_id);
> CREATE INDEX stuff_by_account_stuff_id ON stuff_by_account(stuff_id);

For this last case, you most likely want to declare either account_id, stuff_id
or stuff_id, account_id as a primary key. You may want to create an index
just on the second column of the primary key, depending on your usage pattern.
You almost certainly wouldn't want to create an index on the first column
of the primary key.

В списке pgsql-general по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Example of RETURNING clause to get auto-generated keys
Следующее
От: Bruno Wolff III
Дата:
Сообщение: Re: Converting 7.x to 8.x