Re: CROSS JOIN performance

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andy Chambers <achambers(at)mcna(dot)net>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: CROSS JOIN performance
Date: 2012-02-21 15:29:32
Message-ID: 15109.1329838172@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andy Chambers <achambers(at)mcna(dot)net> writes:
> In our porting of a big mysql app to postgres, we're finding lots of
> queries like

> select foo
> from (foo f, bar b)
> left join caz c on f.id = f.caz_id
> where f.id = b.foo_id

> I've seen the message where Tom explains why this is invalid in ANSI
> SQL so I converted it to

> select foo
> from foo f CROSS JOIN bar b
> left join caz c on f.id = f.caz_id
> where f.id = b.foo_id

> ...and it works. However, sometimes quite slowly. When we've looked
> into the slow ones, we've found that changing it again to

> select foo
> from foo f INNER JOIN bar b ON f.id = b.foo_id
> left join caz c on f.id = f.caz_id

> makes it perform much better.

Those formulations look equivalent to me. Could you provide a concrete
test case, or at least some specific queries and their EXPLAIN ANALYZE
results? Which PG version are we talking about? Oh, and are the LEFT
JOIN conditions really not constraining table c at all?

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2012-02-21 15:48:31 Re: How to store variable data in a field?
Previous Message Andre Lopes 2012-02-21 15:11:08 How to store variable data in a field?