美文网首页databases
Mysql 优化器之外连接消除笔记

Mysql 优化器之外连接消除笔记

作者: 沐一同学 | 来源:发表于2018-06-13 18:38 被阅读0次

纸上得来终觉浅,绝知此事要躬行。

看了好久理解理解的都不深刻,动手做了一下实验,发现好简单。

理解NULL拒绝含义,满足空值拒绝,则退化为内连接,可以消除外连接

1. prepare sql

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `t_1`;

CREATE TABLE `t_1` (

  `t_1_id` int(11) DEFAULT NULL,

  `t_1_col_1` int(11) DEFAULT NULL,

  `t_1_col_2` varchar(10) DEFAULT NULL,

  UNIQUE KEY `t_1_id` (`t_1_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `t_1` VALUES ('1', '11', 't_1_1');

INSERT INTO `t_1` VALUES ('2', '12', null);

INSERT INTO `t_1` VALUES ('3', null, 't_1_3');

INSERT INTO `t_1` VALUES ('4', '14', 't_1_4');

INSERT INTO `t_1` VALUES ('5', '15', null);

INSERT INTO `t_1` VALUES ('7', null, null);

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `t_2`;

CREATE TABLE `t_2` (

  `t_2_id` int(11) DEFAULT NULL,

  `t_2_col_1` int(11) DEFAULT NULL,

  `t_2_col_2` varchar(10) DEFAULT NULL,

  UNIQUE KEY `t_2_id` (`t_2_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `t_2` VALUES ('1', '11', 't_2_1');

INSERT INTO `t_2` VALUES ('2', null, 't_2_2');

INSERT INTO `t_2` VALUES ('3', '13', null);

INSERT INTO `t_2` VALUES ('4', '14', 't_2_4');

INSERT INTO `t_2` VALUES ('6', '16', 't_2_6');

INSERT INTO `t_2` VALUES ('7', null, null);

2. select 语句

当null比较时unknown会被处理成false

原则:当left join退化为内连接时,可以消除外连接,满足空值拒绝

2.1  条件(where条件)可以保证结果中排除外连接右侧(右表)生成的值为null的行(即条件确保应用的右表带有的空值的列对象上时,条件不成立,条件的结果为false或者unkown,这样游标就不会有值为null的行生成),退化成内连接。

1)

select * from t_1 left join t_2 on true where t_1_id = t_2_id;

explain extended select * from t_1 left join t_2 on true wheret_1_id = t_2_id;

2)

select * from t_1 left join t_2 on t_1_id = t_2_id;

explain extended select * from t_1 left join t_2 on t_1_id = t_2_id;

3)

select * from t_1 left join t_2 on t_1_id = t_2_id where t_1_id = t_2_id;

explain extended select * from t_1 left join t_2 on t_1_id = t_2_id where t_1_id = t_2_id;

PS:t_1_id = t_2_idunique 所以,不存在未null的行

2.2 外连接提供空值的一侧为另一侧的每行只返回一行,如果该条件为真,则不存在提供空值的行,退化为内连接。

1)

select * from t_1 left join t_2 on true where t_1_col_1 = t_2_col_1;

explain extended select * from t_1 left join t_2 on true wheret_1_col_1 = t_2_col_1;

2)

select  * from t_1 left join t_2 on  t_1_col_1 = t_2_col_1;

explain extended select  * from t_1 left join t_2 on  t_1_col_1 = t_2_col_1;

3)

select  * from t_1 left join t_2 on  t_1_col_1 = t_2_col_1 wheret_1_col_1 = t_2_col_1;

explain extended select  * from t_1 left join t_2 on  t_1_col_1 = t_2_col_1 where t_1_col_1 = t_2_col_1;

ps:t_1_col_1 = t_2_col_1 肯定为true,不存在为false的情况。null = x or x = null or null = null -> false

2.3

1)

explain extended select * from t_1 left join t_2 on true where t_1_id > 0 and t_2_id > 0;

ok : t_2_id > 0 为ture,退化为内连接,可以消除链接

2)

explain extended select * from t_1 left join t_2 on t_1_id = t_2_id where t_1_id > 0;

no : 提供空值的右表中,提供一些空值几率。没有消除t_1表中的id=5的记录

3)

explain extended select * from t_1 left join t_2 on t_1_id = t_2_id where t_2_id >0;

ok: t_2_id > 0会过滤到提供的空值的记录

4)

explain extended select * from t_1 left join t_2 on t_1_id = t_2_id where t_1_id > 0 or t_2_id >0;

no :  or 表达式,是的where条件可能无法过滤提供空值的记录 t_1表中id=5的记录

相关文章

  • Mysql 优化器之外连接消除笔记

    纸上得来终觉浅,绝知此事要躬行。 看了好久理解理解的都不深刻,动手做了一下实验,发现好简单。 理解NULL拒绝含义...

  • mysql 优化器链接消除笔记

    1. 嵌套链接消除 连接存在多个层次,用括号标识连接的优先次序。嵌套连接消除,就是消除嵌套的连接层次,把多个层次的...

  • MySQL:一条查询语句是怎么执行的?

    本章内容主要讲解MySQL各个功能模块怎么执行。 首先,MySQL中有连接器、分析器、优化器、执行器。 连接器:用...

  • MySQL

    组成 MySQL结构是分为:连接器、分析器、优化器、执行器连接器负责连接控制、用户认证。分析器负责语法分析、表字段...

  • 1. MySql基础架构

    Mysql的基本架构示意图: Mysql可分为:Server层——连接器,查询缓存,分析器,优化器,执行器等。大多...

  • 3. MySQL架构和性能优化

    5. MySQL架构和性能优化 MySQL是C/S架构的, Connector是连接器, 可供Native C A...

  • mysql的执行过程

    有时候会问说一下mysql的执行过程? mysql 六大组价 连接器、缓存、分析器、优化器、执行器、存储引擎 1、...

  • MySQL执行流程

    MySQL执行流程 一共会经历四步,分别是连接、分析、优化与执行。 连接器 连接器会拿着我们输入的IP和端口,去做...

  • MySQL - 基础架构

    MySql的基础架构分成两大部分(如图): Server层 : 连接器 、查询缓存、解析器、优化器、执行器数据...

  • mysql的概念以及mysql架构分析

    mysql逻辑结构从图中可以看到MySQL由以下几个部分组成连接池管理服务和工具组件SQL接口查询分析器优化器缓存...

网友评论

    本文标题:Mysql 优化器之外连接消除笔记

    本文链接:https://www.haomeiwen.com/subject/pskseftx.html