Re: Deleting vs foreign keys

From: Csaba Nagy <nagy(at)ecircle-ag(dot)com>
To: WireSpot <wirespot(at)gmail(dot)com>
Cc: Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Deleting vs foreign keys
Date: 2005-10-25 09:33:15
Message-ID: 1130232795.27587.277.camel@coppola.muc.ecircle.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 2005-10-25 at 11:18, WireSpot wrote:
> On 10/25/05, Michael Fuhr <mike(at)fuhr(dot)org> wrote:
> > Are there indexes on the foreign key columns? That is, given
> > the following example,
> >
> > CREATE TABLE foo (id integer PRIMARY KEY);
> > CREATE TABLE bar (fooid integer NOT NULL REFERENCES foo ON DELETE CASCADE);
> >
> > do you have an index on bar.fooid?
>
> There are no indexes on the referring fields in any tables. Would this
> make a huge difference?

Yes it will. Think about it: when the delete cascades, the rows which
have to be deleted in the referring table have to be found first... if
there's no index, a full table scan will be done for that. An index will
allow the delete process to quickly find the referring rows which have
to be deleted too. So it's a good practice to create indexes on fields
referring to parent tables.

Cheers,
Csaba.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message WireSpot 2005-10-25 09:45:18 Re: Deleting vs foreign keys
Previous Message Martijn van Oosterhout 2005-10-25 09:25:38 Re: Deleting vs foreign keys