Re: left outer join terrible slow compared to inner join - Mailing list pgsql-general

From Tom Lane
Subject Re: left outer join terrible slow compared to inner join
Date
Msg-id 21691.1062085320@sss.pgh.pa.us
Whole thread Raw
In response to left outer join terrible slow compared to inner join  (Thomas Beutin <tyrone@laokoon.IN-Berlin.DE>)
Responses Re: left outer join terrible slow compared to inner join  (Thomas Beutin <tyrone@laokoon.IN-Berlin.DE>)
List pgsql-general
Thomas Beutin <tyrone@laokoon.IN-Berlin.DE> writes:
> i've a speed problem withe the following statement:
> SELECT DISTINCT pz.l1_id, pz.l2_id, pz.l3_id, pz.l4_id
> FROM ot_adresse AS a, ot_produkt AS p
> LEFT OUTER JOIN  ot_kat_prod AS pz ON ( p.p_id = pz.p_id )
> WHERE  p.a_id = a.id AND a.id = '105391105424941' AND a.m_id = '37';

In 7.2 (and 7.3), this syntax forces the planner to join ot_produkt to
ot_kat_prod first, which is terribly inefficient because the WHERE
constraints don't constrain that join at all.  You could work around
this by writing instead

 FROM (ot_adresse AS a CROSS JOIN ot_produkt AS p)
 LEFT OUTER JOIN  ot_kat_prod AS pz ON ( p.p_id = pz.p_id )
 WHERE  p.a_id = a.id AND a.id = '105391105424941' AND a.m_id = '37';

See http://www.postgresql.org/docs/7.3/static/explicit-joins.html
for discussion.  (Note: 7.4 will be less rigid about this issue.)

            regards, tom lane

pgsql-general by date:

Previous
From: "Shridhar Daithankar"
Date:
Subject: Re: Problems with transactions and sequences
Next
From: Mike Mascari
Date:
Subject: Re: Functions have 32 args limt ???