mysql执行计划详解

​ 在企业的应用场景中,为了知道优化SQL语句的执行,需要查看SQL语句的具体执行过程,以加快SQL语句的执行效率。

​ 可以使用explain+SQL语句来模拟优化器执行SQL查询语句,从而知道mysql是如何处理sql语句的。

Mycat执行计划格式

1.先用explain查看路由信息

格式: explain sql语句例: explain select * from t_user

2.查看执行计划的语句格式(MyCat)

# 格式:
explain2 datanode=数据节点 sql=SQL语句# 实例
EXPLAIN2 explain2 datanode=ios_dn1 sql=select * from t_user;

​ 官网地址: https://dev.mysql.com/doc/refman/5.5/en/explain-output.html

执行计划中包含的信息

Column Meaning
id The SELECT identifier
select_type The SELECT type
table The table for the output row
partitions The matching partitions
type The join type
possible_keys The possible indexes to choose
key The index actually chosen
key_len The length of the chosen key
ref The columns compared to the index
rows Estimate of rows to be examined
filtered Percentage of rows filtered by table condition
extra Additional information

id

select查询的序列号,包含一组数字,表示查询中执行select子句或者操作表的顺序

id号分为三种情况:

​ 1、如果id相同,那么执行顺序从上到下

explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal;

​ 2、如果id不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行


explain select * from emp e where e.deptno in (select d.deptno from dept d where d.dname = 'SALES');

​ 3、id相同和不同的,同时存在:相同的可以认为是一组,从上往下顺序执行,在所有组中,id值越大,优先级越高,越先执行


explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal where e.deptno in (select d.deptno from dept d where d.dname = 'SALES');

select_type

主要用来分辨查询的类型,是普通查询还是联合查询还是子查询

select_type Value Meaning
SIMPLE Simple SELECT (not using UNION or subqueries)
PRIMARY Outermost SELECT
UNION Second or later SELECT statement in a UNION
DEPENDENT UNION Second or later SELECT statement in a UNION, dependent on outer query
UNION RESULT Result of a UNION.
SUBQUERY First SELECT in subquery
DEPENDENT SUBQUERY First SELECT in subquery, dependent on outer query
DERIVED Derived table
UNCACHEABLE SUBQUERY A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query
UNCACHEABLE UNION The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY)
--sample:简单的查询,不包含子查询和union
explain select * from emp;
​
--primary:查询中若包含任何复杂的子查询,最外层查询则被标记为Primary
explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ;
​
--union:若第二个select出现在union之后,则被标记为union
explain select * from emp where deptno = 10 union select * from emp where sal >2000;
​
--dependent union:跟union类似,此处的depentent表示union或union all联合而成的结果会受外部表影响
explain select * from emp e where e.empno  in ( select empno from emp where deptno = 10 union select empno from emp where sal >2000)
​
--union result:从union表获取结果的select
explain select * from emp where deptno = 10 union select * from emp where sal >2000;
​
--subquery:在select或者where列表中包含子查询
explain select * from emp where sal > (select avg(sal) from emp) ;
​
--dependent subquery:subquery的子查询要受到外部表查询的影响
explain select * from emp e where e.deptno in (select distinct deptno from dept);
​
--DERIVED: from子句中出现的子查询,也叫做派生类,
explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ;
​
--UNCACHEABLE SUBQUERY:表示使用子查询的结果不能被缓存explain select * from emp where empno = (select empno from emp where deptno=@@sort_buffer_size);--uncacheable union:表示union的查询结果不能被缓存:sql语句未验证

table

对应行正在访问哪一个表,表名或者别名,可能是临时表或者union合并结果集 1、如果是具体的表名,则表明从实际的物理表中获取数据,当然也可以是表的别名

​ 2、表名是derivedN的形式,表示使用了id为N的查询产生的衍生表

​ 3、当有union result的时候,表名是union n1,n2等的形式,n1,n2表示参与union的id

type

type显示的是访问类型,访问类型表示我是以何种方式去访问我们的数据,最容易想的是全表扫描,直接暴力的遍历一张表去寻找需要的数据,效率非常低下,访问的类型有很多,效率从最好到最坏依次是:

