I would like to confirm that the SQL query
SELECT ....
FROM apples,
oranges
LEFT JOIN kiwis ON kiwis.orange_id = oranges.id,
bananas
WHERE ....
is exactly equivalent to other permutations in the FROM subclause, like
SELECT ....
FROM oranges
LEFT JOIN kiwis ON kiwis.orange_id = oranges.id,
bananas,
apples
WHERE ....
or
SELECT ....
FROM bananas,
apples,
oranges
LEFT JOIN kiwis ON kiwis.orange_id = oranges.id
WHERE ....
as long as the explicit LEFT JOIN between oranges and kiwis remains intact. From what I've read in various documents, the returned set should be exactly the same.
I'm really only concerned with the results of the query, not its performance in an actual database. (I'm using PostgreSQL 8.3, which AFAIK doesn't support optimizer hints about the join order, and will try to create an optimal query plan automatically).
It is the same but it is ambiguous as hell with the implicit CROSS JOINs. Use explicit JOINS.
If you are joining in the WHERE clause then the results may differ because joins and filters are mixed up.
SELECT ....
FROM apples a
JOIN
bananas b ON ...
JOIN
oranges o ON ...
LEFT JOIN
kiwis k ON k.orange_id = o.id
WHERE (filters only)
Notes:
The situation is summarized at Controlling the Planner with Explicit JOIN Clauses. Outer joins don't get reordered, inner ones can be. And you can force a particular optimizer order by dropping *join_collapse_limit* before running the query, and just putting things in the order you want them it. That's how you "hint" at the database in this area.
In general, you want to use EXPLAIN to confirm what order you're getting, and that can sometimes be used to visually confirm that two queries are getting the same plan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With