SELECT * FROM foo f
INNER JOIN (
SELECT bar_id, MAX(revision_id) FROM bar b
GROUP BY bar_id
) t ON t.bar_id = f.bar_id
OK, so hears the question: lets say there are in the millions of records in these tables and I want the query to be as efficient as possible.
Is MySQL going to pull all of the records for the bar table and then filter them on the ON statement at the join level instead of inside the sub query? Or is there a way to filter the items inside the sub query with just SQL by itself before the JOIN filter?
It seems querying all of the records to filter them would be inefficient and I have not thought of a way yet to get around this.
I have tried this but the subquery cannot see the foo table:
SELECT * FROM foo f
INNER JOIN (
SELECT bar_id, MAX(revision_id) FROM bar b
WHERE b.bar_id = f.bar_id
GROUP BY bar_id
) t ON t.bar_id = f.bar_id
Is there a way to pass the id down to the subquery, I just like to do things the optimal way, and I'm sure there is a way to do this.
Thanks for all responses.