Re: left outer join terrible slow compared to inner join

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Thomas Beutin <tyrone(at)laokoon(dot)IN-Berlin(dot)DE>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: left outer join terrible slow compared to inner join
Date: 2003-08-28 15:42:00
Message-ID: 21691.1062085320@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thomas Beutin <tyrone(at)laokoon(dot)IN-Berlin(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mike Mascari 2003-08-28 15:43:16 Re: Functions have 32 args limt ???
Previous Message Shridhar Daithankar 2003-08-28 15:40:19 Re: Problems with transactions and sequences