In many cases, the expression of a table in the FROM clause can be simplified.
During the analysis phase, queries with right outer join operations are converted to equivalent queries that only contain left join operations. In general, the conversion is based on the following principles:
(T1,...) right join (T2,...) on p (T1,..., T2,...) =
(T2,...) left join (T1,...) on p (T1,..., T2 ,...)
All the inner join expressions in the form of T1 inner join T2 on p (T1, T2) are replaced with T1, T2, P (T1, T2 ), join a connection based on the WHERE condition (or nested connection condition, if any.
When the optimizer evaluates the join query scheme for external join operations, it only considers the scheme of accessing the external table before accessing the internal table. The Optimizer option is limited because only such a solution allows us to use the nested loop mechanism to perform queries for out-of-band join operations.
Suppose we have a query in the following form:
SELECT * T1 left join T2 ON P1 (T1, T2)
Where p (T1, T2) and r (T2)
R (T2) greatly reduces the number of matched rows in table T2. If we execute the query in this way, the optimizer will not have any other options. We can only access table T1 before table T2, resulting in a very low execution plan.
Fortunately, if the WHERE condition rejects null, MySQL can convert this type of query to a query without any outer join operation. If the row supplemented by NULL constructed for this operation is evaluated as FALSE or UNKNOWN, this condition is called rejecting null for an outer join operation.
Therefore, for this outer join:
T1 left join T2 ON T1.A = T2.A
Similar to the following condition:
T2. B IS NOT NULL,
T2. B> 3,
T2.C <= T1.C,
T2. B <2 OR T2.C> 1
Similar to the following condition, null is not denied:
T2. B IS NULL,
T1. B <3 OR T2. B IS NOT NULL,
T1. B <3 OR T2. B> 3
It is easy to check whether a condition for an outer join operation rejects null. The condition for rejecting null is:
· The form is a is not null, where a is an attribute of any internal table
· Contains the correlated formula referenced by the internal table. When a parameter is NULL, it is evaluated as UNKNOWN.
· Include the Union of conditions used to reject null for connection
· Reject the logic and
One condition can deny null for one outer join operation in a query, but does not deny null for the other. In the following query:
SELECT * FROM T1 left join T2 ON T2.A = T1.A
Left join T3 ON T3. B = T1. B
WHERE T3.C> 0
The WHERE condition rejects null for 2nd outer join operations, but does not reject null for 1st outer join operations.
If the WHERE condition rejects null for an outer join operation in a query, the outer join operation is replaced by an inner join operation.
For example, the preceding query is replaced by the following query:
SELECT * FROM T1 left join T2 ON T2.A = T1.A
Inner join T3 ON T3. B = T1. B
WHERE T3.C> 0
For the original query, the optimizer will evaluate a solution that is only compatible with one access sequence T1, T2, and T3. The access sequence T3, T1, and T2.
Conversion of an external join operation can trigger another conversion. In this way, query:
SELECT * FROM T1 left join T2 ON T2.A = T1.A
Left join T3 ON T3. B = T2. B
WHERE T3.C> 0
Convert to query first:
SELECT * FROM T1 left join T2 ON T2.A = T1.A
Inner join T3 ON T3. B = T2. B
WHERE T3.C> 0
This query is equivalent to the query:
SELECT * FROM (T1 left join T2 ON T2.A = T1.A), T3
WHERE T3.C> 0 AND T3. B = T2. B
Now the remaining outer join operations can be replaced by an inner join, because the condition T3. B = T2. B rejects null, we can get a query with no outer join at all:
SELECT * FROM (T1 inner join T2 ON T2.A = T1.A), T3
WHERE T3.C> 0 AND T3. B = T2. B
Sometimes we can successfully replace the embedded outer join operation, but cannot convert the embedded outer join. The following query:
SELECT * FROM T1 LEFT JOIN
(T2 left join T3 ON T3. B = T2. B)
ON T2.A = T1.A
WHERE T3.C> 0
Converted:
SELECT * FROM T1 LEFT JOIN
(T2 inner join T3 ON T3. B = T2. B)
ON T2.A = T1.A
WHERE T3.C> 0,
It can only be rewritten as a form that still contains the embedded outer join operation:
SELECT * FROM T1 LEFT JOIN
(T2, T3)
ON (T2.A = T1.A AND T3. B = T2. B)
WHERE T3.C> 0.
To convert an embedded outer join operation in a query, we must consider the join conditions and WHERE conditions of the embedded outer join. In the following query:
SELECT * FROM T1 LEFT JOIN
(T2 left join T3 ON T3. B = T2. B)
ON T2.A = T1.A AND T3.C = T1.C
WHERE T3.D> 0 OR T1.D> 0
The WHERE condition does not deny null for the embedded outer join, but the join condition of the embedded outer join T2.A = T1.A AND T3.C = T1.C is deny null. Therefore, the query can be converted:
SELECT * FROM T1 LEFT JOIN
(T2, T3)
ON T2.A = T1.A AND T3.C = T1.C AND T3. B = T2. B
WHERE T3.D> 0 OR T1.D> 0
7.2.12. How does MySQL optimize order?