dbTalk Databases Forums  

How do FKs work?

comp.databases.postgresql.sql comp.databases.postgresql.sql


Discuss How do FKs work? in the comp.databases.postgresql.sql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Marc G. Fournier
 
Posts: n/a

Default How do FKs work? - 10-09-2004 , 01:08 PM







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 (AT) hub (DOT) org Yahoo!: yscrappy ICQ: 7615664

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: How do FKs work? - 10-09-2004 , 07:23 PM






"Marc G. Fournier" <scrappy (AT) hub (DOT) org> writes:
Quote:
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 (AT) postgresql (DOT) org)



Reply With Quote
  #3  
Old   
Marc G. Fournier
 
Posts: n/a

Default Re: How do FKs work? - 10-09-2004 , 07:43 PM



On Sat, 9 Oct 2004, Tom Lane wrote:

Quote:
"Marc G. Fournier" <scrappy (AT) hub (DOT) 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 (AT) hub (DOT) org Yahoo!: yscrappy ICQ: 7615664

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
  #4  
Old   
Marc G. Fournier
 
Posts: n/a

Default Re: How do FKs work? - 10-09-2004 , 07:48 PM




On thing I failed to note here, that is probably critical ... its a 7.3
database ...

On Sat, 9 Oct 2004, Tom Lane wrote:

Quote:
"Marc G. Fournier" <scrappy (AT) hub (DOT) 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 (AT) hub (DOT) org Yahoo!: yscrappy ICQ: 7615664

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
  #5  
Old   
Janning Vygen
 
Posts: n/a

Default Re: How do FKs work? - 10-10-2004 , 07:24 AM



Am Sonntag, 10. Oktober 2004 02:43 schrieb Marc G. Fournier:
Quote:
On Sat, 9 Oct 2004, Tom Lane wrote:
"Marc G. Fournier" <scrappy (AT) hub (DOT) 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


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
  #6  
Old   
Marc G. Fournier
 
Posts: n/a

Default Re: How do FKs work? - 10-10-2004 , 08:01 AM



On Sun, 10 Oct 2004, Janning Vygen wrote:

Quote:
Am Sonntag, 10. Oktober 2004 02:43 schrieb Marc G. Fournier:
On Sat, 9 Oct 2004, Tom Lane wrote:
"Marc G. Fournier" <scrappy (AT) hub (DOT) 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 (AT) hub (DOT) 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



Reply With Quote
  #7  
Old   
Janning Vygen
 
Posts: n/a

Default Re: How do FKs work? - 10-11-2004 , 06:03 AM



Am Sonntag, 10. Oktober 2004 15:01 schrieb Marc G. Fournier:
Quote:
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 (AT) hub (DOT) 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

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.