Re: indexing primary and foreign keys w/lookup table

From: Chris <dmagick(at)gmail(dot)com>
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 04:40:57
Message-ID: 45B834D9.9050804@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> 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);
>
> do I need any/all of these indexes for my lookup table to work well? I
> am thinking I can get rid of stuff_id and accounts_id. Thoughts?

You should have indexes on the fields used in joins.

So if you join stuff_by_account to accounts using account_id, make sure
there is an index on both sides of that (primary key already has one but
the lookup table needs one too).

Foreign keys need them because when a row gets added/removed, the index
is used to quickly make sure the data is in the external table.

--
Postgresql & php tutorials
http://www.designmagick.com/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris 2007-01-25 04:43:19 Re: Converting 7.x to 8.x
Previous Message Neal Clark 2007-01-25 04:14:07 indexing primary and foreign keys w/lookup table