How do FKs work?
am 09.10.2004 20:08:51 von scrappy
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
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faqs/FAQ.html
Re: How do FKs work?
am 10.10.2004 02:23:53 von tgl
"Marc G. Fournier" 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
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
Re: How do FKs work?
am 10.10.2004 02:43:50 von scrappy
On Sat, 9 Oct 2004, Tom Lane wrote:
> "Marc G. Fournier" 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
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?
http://archives.postgresql.org
Re: How do FKs work?
am 10.10.2004 02:48:43 von scrappy
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" 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
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faqs/FAQ.html
Re: How do FKs work?
am 10.10.2004 14:24:32 von vygen
Am Sonntag, 10. Oktober 2004 02:43 schrieb Marc G. Fournier:
> On Sat, 9 Oct 2004, Tom Lane wrote:
> > "Marc G. Fournier" 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
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faqs/FAQ.html
Re: How do FKs work?
am 10.10.2004 15:01:17 von scrappy
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" 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
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match
Re: How do FKs work?
am 11.10.2004 13:03:37 von vygen
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" 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
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster