创建一个表test_explain,并添加入下的数据

mysql> create  table test_explain( a int primary key, b int);

Query OK, 0 rows affected (0.09 sec)

mysql> insert into test_explain value(1,1),(2,2),(3,3),(4,4),(5,5);

explian中的type字段:表示mysql在表中找到所需行的方式,或者叫访问类型,常见的取值有ALL,INDEX ,RANGE,REF,EQ_REF,CONST(SYSTEM),NULL

情况1:type=all,全表扫描,mysql遍历全表来找到匹配的行。

mysql> explain select b from test_explain where b>3\G;

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: test_explain

partitions: NULL

type: ALL

possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: 5

filtered: 33.33

Extra: Using where

1 row in set, 1 warning (0.01 sec)

情况2:type=index,索引扫描,MYSQL遍历整个索引来查询匹配的行

mysql> explain select a from test_explain where a>3\G;

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: test_explain

partitions: NULL

type: index

possible_keys: PRIMARY

key: PRIMARY

key_len: 4

ref: NULL

rows: 5

filtered: 40.00

Extra: Using where; Using index

1 row in set, 1 warning (0.01 sec)

情况3:type=range,索引扫描范围,常见于,>=,between等操作符

mysql> explain select * from test_explain where a>3 and a<5\G;

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: test_explain

partitions: NULL

type: range

possible_keys: PRIMARY

key: PRIMARY

key_len: 4

ref: NULL

rows: 1

filtered: 100.00

Extra: Using where

1 row in set, 1 warning (0.01 sec)

情况4:type=ref,非唯一索引扫描或唯一索引扫描的前缀扫描,返回匹配某个单独值的记录行

首先为之前创建的表test_explain表的列b增加一个非唯一索引,操作如下:

mysql> alter table test_explain add index(b);,接着的实验结果为:

mysql> explain select *from test_explain where b=3 \G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: test_explain

partitions: NULL

type: ref

possible_keys: b

key: b

key_len: 5

ref: const

rows: 1

filtered: 100.00

Extra: Using index

1 row in set, 1 warning (0.00 sec)

情况5:type=eq_ref,类似ref,区别就在使用的索引是唯一索引,对于每个索引键值,表中只有一条记录匹配;简单来说,就是在多表连接中使用primary key或者unique index作为关联条件;注意的前提条件一定是多表连接中;

举例:新建一个表test_explain2

mysql> create table test_explain2( d int primary key,

-> e char(10) unique key,

-> f int);

Query OK, 0 rows affected (0.08 sec)

mysql> insert into  test_explain2 values(1,'a',1),(2,'b',2);

Query OK, 2 rows affected (0.04 sec)

Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into  test_explain2 values(3,'c',3),(4,'d',4);

Query OK, 2 rows affected (0.02 sec)

Records: 2  Duplicates: 0  Warnings: 0

接着来进行type=eq_ref的试验验证;

mysql> explain SELECT *from test_explain tt,test_explain2  yy where tt.a=yy.d \G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: yy

partitions: NULL

type: ALL

possible_keys: PRIMARY

key: NULL

key_len: NULL

ref: NULL

rows: 4

filtered: 100.00

Extra: NULL

*************************** 2. row ***************************

id: 1

select_type: SIMPLE

table: tt

partitions: NULL

type: eq_ref

possible_keys: PRIMARY

key: PRIMARY

key_len: 4

ref: test.yy.d

rows: 1

filtered: 100.00

Extra: NULL

2 rows in set, 1 warning (0.00 sec)

情况6:type=const/system,单表中最多有一个匹配行,查询起来非常迅速,所以这个匹配行中的其他列值可以被优化器在当前查询中当做常量来处理,例如根据主键或者唯一索引unique key进行的查询;

mysql> explain select *from test_explain where a=1\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: test_explain

partitions: NULL

type: const

possible_keys: PRIMARY

key: PRIMARY

key_len: 4

ref: const

rows: 1

filtered: 100.00

Extra: NULL

1 row in set, 1 warning (0.00 sec)

情况7:type=NULL,不用访问表或者索引就可以得到结果,如;

mysql> EXPLAIN SELECT 3 \G *************************** 1. row ***************************            id: 1   select_type: SIMPLE         table: NULL    partitions: NULL          type: NULL possible_keys: NULL           key: NULL       key_len: NULL           ref: NULL          rows: NULL      filtered: NULL         Extra: No tables used 1 row in set, 1 warning (0.00 sec)

