For an INNER JOIN
, there's no conceptual difference between putting a condition in ON
and in WHERE
. It's a common practice to use ON
for conditions that connect a key in one table to a foreign key in another table, such as your event_id
, so that other people maintaining your code can see how the tables relate.
If you suspect that your database engine is mis-optimizing a query plan, you can try it both ways. Make sure to time the query several times to isolate the effect of caching, and make sure to run ANALYZE TABLE occurs
and ANALYZE TABLE events
to provide more info to the optimizer about the distribution of keys. If you do find a difference, have the database engine EXPLAIN
the query plans it generates. If there's a gross mis-optimization, you can create an Oracle account and file a feature request against MySQL to optimize a particular query better.
But for a LEFT JOIN
, there's a big difference. A LEFT JOIN
is often used to add details from a separate table if the details exist or return the rows without details if they do not. This query will return result rows with NULL
values for b.*
if no row of b
matches both conditions:
SELECT a.*, b.*
FROM a
LEFT JOIN b
ON (condition_one
AND condition_two)
WHERE condition_three
Whereas this one will completely omit results that do not match condition_two
:
SELECT a.*, b.*
FROM a
LEFT JOIN b ON some_condition
WHERE condition_two
AND condition_three
Code in this answer is dual licensed: CC BY-SA 3.0 or the MIT License as published by OSI.