0
点赞
收藏
分享

微信扫一扫

Outer Join Optimization

滚过红尘说红尘 2022-02-18 阅读 61
mysqlsedide



Outer joins include LEFT JOIN and RIGHT JOIN.
外部连接包括左连接和右连接。
MySQL implements an A LEFT JOIN B join_specification as follows:
MySQL实现了A LEFT JOIN B JOIN 规范,如下所示:



Table B is set to depend on table A and all tables on which A depends.
表B被设置为依赖于表A和A所依赖的所有表。



Table A is set to depend on all tables (except B) that are used in the LEFT JOIN condition.
表A被设置为依赖于左联接条件中使用的所有表(B除外)。



The LEFT JOIN condition is used to decide how to retrieve rows from table B. (In other words, any condition in the WHERE clause is not used.)
LEFT JOIN条件用于决定如何从表B中检索行。(换句话说,不使用WHERE子句中的任何条件。)



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 dependency, an error occurs.
执行所有标准的连接优化,不同之处在于始终在表所依赖的所有表之后读取该表。 如果存在循环依赖关系,则会发生错误。



All standard WHERE optimizations are performed.
执行优化的所有标准。



If there is a row in A that matches the WHERE clause, but there is no row in B that matches the ON condition, an extra B row is generated with all columns set to NULL.
如果A中有一行与WHERE子句匹配,但B中没有任何一行与ON条件匹配,则会生成多余的B行,并将所有列都设置为NULL。



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.
如果使用LEFT JOIN查找某个表中不存在的行,并且进行以下测试:col_name在WHERE部分中为NULL,其中col_name是声明为NOT NULL的列,则MySQL停止搜索更多行(对于 找到一个符合LEFT JOIN条件的行。



The RIGHT JOIN implementation is analogous to that of LEFT JOIN with the table roles reversed. Right joins are converted to equivalent left joins, as described in Section 8.2.1.9, “Outer Join Simplification”.
RIGHT JOIN的实现类似于LEFT JOIN的实现,其中表角色相反。 如第8.2.1.9节“简化外部连接”所述,将右连接转换为等效的左连接。



For a LEFT JOIN, if the WHERE condition is always false for the generated NULL row, the LEFT JOIN is changed to an inner join. For example, the WHERE clause would be false in the following query if t2.column1 were NULL:
对于LEFT JOIN,如果对于所生成的NULL行,WHERE条件始终为false,则将LEFT JOIN更改为内部联接。 例如,如果t2.column1为NULL,则在以下查询中WHERE子句将为false:


SELECT * FROM t1 LEFT JOIN t2 ON (column1) WHERE t2.column2=5;


Therefore, it is safe to convert the query to an inner join:
因此,将查询转换为内部联接是安全的:


SELECT * FROM t1, t2 WHERE t2.column2=5 AND t1.column1=t2.column1;


Now the optimizer 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;
现在,优化程序可以在表t1之前使用表t2,如果这样做会导致更好的查询计划。 要提供有关表连接顺序的提示,请使用STRAIGHT_JOIN;




举报

相关推荐

0 条评论