Re: Deleting vs foreign keys - Mailing list pgsql-general

From Martijn van Oosterhout
Subject Re: Deleting vs foreign keys
Date
Msg-id 20051025092537.GF22318@svana.org
Whole thread Raw
In response to Re: Deleting vs foreign keys  (WireSpot <wirespot@gmail.com>)
Responses Re: Deleting vs foreign keys  (WireSpot <wirespot@gmail.com>)
List pgsql-general
On Tue, Oct 25, 2005 at 12:18:34PM +0300, WireSpot wrote:
> On 10/25/05, Michael Fuhr <mike@fuhr.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?

If bar is at all large, yes. Consider if you delete something from foo.
The system has to then scan bar to find all matching fooids. If there
is no index on that column it will do a seq scan. If you delete
everything from foo, it will (sequentially) scan bar once for every row
in foo...

BTW, have you considered TRUNCATE? (although it may not work with
foreign keys).

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Attachment

pgsql-general by date:

Previous
From: Oliver Elphick
Date:
Subject: Re: newbie question: reading sql commands from script
Next
From: Csaba Nagy
Date:
Subject: Re: Deleting vs foreign keys