pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedHow do FKs work?
7+ messages / 4 participants
[nested] [flat]
* How do FKs work?
@ 2004-10-09 18:08 Marc G. Fournier <scrappy@hub.org>
0 siblings, 1 reply; 7+ messages in thread
From: Marc G. Fournier @ 2004-10-09 18:08 UTC (permalink / raw)
To: pgsql-sql
Got a problem here, and this is a new area for me ... analyzing FKs and
improving their performance :)
Have a table with two FKs on it ... 2 different fields in the table point
to the same field in another table ...
When I do an 'EXPLAIN ANALYZE DELETE FROM table WHERE field = #;', it
never comes back ... or, at lesat, takes a *very* long time ...
If I do a count(1) for that #, there are 1639 rows ...
Now, 'ON DELETE' is set to 'NO ACTION', so my *guess* is that all it does
is a 'SELECT FROM table WHERE field = value' on the referenced table, to
make sure it exists ...
Is this correct? So, its effectively having to do 3278 "SELECTS" against
the REFERENCED table? (two fields have contraints on them, 1639 rows to be
deleted) ... ?
----
Marc G. Fournier Hub.Org Networking Services (http://www.hub.org)
Email: scrappy@hub.org Yahoo!: yscrappy ICQ: 7615664
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: How do FKs work?
@ 2004-10-10 00:23 Tom Lane <tgl@sss.pgh.pa.us>
parent: Marc G. Fournier <scrappy@hub.org>
0 siblings, 2 replies; 7+ messages in thread
From: Tom Lane @ 2004-10-10 00:23 UTC (permalink / raw)
To: Marc G. Fournier <scrappy@hub.org>; +Cc: pgsql-sql
"Marc G. Fournier" <scrappy@hub.org> writes:
> Have a table with two FKs on it ... 2 different fields in the table point
> to the same field in another table ...
> When I do an 'EXPLAIN ANALYZE DELETE FROM table WHERE field = #;', it
> never comes back ... or, at lesat, takes a *very* long time ...
Do you have indexes on the referencing columns? Are they exactly the
same datatype as the referenced column? You can get really awful plans
for the FK-checking queries if not.
regards, tom lane
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: How do FKs work?
@ 2004-10-10 00:43 Marc G. Fournier <scrappy@postgresql.org>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 7+ messages in thread
From: Marc G. Fournier @ 2004-10-10 00:43 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-sql
On Sat, 9 Oct 2004, Tom Lane wrote:
> "Marc G. Fournier" <scrappy@hub.org> writes:
>> Have a table with two FKs on it ... 2 different fields in the table point
>> to the same field in another table ...
>
>> When I do an 'EXPLAIN ANALYZE DELETE FROM table WHERE field = #;', it
>> never comes back ... or, at lesat, takes a *very* long time ...
>
> Do you have indexes on the referencing columns? Are they exactly the
> same datatype as the referenced column? You can get really awful plans
> for the FK-checking queries if not.
Yup, that was my first thought ... running SELECT's joining the two tables
on the FK fields shows indices being used, and fast times ...
----
Marc G. Fournier Hub.Org Networking Services (http://www.hub.org)
Email: scrappy@hub.org Yahoo!: yscrappy ICQ: 7615664
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: How do FKs work?
@ 2004-10-10 00:48 Marc G. Fournier <scrappy@postgresql.org>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 7+ messages in thread
From: Marc G. Fournier @ 2004-10-10 00:48 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-sql
On thing I failed to note here, that is probably critical ... its a 7.3
database ...
On Sat, 9 Oct 2004, Tom Lane wrote:
> "Marc G. Fournier" <scrappy@hub.org> writes:
>> Have a table with two FKs on it ... 2 different fields in the table point
>> to the same field in another table ...
>
>> When I do an 'EXPLAIN ANALYZE DELETE FROM table WHERE field = #;', it
>> never comes back ... or, at lesat, takes a *very* long time ...
>
> Do you have indexes on the referencing columns? Are they exactly the
> same datatype as the referenced column? You can get really awful plans
> for the FK-checking queries if not.
>
> regards, tom lane
>
----
Marc G. Fournier Hub.Org Networking Services (http://www.hub.org)
Email: scrappy@hub.org Yahoo!: yscrappy ICQ: 7615664
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: How do FKs work?
@ 2004-10-10 12:24 Janning Vygen <vygen@gmx.de>
parent: Marc G. Fournier <scrappy@postgresql.org>
0 siblings, 1 reply; 7+ messages in thread
From: Janning Vygen @ 2004-10-10 12:24 UTC (permalink / raw)
To: Marc G. Fournier <scrappy@postgresql.org>; +Cc: pgsql-sql
Am Sonntag, 10. Oktober 2004 02:43 schrieb Marc G. Fournier:
> On Sat, 9 Oct 2004, Tom Lane wrote:
> > "Marc G. Fournier" <scrappy@hub.org> writes:
> >> Have a table with two FKs on it ... 2 different fields in the table
> >> point to the same field in another table ...
> >>
> >> When I do an 'EXPLAIN ANALYZE DELETE FROM table WHERE field = #;', it
> >> never comes back ... or, at lesat, takes a *very* long time ...
> >
> > Do you have indexes on the referencing columns? Are they exactly the
> > same datatype as the referenced column? You can get really awful plans
> > for the FK-checking queries if not.
>
> Yup, that was my first thought ... running SELECT's joining the two tables
> on the FK fields shows indices being used, and fast times ...
Why dont you try EXPLAIN DELETE ... (without ANALYZE). it shows you the plan
without executing it and shows you which index is missing.
janning
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: How do FKs work?
@ 2004-10-10 13:01 Marc G. Fournier <scrappy@postgresql.org>
parent: Janning Vygen <vygen@gmx.de>
0 siblings, 1 reply; 7+ messages in thread
From: Marc G. Fournier @ 2004-10-10 13:01 UTC (permalink / raw)
To: Janning Vygen <vygen@gmx.de>; +Cc: Marc G. Fournier <scrappy@postgresql.org>; pgsql-sql
On Sun, 10 Oct 2004, Janning Vygen wrote:
> Am Sonntag, 10. Oktober 2004 02:43 schrieb Marc G. Fournier:
>> On Sat, 9 Oct 2004, Tom Lane wrote:
>>> "Marc G. Fournier" <scrappy@hub.org> writes:
>>>> Have a table with two FKs on it ... 2 different fields in the table
>>>> point to the same field in another table ...
>>>>
>>>> When I do an 'EXPLAIN ANALYZE DELETE FROM table WHERE field = #;', it
>>>> never comes back ... or, at lesat, takes a *very* long time ...
>>>
>>> Do you have indexes on the referencing columns? Are they exactly the
>>> same datatype as the referenced column? You can get really awful plans
>>> for the FK-checking queries if not.
>>
>> Yup, that was my first thought ... running SELECT's joining the two tables
>> on the FK fields shows indices being used, and fast times ...
>
> Why dont you try EXPLAIN DELETE ... (without ANALYZE). it shows you the plan
> without executing it and shows you which index is missing.
Thought about that too ... shows the index being used on the thable where
the delete is happening, but doesn't show anything as concerns the FKs
themselves ...
----
Marc G. Fournier Hub.Org Networking Services (http://www.hub.org)
Email: scrappy@hub.org Yahoo!: yscrappy ICQ: 7615664
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: How do FKs work?
@ 2004-10-11 11:03 Janning Vygen <vygen@gmx.de>
parent: Marc G. Fournier <scrappy@postgresql.org>
0 siblings, 0 replies; 7+ messages in thread
From: Janning Vygen @ 2004-10-11 11:03 UTC (permalink / raw)
To: Marc G. Fournier <scrappy@postgresql.org>; +Cc: pgsql-sql
Am Sonntag, 10. Oktober 2004 15:01 schrieb Marc G. Fournier:
> On Sun, 10 Oct 2004, Janning Vygen wrote:
> > Am Sonntag, 10. Oktober 2004 02:43 schrieb Marc G. Fournier:
> >> On Sat, 9 Oct 2004, Tom Lane wrote:
> >>> "Marc G. Fournier" <scrappy@hub.org> writes:
> >>>> Have a table with two FKs on it ... 2 different fields in the table
> >>>> point to the same field in another table ...
> >>>>
> >>>> When I do an 'EXPLAIN ANALYZE DELETE FROM table WHERE field = #;', it
> >>>> never comes back ... or, at lesat, takes a *very* long time ...
> >>>
> >>> Do you have indexes on the referencing columns? Are they exactly the
> >>> same datatype as the referenced column? You can get really awful plans
> >>> for the FK-checking queries if not.
> >>
> >> Yup, that was my first thought ... running SELECT's joining the two
> >> tables on the FK fields shows indices being used, and fast times ...
Could you please show me your schema design regarding those two tables. I had
this problem too and it just lacks from an index on the foreign key.
janning
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2004-10-11 11:03 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-10-09 18:08 How do FKs work? Marc G. Fournier <scrappy@hub.org>
2004-10-10 00:23 ` Tom Lane <tgl@sss.pgh.pa.us>
2004-10-10 00:43 ` Marc G. Fournier <scrappy@postgresql.org>
2004-10-10 12:24 ` Janning Vygen <vygen@gmx.de>
2004-10-10 13:01 ` Marc G. Fournier <scrappy@postgresql.org>
2004-10-11 11:03 ` Janning Vygen <vygen@gmx.de>
2004-10-10 00:48 ` Marc G. Fournier <scrappy@postgresql.org>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox