Forum OpenACS Q&A: Response to Faster Selects with study of EXPLAIN

Collapse
Posted by Don Baccus on
No, I do *not* recommend using the explicit JOIN syntax as a general rule.  For one thing, as Tom Lane's note indicates, the optimizer doesn't fully optimize complex JOIN expressions in the FROM list.  For another the syntax isn't supported by Oracle 8i and we want to maintain portable queries as much as possible, so gratuitous differences are a no-no.

IMO the optimization issue is a bug but thus far the PG folk seem to take the point of view that it's a feature.  "You can constrain the optimizer by writing explicit JOINs or get full optimization by writing implicit JOINs!"  Hogwash.  In an ideal world all JOINs would be written in the FROM list, as this is the preferred SQL92 standard way to do it, but if the optimizer's going to punt on them then it isn't a good idea.  The reason the optimizer punts is because outer JOINs aren't associative, and the code doesn't differentiate between outer and inner JOINs when optimizing (i.e. it treats inner JOINs as though they're not associative).  The PG folks are used to the implicit join syntax and thus seem to think the only reason folks might use it is in order to take "advantage" of the fact that there's only limited optimization done on the explicit joins.

So ... the bottom line is that inner JOINs written in the FROM list are only optimized in a limited way.  Given that the best join order will vary depending on the size of various tables, in general it isn't possible to write queries to explicitly express the "best" way to JOIN, because the "best" will vary from installation to installation.  An installation with lots of users and few groups, for instance, may perform better with joins against parties and relationships done in a different order than an installation with lots of groups relative to the number of users.