Re: indexing primary and foreign keys w/lookup table

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Neal Clark <nclark(at)securescience(dot)net>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: indexing primary and foreign keys w/lookup table
Date: 2007-01-25 06:26:26
Message-ID: 20070125062626.GA1251@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jan 24, 2007 at 20:14:07 -0800,
Neal Clark <nclark(at)securescience(dot)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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruno Wolff III 2007-01-25 06:31:44 Re: Converting 7.x to 8.x
Previous Message Tom Lane 2007-01-25 05:53:51 Re: Example of RETURNING clause to get auto-generated keys