system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

一般情况下,得保证查询至少达到range级别,最好能达到ref


--all:全表扫描,一般情况下出现这样的sql语句而且数据量比较大的话那么就需要进行优化。
explain select * from emp;
​
--index:全索引扫描这个比all的效率要好,主要有两种情况,一种是当前的查询时覆盖索引,即我们需要的数据在索引中就可以索取,或者是使用了索引进行排序,这样就避免数据的重排序
explain  select empno from emp;
​
--range:表示利用索引查询的时候限制了范围,在指定范围内进行查询,这样避免了index的全索引扫描,适用的操作符: =, <>, >, >=, <, <=, IS NULL, BETWEEN, LIKE, or IN()
explain select * from emp where empno between 7000 and 7500;
​
--index_subquery:利用索引来关联子查询,不再扫描全表
explain select * from emp where emp.job in (select job from t_job);
​
--unique_subquery:该连接类型类似与index_subquery,使用的是唯一索引explain select * from emp e where e.deptno in (select distinct deptno from dept);--index_merge:在查询过程中需要多个索引组合使用,没有模拟出来
​
--ref_or_null:对于某个字段即需要关联条件,也需要null值的情况下,查询优化器会选择这种访问方式
explain select * from emp e where  e.mgr is null or e.mgr=7369;
​
--ref:使用了非唯一性索引进行数据的查找create index idx_3 on emp(deptno);explain select * from emp e,dept d where e.deptno =d.deptno;
​
--eq_ref :使用唯一性索引进行数据查找
explain select * from emp,emp2 where emp.empno = emp2.empno;
​
--const:这个表至多有一个匹配行,
explain select * from emp where empno = 7369;--system:表只有一行记录(等于系统表),这是const类型的特例,平时不会出现

possible_keys

​ 显示可能应用在这张表中的索引,一个或多个,查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询实际使用


explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

key

​ 实际使用的索引,如果为null,则没有使用索引,查询中若使用了覆盖索引,则该索引和查询的select字段重叠。


explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

key_len

表示索引中使用的字节数,可以通过key_len计算查询中使用的索引长度,在不损失精度的情况下长度越短越好。


explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

ref

显示索引的哪一列被使用了,如果可能的话,是一个常数


explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10;

rows

根据表的统计信息及索引使用情况,大致估算出找出所需记录需要读取的行数,此参数很重要,直接反应的sql找了多少数据,在完成目的的情况下越少越好


explain select * from emp;

extra

包含额外的信息。


--using filesort:说明mysql无法利用索引进行排序,只能利用排序算法进行排序,会消耗额外的位置
explain select * from emp order by sal;
​
--using temporary:建立临时表来保存中间结果,查询完成之后把临时表删除
explain select ename,count(*) from emp where deptno = 10 group by ename;
​
--using index:这个表示当前的查询时覆盖索引的,直接从索引中读取数据,而不用访问数据表。如果同时出现using where 表名索引被用来执行索引键值的查找,如果没有,表面索引被用来读取数据,而不是真的查找
explain select deptno,count(*) from emp group by deptno limit 10;
​
--using where:使用where进行条件过滤
explain select * from t_user where id = 1;
​
--using join buffer:使用连接缓存,情况没有模拟出来
​
--impossible where:where语句的结果总是false
explain select * from emp where empno = 7469;

文章部分内容来自:https://www.bilibili.com/video/BV16i4y1g7TF?from=search&seid=17650357849987331116&spm_id_from=333.337.0.0