mysql 执行计划详解,Mysql中的explain执行计划详解(1)相关推荐

  1. Mysql高级调优篇——第二章:Explain执行计划深度剖析

    1.Mysql Query Optimizer 这个名称在前言部分我在Mysql的整体架构中介绍过,称为查询优化器:这个查询优化器在绝大多数的公司,是不会做任何修改和扩展的,因为业务不需要,大牛请不起 ...

  2. python中configparser详解_Python中的ConfigParser模块使用详解

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...

  3. 详解python中for循环的_详的意思

    字: 详 简解: 详 (詳) xiáng 细密,完备,与"略"相对:详细.详略.详情.详谈.周详.详实(详细而确实.亦作"翔实").语焉不详(说得不详细). 清 ...

  4. php 代码延迟执行,php和js编程中的延迟执行效果的代码

    php和js编程中的延迟执行效果的代码 php sleep(10); usleep(10); js里的 setInterval("方法", 100); PHP sleep() 函数 ...

  5. mysql日期比较大小 方式_mysql中日期比较大小方法详解

    在mysql中日期比较有许多的函数,下面我来给大家总结一下常用的mysql中日期比较大小有需要了解的朋友可进入参考参考,假如有个表product有个字段add_time,它的数据类型为datetime ...

  6. mysql枚举类型enum用法_mysql中枚举类型之enum详解

    enum类型就是我们常说的枚举类型,它的取值范围需要在创建表时通过枚举方式(一个个的列出来)显式指定,对1至255个成员的枚举需要1个字节存储: 对于255至65535个成员,需要2个字节存储.最多允 ...

  7. mysql 对视图的操作_Mysql中关于视图操作的详解

    视图的操作: 1.视图的创建:create view view_name as 查询语句; 2.视图的查看:show tables;// 显示所有的表和视图 show create view view ...

  8. mysql执行计划性能_MySQL SQL性能分析Explain执行计划

    一. 执行计划返回信息详解 ①. 执行计划所含字段 输出列含义id查询标识 select_type查询类型 table查询涉及的表 partitions匹配到的分区信息 type连接类型 possib ...

  9. java构造器详解_Java中关于构造器的使用详解

    这篇文章主要介绍了Java构造器使用方法及注意事项的相关资料,这里举例说明如何使用构造器及需要注意的地方,需要的朋友可以参考下 Java构造器使用方法及注意事项 超类的构造器在子类的构造器运行之前运行 ...

最新文章

  1. libyuv库的使用
  2. 用JDBC写一个学生管理系统(添加、删除、修改、查询学生信息)
  3. Netty聊天之发送图片
  4. php打印四边形,用php的for循环输出四边形,各种三角形和菱形【含空心版本】
  5. 开启AngularJs之旅
  6. Java 8 – Period and Duration examples
  7. mysql查询建表SQL语句
  8. 最小生成树计数(HYSBZ-1016)(简化版实现)
  9. 【nexus】nexus : mac 安装 nexus
  10. Mongodb源码分析--Mongos之分布式锁
  11. python 获取json后 dict列表形式输出结果
  12. Git提交错误:Permission denied (publickey),fatal: Could not read from remote reposito
  13. mapgis6.7破解版|mapgis6.7破解版客户端(附安装教程)下载
  14. qlabel 添加图标_Qt中用QLabel显示图片
  15. windows 的快捷截图
  16. 算法工程师独得恩宠 四面楚歌的Android工程师该何去何从?
  17. [译] 什么是即时通信(Instant Messaging)
  18. cocos shader 之 黑白滤镜
  19. [电路]10-支路电流法
  20. 无界键鼠(MouseWithoutBorders)的安装与无法连接的解决方案

热门文章

  1. ZooKeeper启动报错 JAVA_HOME is incorrectly set
  2. Java-异常02 捕获和抛出异常
  3. php脚本防上多进程同时执行,Jorker
  4. floquet端口x极化入射波_请问CST 2012 floquet中的模式设置
  5. ora-07445 oracle 9,Oracle ORA-07445 : 出现异常错误: 核心转储(一)
  6. linux账户初始化文件,Linux启动初始化配置文件浅析
  7. curd什么意思中文_查英英字典:What a shame是什么意思?
  8. 可观测性PHP秩判据,线性系统的可控性与可观测性
  9. 字符ascii码值转换_没想到 Unicode 字符还能这样玩?
  10. frm考试可以用计算机,FRM考试,考生自己可以携带计算器吗?