mysql5.6子查询的优化
https://dev.mysql.com/doc/refman/5.6/en/subquery-optimization.html
Semi-join in MySQL 5.6
A subquery can be transformed to a semi-join if it matches these criteria:
- The subquery is part of an IN or =ANY predicate. It cannot be e.g. NOT IN.
- The subquery consists of a single query block (it must not contain UNION).
- The subquery does not contain GROUP BY or HAVING.
- The subquery is not implicitly grouped (it contains no aggregate functions).
- The subquery predicate is part of a WHERE clause.
- The subquery predicate must not be part of a disjunctive nor a negated search condition.
- Neither query block contains the STRAIGHT_JOIN qualifier.
- The statement must be a SELECT or INSERT statement. Semi-joins are not allowed with UPDATE or DELETE statements.
#子查询是IN或者=ANY的,不可以是NOT IN
#子查询只能包含一个Query block,不可以有UNION等操作
#子查询不能包含GROUP BY或者HAVING
#子查询不能包含聚合函数
#子查询谓语不可以是外接查询条件或者否定查询条件
#不可以包含STRAIGHT_JOIN限定词
#Semi-join只能用于SELECT或者INSERT,不可用于UPDATE和DELETE
What is a semi-join operation
- In a semi-join, the inner tables do not cause duplicates in the result.
- No columns from the inner tables are added to the result of the operation.
Semi-join optimization
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+---------------+---------+-------------------------+------+-------------+
| 1 | PRIMARY | region | ALL | PRIMARY | NULL | NULL | NULL | 5 | Using where |
| 1 | PRIMARY | nation | ref | i_n_regionkey | i_n_regionkey | 5 | dbt3.region.r_regionkey | 2 | |
Semi-join execution strategies
- FirstMatch
- DuplicateWeedout
- Materialization
- LooseScan
Controlling semi-join strategies
Semi-join FirstMatch strategy
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+---------------+---------+-------------------------+------+---------------------------------+
| 1 | PRIMARY | nation | ALL | PRIMARY | NULL | NULL | NULL | 25 | |
| 1 | PRIMARY | customer | ref | i_c_nationkey | i_c_nationkey | 5 | dbt3.nation.n_nationkey | 1115 | Using index; FirstMatch(nation) |
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+----------+----------------+---------------+---------------+---------+------+------+-------------+
| 1 | PRIMARY | nation | ALL | NULL | NULL | NULL | NULL | 25 | Using where |
| 2 | DEPENDENT SUBQUERY | customer | index_subquery | i_c_nationkey | i_c_nationkey | 5 | func | 1115 | Using index |
Multiple subqueries
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+-----------------------+---------------+---------+-------------------------+------+---------------------------------+
| 1 | PRIMARY | region | ALL | PRIMARY | NULL | NULL | NULL | 5 | Using where |
| 1 | PRIMARY | nation | ref | PRIMARY,i_n_regionkey | i_n_regionkey | 5 | dbt3.region.r_regionkey | 2 | |
| 1 | PRIMARY | customer | ref | i_c_nationkey | i_c_nationkey | 5 | dbt3.nation.n_nationkey | 1115 | Using index; FirstMatch(nation) |
Nested subqueries
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+---------------+---------+-------------------------+---------+---------------------------------+
| 1 | PRIMARY | nation | ALL | PRIMARY | NULL | NULL | NULL | 25 | |
| 1 | PRIMARY | customer | ref | i_c_nationkey | i_c_nationkey | 5 | dbt3.nation.n_nationkey | 1115 | |
| 1 | PRIMARY | orders | ALL | NULL | NULL | NULL | NULL | 1486350 | Using where; FirstMatch(nation) |
mysql5.6子查询的优化的更多相关文章
- MySQL子查询的优化
本文基于MySQL5.7.19测试 创建四张表,pt1.pt2表加上主键 mysql> create table t1 (a1 int, b1 int); mysql> create ta ...
- MySQL 子查询(四)子查询的优化、将子查询重写为连接
MySQL 5.7 ref ——13.2.10.10优化子查询 十.子查询的优化 开发正在进行中,因此从长远来看,没有什么优化建议是可靠的.以下列表提供了一些您可能想要使用的有趣技巧.See also ...
- 优化系列 | DELETE子查询改写优化
0.导读 有个采用子查询的DELETE执行得非常慢,改写成SELECT后执行却很快,最后把这个子查询DELETE改写成JOIN优化过程 1.问题描述 朋友遇到一个怪事,一个用子查询的DELETE,执行 ...
- Mysql查询优化器之关于子查询的优化
下面这些sql都含有子查询: mysql> select * from t1 where a in (select a from t2); mysql> select * from (se ...
- mysql关联、子查询索引优化
1.驱动表:加索引不起作用,因为全表扫描.表1 left join 表2 ,此时表1是驱动表 被驱动表:给这个加索引. 关联查询 子查询时 尽量不使用not in 或者not exists 而是用 ...
- oracle查询优化之子查询条件优化
环境:oracle 11g 现有a表与b表通过a01字段关联,要查询出a表的数据在b表没有数据的数据:sql如下 ) ) 因为flag是虚拟字段没有走不了索引导致这条sql执行起来特别慢 310W条数 ...
- 深入理解MySql子查询IN的执行和优化
IN为什么慢? 在应用程序中使用子查询后,SQL语句的查询性能变得非常糟糕.例如: SELECT driver_id FROM driver where driver_id in (SELECT dr ...
- MySQL的一次优化记录 (IN子查询和索引优化)
这两天实习项目遇到一个网页加载巨慢的问题(10多秒),然后定位到是一个MySQL查询特别慢的语句引起的: SELECT * FROM ( SELECT DISTINCT t.vc_date, t.c_ ...
- 聊聊MySQL的子查询
1. 背景 在之前介绍MySQL执行计划的博文中已经谈及了一些关于子查询相关的执行计划与优化.本文将重点介绍MySQL中与子查询相关的内容,设计子查询优化策略,包含半连接子查询的优化与非半连接子查询的 ...
随机推荐
- Unity3D开发之“获取IOS设备所在的国家代码"
原地址:http://dong2008hong.blog.163.com/blog/static/469688272014021025578/ 在前一段时间游戏开发中需要实现获取IOS设备所在的国家代 ...
- Android开发--解决AndroidADT开发工具不能代码提示的问题
google android的新的开发工具,打开以后没有代码自动提示功能,下面对ADT工具的一些配置: 1.设置代码的字体 设置JAVA文件代码的字体:我这里设置的14 常规.
- Win7-其中的文件夹或文件已在另一个程序中打开
Win7-其中的文件夹或文件已在另一个程序中打开 如何解决Win7系统在删除或移动文件时提示,“操作无法完成,因为其中的文件夹或文件已在另一个程序中打开,请关闭该文件夹或文件,然后重试”. 步骤阅 ...
- LR_问题_如何将场景中的用户设置为百分比形式
一个场景运行多个脚本时,如何按照百分比模式运行
- HTML5 编辑 API 之 Range 对象(二)
1.Range.cloneContents()The Range.cloneContents() returns a DocumentFragment copying the objects of t ...
- OSSEC - Agent端查看命令
命令:/opt/ossec/bin/agent_control -h注释:/opt/为安装目录 [root@redhat rules]# /opt/ossec/bin/agent_control -h ...
- VLLookUp 不同表单按条件赋值
按条件赋值,如两个表单 假设在Sheet1中存放小麦.水稻.玉米.花生等若干农产品的销售单价: A B 1 农产品名称 单价 2 小麦 0.56 3 水稻 0.48 4 玉米 0.39 5 花生 0. ...
- 转TerreyLee AJAX入门系列2——ScriptManager的理解总结
ScriptManager的功能之一就是处理页面上局部更新,对于这点,我想大家都知道.但是他工作的原理到底是什么呢,这个暂且不从正面来回答. 我们这样想一下,目前能够真正实现局部刷新的就是js+xml ...
- 怎么在eclipse里调试WebDriver的源代码
当你看完WebDriver的工作原理这篇博客以后,是不是也跃跃欲试想印证文章里的理论是不是正确,想自己也看下webdriver的源代码,并且调试下,通过代码来更深入的了解WebDriver的工作原理. ...
- Drawit插件
gvim用不了画矩形的功能,只能在vim下用 \di,\ds开始/结束画图(Vim里\按键没有被映射,可以做leader按键) 用鼠标选择一块之后,\b画矩形,\e画椭圆 选单行\a画箭头,\l画线 ...