LEFT JOIN
and RIGHT JOIN
Optimization
MySQL implements an
as follows:
A
LEFT JOIN B
join_condition
- Table
B
is set to depend on tableA
and all tables on whichA
depends. - Table
A
is set to depend on all tables (exceptB
) that are used in theLEFT JOIN
condition. - The
LEFT JOIN
condition is used to decide how to retrieve rows from tableB
. (In other words, any condition in theWHERE
clause is not used.) - All standard join optimizations are performed, with the exception that a table is always read after all tables on which it depends. If there is a circular dependence, MariaDB issues an error.
- All standard
WHERE
optimizations are performed. - If there is a row in
A
that matches theWHERE
clause, but there is no row inB
that matches theON
condition, an extraB
row is generated with all columns set toNULL
. - If you use
LEFT JOIN
to find rows that do not exist in some table and you have the following test:
in thecol_name
IS NULLWHERE
part, wherecol_name
is a column that is declared asNOT NULL
, MariaDB stops searching for more rows (for a particular key combination) after it has found one row that matches theLEFT JOIN
condition.
The implementation of RIGHT JOIN
is analogous to that of LEFT JOIN
with the roles of the tables reversed.
The join optimizer calculates the order in which tables should be joined. The table read order forced by LEFT JOIN
or STRAIGHT_JOIN
helps the join optimizer do its work much more quickly, because there are fewer table permutations to check. Note that this means that if you do a query of the following type, MariaDB does a full scan on b
because the LEFT JOIN
forces it to be read before d
:
SELECT * FROM a JOIN b LEFT JOIN c ON (c.key=a.key) LEFT JOIN d ON (d.key=a.key) WHERE b.key=d.key;
The fix in this case is reverse the order in which a
and b
are listed in the FROM
clause:
SELECT * FROM b JOIN a LEFT JOIN c ON (c.key=a.key) LEFT JOIN d ON (d.key=a.key) WHERE b.key=d.key;
For a LEFT JOIN
, if the WHERE
condition is always false for the generated NULL
row, the LEFT JOIN
is changed to a normal join. For example, the WHERE
clause would be false in the following query if t2.column1
were NULL
:
SELECT * FROM t1 LEFT JOIN t2 ON (column1) WHERE t2.column2=5;
Therefore, it is safe to convert the query to a normal join:
SELECT * FROM t1, t2 WHERE t2.column2=5 AND t1.column1=t2.column1;
This can be made faster because MariaDB can use table t2
before table t1
if doing so would result in a better query plan. To provide a hint about the table join order, use STRAIGHT_JOIN
. (See , "SELECT
Syntax".)