Re: Any optimizations to the join code in 7.1? - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Any optimizations to the join code in 7.1?
Date
Msg-id 6243.988234332@sss.pgh.pa.us
Whole thread Raw
In response to Re: Any optimizations to the join code in 7.1?  (Joel Burton <jburton@scw.org>)
List pgsql-hackers
Joel Burton <jburton@scw.org> writes:
> In other DB systems I've used, some find that for this original query:
>   SELECT * FROM a, b WHERE a.id=b.id AND b.name = 'foo';
> that this version 
>   SELECT * FROM a JOIN b USING (id) WHERE b.name = 'foo';
> has slower performance than
>   SELECT * FROM b JOIN a USING (id) WHERE b.name = 'foo';
> because it can reduce b before any join. 

> Is it safe to assume that this is a valid optimization in PostgreSQL?

In general, that'd be a waste of time --- our planner considers the same
set of plans in either case.

However, it could make a difference if the planner thinks that the two
choices (a outer or b outer) have exactly the same cost.  In that case
the order you wrote them in will influence which plan actually gets
picked; and if the planner's estimate is wrong --- ie, there really is a
considerable difference in the costs --- then you could see a change in
performance depending on which way you wrote it.  That's a pretty
unusual circumstance, maybe, but it just happens that I'm in the middle
of looking at a planning bug wherein exactly this behavior occurs...

> If this whole thing were a view, except w/o the WHERE clause, and we were
> querying the view w/the b.name WHERE clause, would we still see a
> performance boost from the right arrangement? (ie, does our criteria get
> pushed down early enough in the joining process?)

Shouldn't make a difference; AFAIK the WHERE clause will get pushed down
as far as possible, independently of whether a view is involved or you
wrote it out the hard way.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: refusing connections based on load ...
Next
From: The Hermit Hacker
Date:
Subject: Re: refusing connections based on load ...