半连接是MySQL 5.6.5引入的,多在子查询exists中使用,对外部row source的每个键值,查找到内部row source匹配的第一个键值后就返回,如果找到就不用再查找内部row source其他的键值了。

测试环境

mysql> desc class;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| class_num | int(11) | NO | PRI | NULL | |
| class_name | varchar(20) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> desc roster;
+-------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| class_num | int(11) | YES | | NULL | |
| student_num | int(11) | YES | | NULL | |
+-------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

roster表中记录的是学生的学号以及对应的教室,多个学生可能在同一个教室,所以字段class_num有重复值

class表中记录的是教室及对应的班级名,字段class_num为唯一值

如果要查询存在学生的班级有哪些

mysql>  SELECT class.class_num, class.class_name FROM class INNER JOIN roster WHERE class.class_num = roster.class_num;
+-----------+------------+
| class_num | class_name |
+-----------+------------+
| 2 | class 2 |
| 3 | class 3 |
| 3 | class 3 |
+-----------+------------+
3 rows in set (0.00 sec)

可以通过distinct去除重复值,但这样做影响性能,所以通过子查询来得出结果

mysql>  SELECT class_num, class_name FROM class WHERE class_num IN (SELECT class_num FROM roster);
+-----------+------------+
| class_num | class_name |
+-----------+------------+
| 2 | class 2 |
| 3 | class 3 |
+-----------+------------+
2 rows in set (0.00 sec)

优化器实际上是将子查询改写为了半连接

mysql> explain SELECT class_num, class_name FROM class WHERE class_num IN (SELECT class_num FROM roster);
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------------------------------------------------------------+
| 1 | SIMPLE | roster | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | Start temporary |
| 1 | SIMPLE | class | NULL | ALL | PRIMARY | NULL | NULL | NULL | 4 | 25.00 | Using where; End temporary; Using join buffer (Block Nested Loop) |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------------------------------------------------------------+
2 rows in set, 1 warning (0.00 sec) mysql> show warnings;
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1003 | /* select#1 */ select `test`.`class`.`class_num` AS `class_num`,`test`.`class`.`class_name` AS `class_name` from `test`.`class` semi join (`test`.`roster`) where (`test`.`class`.`class_num` = `test`.`roster`.`class_num`) |
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Start temporary 和 End temporary的使用表明使用了临时表来去除重复值
如果 select_type 的值为 MATERIALIZED 并且 字段 rows的输出是 <subqueryN> 则表明临时表用于了物化表 select_type value of MATERIALIZED and rows with a table value of <subqueryN>.

