From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | rihad <rihad(at)mail(dot)ru> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: FK index q'n |
Date: | 2007-11-30 15:48:07 |
Message-ID: | 2368.1196437687@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
rihad <rihad(at)mail(dot)ru> writes:
> Given this table:
> CREATE TABLE foo (
> id integer primary key,
> bar_id integer references bar (id)
> );
> and provided that bar.id is itself a PK, do I still need to create an
> index on bar_id if often doing queries like:
> SELECT MIN(id) FROM foo WHERE bar_id IS NULL;
The FK relationship as such is only a reason to create an index if you
frequently do updates or deletes in table bar. For such operations, the
database has to check if there are any matching rows in foo, and an
index on foo.bar_id makes that go faster.
Now as far as the above-illustrated query goes, no simple index is going
to help it, because IS NULL is not an indexable operation. If you are
really concerned about queries of that specific form, you could make a
partial index
create index fooi on foo (id) where bar_id is null;
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Paul Boddie | 2007-11-30 15:53:20 | Re: PostgresSQL vs Ingress |
Previous Message | Ivan Sergio Borgonovo | 2007-11-30 15:38:00 | Re: PostgresSQL vs Ingress |