For simple queries like the one in your example, the query optimizer will be able to resolve either query with the same execution plan:
If you use LEFT JOIN
to find rows that do not exist in some table and you have the following test: col_name IS NULL
in the WHERE
part, where col_name
is a column that is declared as NOT NULL
, MySQL stops searching for more rows (for a particular key combination) after it has found one row that matches the LEFT JOIN
condition.
In most cases, the optimizer is even able to use the smart trick that Gordon suggested previously:
Consider the following subquery comparison:
outer_expr IN (SELECT inner_expr FROM ... WHERE subquery_where)
[Provided that] an appropriate equality [is pushed down] into the subquery's WHERE
clause (...) the comparison is converted to this:
EXISTS (SELECT 1 FROM ... WHERE subquery_where AND outer_expr=inner_expr)
My point is, I would recommend using the for you are most used to, and only try other options if you ever encountered an actual performance issue here.