如果子查询符合准则(参考文献:http://dev.mysql.com/doc/refman/5.7/en/subquery-optimization.html#semi-joins),MySQL将其转化为semi-join并从以下策略中作出基于cost的选择

  • Convert the subquery to a join, or use table pullout and run the query as an inner join between subquery tables and outer tables. Table pullout pulls a table out from the subquery to the outer query.

  • Duplicate Weedout: Run the semi-join as if it was a join and remove duplicate records using a temporary table.

  • FirstMatch: When scanning the inner tables for row combinations and there are multiple instances of a given value group, choose one rather than returning them all. This "shortcuts" scanning and eliminates production of unnecessary rows.

  • LooseScan: Scan a subquery table using an index that enables a single value to be chosen from each subquery's value group.

  • Materialize the subquery into a temporary table with an index and use the temporary table to perform a join. The index is used to remove duplicates. The index might also be used later for lookups when joining the temporary table with the outer tables; if not, the table is scanned

不确定的内容不敢随意翻译,摘出来原汁原味的文献内容

系统变量optimizer_switch中的semi join 标签控制着半连接是否可用,5.6默认是开启的

MySQL 通过semi join 优化子查询的更多相关文章

  1. MySQL 使用JOIN优化子查询

    1.数据准备 mysql> select * from student; +----+--------+----------+---------+-------------+ | id | na ...

  2. paip.sql索引优化----join 代替子查询法

    paip.sql索引优化----join 代替子查询法 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.n ...

  3. SQL Server中INNER JOIN与子查询IN的性能测试

    这个月碰到几个人问我关于"SQL SERVER中INNER JOIN 与 IN两种写法的性能孰优孰劣?"这个问题.其实这个概括起来就是SQL Server中INNER JOIN与子 ...

  4. join和子查询的一点点思考

    代码和表设计过程中,为了考虑数据库的范式,通常导致需要join多张表或子查询, 如报表场景, 可此种方式在大数据量的 情况下,效率较低.  如果能做适量的数据冗余,便可以减少join或子查询,效率较高 ...

  5. Mysql-SQL优化-子查询替代LEFT JOIN

    表A:批次信息表, 表B:实际批次明细表, Mysql版本:5.6.36 两表之间的数据体量差异:表B是表A的10000倍. 经过结转,表B通常保留 1千5百万数据.表A就是1千多条数据. 计算近24 ...

  6. MySql优化子查询

    用子查询语句来影响子查询中产生结果rows的数量和顺序. For example: SELECT * FROM t1 WHERE t1.column1 IN (SELECT column1 FROM ...

  7. Mysql基本用法-left join、right join、 inner join、子查询和join-02

    left join #左连接又叫外连接 left join 返回左表中所有记录和右表中连接字段相等的记录  test_user表 phpcvs表 SQL: select * from test_use ...

  8. Database学习 - mysql 数据库 多表/复合/子 查询

    多表查询 多表查询,基本规则,通过两表有关联字段的进行条件匹配查询 内连接查询 方式一: SELECT 查看字段名[,查看字段名] FROM 一表名,二表名 WHERE 一/二表.字段 = 一/二表. ...

  9. 优化子查询sql语句为内连接

    背景: 希望提高查询的效率,从sql语句中频繁出现的子查询入手. 数据表如下:Student表中的CityCode对应于City表中的Code. Student表:                   ...

随机推荐

  1. 在线调试lua原型设计

    在规模日益增长的软件项目开发中, 如何有效的进行代码调试成为影响开发效率的致命因素之一.在当今网络游戏项目中, lua几乎成了项目脚本的标配.编译型的语言, 诸如C++, 都有良好的ide支持调试.而 ...

  2. 十天精通CSS3学习笔记 part2

    第6章 征服CSS3选择器(上) 属性选择器 在HTML中,通过各种各样的属性可以给元素增加很多附加的信息.例如,通过id属性可以将不同div元素进行区分. 在CSS2中引入了一些属性选择器,而CSS ...

  3. IIS 常见问题集记录

    win7 iis7.5 详细错误信息模块 IIS Web Core 通知 BeginRequest 处理程序 尚未确定 错误代码 0x80070021 配置错误 不能在此路径中使用此配置节.如果在父级 ...

  4. Sublime Text 2 代码片断

    原文:Snippets 不管是在编码,还是写畅销书,你都可能会需要一遍又一遍的用到一些文本的小片断.使用片断来结束这种单调无聊的码字吧,片断是一种智能的模板,它能在合适的上下文中插入你需要的文本内容. ...

  5. ABAP面试问题及侧重点

    ABAP面试 1.简单的Report包括哪些东西 2.Dialog 逻辑流以及相应的处理内容 3.用过的几种增强方式:怎么找增强 4.接口和函数的使用,一般遇到自己不会的函数怎么处理 5.关联查询:I ...

  6. Codeforces Round #372 (Div. 2) C. Plus and Square Root

    题目链接 分析:这题都过了2000了,应该很简单..写这篇只是为了凑篇数= = 假设在第级的时候开方过后的数为,是第级的系数.那么 - 显然,最小的情况应该就是, 化简一下公式,在的情况下应该是,注意 ...

  7. 使用dreamweaver去掉文本中的空格和换行

    当我们从其他地方拷贝文本到网页,在html代码中会自动带有空格和换行,手动去掉很麻烦,今天试着用dreamweaver去了一下,方法如下: 1.点击Ctrl+F,打开“查找和替换”窗口 2‘见下图:

  8. JSON格式化与serialize序列化

    一.JSON格式化 1. JSON是什么 JSON是一种数据的存储格式,用来沟通客户端Javascript和服务端PHP的交互.我们把用PHP生成JSON后的字符串传给前台Javascript,Jav ...

  9. DotnetBar在VS2010工具箱中不显示问题

    请参考:http://blog.csdn.net/yanbo710148546/article/details/7862819

  10. 工作需求——JQ小效果分享下

    一.文字索引效果展示: html布局代码 <ul class="n_areaList"> <li> <h5>当前选择区域</h5> ...