Thread: How do FKs work?

How do FKs work?

From
"Marc G. Fournier"
Date:
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


Re: How do FKs work?

From
Tom Lane
Date:
"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


Re: How do FKs work?

From
"Marc G. Fournier"
Date:
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


Re: How do FKs work?

From
"Marc G. Fournier"
Date:
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


Re: How do FKs work?

From
Janning Vygen
Date:
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



Re: How do FKs work?

From
"Marc G. Fournier"
Date:
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


Re: How do FKs work?

From
Janning Vygen
Date:
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