mysql执行计划详解相关推荐

  1. mysql 执行计划详解_mysql explain执行计划详解

    备注:转载 1).id列数字越大越先执行,如果说数字一样大,那么就从上往下依次执行,id列为null的就表是这是一个结果集,不需要使用它来进行查询. 2).select_type列常见的有: A:si ...

  2. mysql 执行计划详解,Mysql中的explain执行计划详解(1)

    创建一个表test_explain,并添加入下的数据 mysql> create  table test_explain( a int primary key, b int); Query OK ...

  3. MySQL 优化:Explain 执行计划详解

    昨天中午在食堂,和部门的技术大牛们坐在一桌吃饭,作为一个卑微技术渣仔默默的吃着饭,听大佬们高谈阔论,研究各种高端技术,我TM也想说话可实在插不上嘴. 聊着聊着突然说到他上午面试了一个工作6年的程序员, ...

  4. Explain执行计划详解

    作者:IT王小二 博客:https://itwxe.com 一条查询 SQL 语句为什么会执行很慢?执行的时候走了什么索引?多表查询的时候 SQL 大体执行流程是怎么样的?Explain 执行计划详解 ...

  5. Explain 执行计划详解

    什么是执行计划 我们往往通过慢查询日志,可以找到慢查询的sql具体是怎么写的.我们往往可以通过表的索引,执行引擎等等去自己判断sql为什么执行缓慢的原因.但是这明显不是高效的. 我们的MySQL中为我 ...

  6. MySQL调优(八):查缺补漏(mysql的锁机制,读写分离,执行计划详解,主从复制原理)

    mysql的锁机制 1.MySQL锁的基本介绍 ​ 锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的 计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共 ...

  7. MySQL数据库:explain执行计划详解

    一.执行计划: 执行计划是SQL语句经过查询分析器后得到的 抽象语法树 和 相关表的统计信息 作出的一个查询方案,这个方案是由查询优化器自动分析产生的.由于是动态数据采样统计分析出来的结果,所以可能会 ...

  8. 引导mysql执行计划_Mysql执行计划详解

    --歌手表 CREATE TABLE`singer` ( `id`int(11) unsigned NOT NULL, `update_time`datetime DEFAULT NULL, `nam ...

  9. MySQL—— MySQL的执行计划详解(Explain)

    文章目录 1.MySQL执行计划的定义 2.MySQL整个查询的过程 3.如何启动执行计划 4.Explain分析示例 4.explain的 两个变种(我的版本是5.7) 5.explain中的列 5 ...

  10. MySQL高级 之 explain执行计划详解

    使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的,分析你的查询语句或是表结构的性能瓶颈. explain执行计划包含的信息 其中最重要的字段为:i ...

最新文章

  1. java opcode 反汇编,OPCode详解及汇编与反汇编原理
  2. oracle rac应急_ORACLE紧急情况检查应急预案
  3. 2.7 Inception 网络-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  4. 19 Handler 子线程向主线程发送信息
  5. 大剑无锋之Zookeeper面试题
  6. php 对象赋值后改变成员变量影响赋值对象
  7. djpython教程_简单介绍Python的Django框架的dj-scaffold项目
  8. struct对象可能分配在托管堆上吗
  9. jQuery之事件移除
  10. hashtable的C++实现
  11. hdu 动态规划46题
  12. EOS合约开发 - 钱包篇
  13. aurelia_Aurelia历险记:创建自定义PDF查看器
  14. 微信公众号开发之分享功能
  15. Android离线输入法,QQ输入法Android版4.9离线语音输入让沟通无障碍
  16. 深入理解设计模式-设计模式七大原则
  17. uchar t 单片机C语言的注释是什么,uchar(单片机中uchar是什么意思)
  18. Spring Boot进阶(13):如何获取@ApiModelProperty(value = “序列号“, name = “uuid“)中的value值 | 超级详细,建议收藏
  19. 成都瀚网科技:抖音发作品到底需要多久的时间才能够给流量呢?
  20. PAT-L2-027(名人堂与代金券)(结构体排序)

热门文章

  1. 通过自定义注解+反射的形式,使用POI实现excel的导入导出
  2. 2020年云计算发展趋势怎么样?
  3. 计算机无法加载这个项目,Windows系统中出现无法加载这个硬件的设备驱动程序(代码39)的解决方法介绍...
  4. java简历校园经历_简历校园经历如何编
  5. 认识了解WMV 9 视频编解码器
  6. Annotation-specified bean name conflicts with existing
  7. 学信网查学历和学位网查学位的基本操作
  8. 惠普服务器ilo默认地址_使用ILO进行HP服务器管理的Docker容器
  9. java中的StringBuffer类
  10. 小程序页面简单功能模块化之取整