MySQL EXPLAIN type类型说明[通俗易懂]

MySQL EXPLAIN type类型说明[通俗易懂]EXPLAIN执行计划中type字段分为以下几种:ALL    INDEX    RANGE    REF    EQ_REF    CONST,SYSTEM    NULL自上而下,性能从最差到最好 type=ALL,全表扫描,MYSQL扫描全表来找到匹配的行(因为film表中rating不是索引)mysql>explainexten…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

EXPLAIN执行计划中type字段分为以下几种:

ALL        

INDEX        

RANGE        

REF        

EQ_REF        

CONST,SYSTEM        

NULL

自上而下,性能从最差到最好

 

type = ALL,全表扫描,MYSQL扫描全表来找到匹配的行

(因为film表中rating不是索引)

mysql> explain extended select * from film where rating > 9\G

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

           id: 1

  select_type: SIMPLE

        table: film

         type: ALL

possible_keys: NULL

          key: NULL

      key_len: NULL

          ref: NULL

         rows: 1024

     filtered: 100.00

        Extra: Using where

1 row in set, 1 warning (0.00 sec)

 

type = index,索引全扫描,MYSQL遍历整个索引来查找匹配的行。(虽然where条件中没有用到索引,但是要取出的列title是索引包含的列,所以只要全表扫描索引即可,直接使用索引树查找数据)

mysql> explain select title from film\G

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

           id: 1

  select_type: SIMPLE

        table: film

         type: index

possible_keys: NULL

          key: idx_title

      key_len: 767

          ref: NULL

         rows: 1024

        Extra: Using index

1 row in set (0.00 sec)

 

type = range ,索引范围扫描,常见于<、<=、>、>=、between等操作符(因为customer_id是索引,所以只要查找索引的某个范围即可,通过索引找到具体的数据)

mysql> explain select * from payment where customer_id > 300 and customer_id < 350\G

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

           id: 1

  select_type: SIMPLE

        table: payment

         type: range

possible_keys: idx_fk_customer_id

          key: idx_fk_customer_id

      key_len: 2

          ref: NULL

         rows: 1294

        Extra: Using where

1 row in set (0.01 sec)

 

type = ref ,使用非唯一性索引或者唯一索引的前缀扫描,返回匹配某个单独值的记录行。

(1)使用非唯一性索引customer_id单表查询

mysql> explain select * from payment where customer_id = 350\G

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

           id: 1

  select_type: SIMPLE

        table: payment

         type: ref

possible_keys: idx_fk_customer_id

          key: idx_fk_customer_id

      key_len: 2

          ref: const

         rows: 23

        Extra:

1 row in set (0.00 sec)

(2)使用非唯一性索引联表查询(由于customer_id在a表中不是主键,是普通索引(非唯一),所以是ref)

mysql> explain select b.*, a.* from payment a ,customer b where a.customer_id = b.customer_id\G

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

           id: 1

  select_type: SIMPLE

        table: b

         type: ALL

possible_keys: PRIMARY

          key: NULL

      key_len: NULL

          ref: NULL

         rows: 541

        Extra:

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

           id: 1

  select_type: SIMPLE

        table: a

         type: ref

possible_keys: idx_fk_customer_id

          key: idx_fk_customer_id

      key_len: 2

          ref: sakila.b.customer_id

         rows: 14

        Extra:

2 rows in set (0.00 sec)  

 

type = eq_ref,相对于ref来说就是使用的是唯一索引,对于每个索引键值,只有唯一的一条匹配记录(在联表查询中使用primary key或者unique key作为关联条件)

(在film和film_text中film_id都是主键,即都是唯一索引)

mysql> explain select * from film a ,film_text b where a.film_id = b.film_id\G

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

           id: 1

  select_type: SIMPLE

        table: b

         type: ALL

possible_keys: PRIMARY

          key: NULL

      key_len: NULL

          ref: NULL

         rows: 1000

        Extra:

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

           id: 1

  select_type: SIMPLE

        table: a

         type: eq_ref

possible_keys: PRIMARY

          key: PRIMARY

      key_len: 2

          ref: sakila.b.film_id

         rows: 1

        Extra: Using where

2 rows in set (0.00 sec)

 

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

mysql> explain select * from film  where film_id = 1\G

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

           id: 1

  select_type: SIMPLE

        table: film

         type: const

possible_keys: PRIMARY

          key: PRIMARY

      key_len: 2

          ref: const

         rows: 1

        Extra:

1 row in set (0.02 sec)

 

注释:如果上表中film表中只有一行数据,那么type就是system。

 

type = NULL,MYSQL不用访问表或者索引就直接能到结果。

mysql> explain select 1 from dual  where 1\G (dual是一个虚拟的表,可以直接忽略)

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

           id: 1

  select_type: SIMPLE

        table: NULL

         type: NULL

possible_keys: NULL

          key: NULL

      key_len: NULL

          ref: NULL

         rows: NULL

        Extra: No tables used

1 row in set (0.00 sec)

 

mysql> select 1+1 from dual;

+—–+

| 1+1 |

+—–+

|   2 |

+—–+

1 row in set (0.05 sec)

 

explain extended

mysql> explain extended select sum(amount) from customer a ,payment b where 1 = 1 and a.customer_id = b.customer_id and email = ‘JANE.BENNETT@sakilacustomer.org’\G

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

           id: 1

  select_type: SIMPLE

        table: a

         type: ALL

possible_keys: PRIMARY

          key: NULL

      key_len: NULL

          ref: NULL

         rows: 541

     filtered: 100.00

        Extra: Using where

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

           id: 1

  select_type: SIMPLE

        table: b

         type: ref

possible_keys: idx_fk_customer_id

          key: idx_fk_customer_id

      key_len: 2

          ref: sakila.a.customer_id

         rows: 14

     filtered: 100.00

        Extra: 

2 rows in set, 1 warning (0.00 sec)

 

mysql> show warnings\G

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

  Level: Note

   Code: 1003

Message: select sum(`sakila`.`b`.`amount`) AS `sum(amount)` from `sakila`.`customer` `a` join `sakila`.`payment` `b` where ((`sakila`.`b`.`customer_id` = `sakila`.`a`.`customer_id`) and (`sakila`.`a`.`email` = ‘JANE.BENNETT@sakilacustomer.org’))

1 row in set (0.00 sec)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/183683.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 多重共线性检验之方差膨胀因子VIF[通俗易懂]

    多重共线性检验之方差膨胀因子VIF[通俗易懂]过程1、构造每一个自变量与其余自变量的线性回归模型,例如,数据集中含有p个自变量,则第一个自变量与其余自变量的线性组合可以表示为2、根据如上线性回归模型得到相应的判决系数R2R^2R2,进而计算第一个自变量的方差膨胀因子VIF:importpandasaspdimportnumpyasnpfromsklearnimportmodel_selectionimportstatsmodels.apiassnfromstatsmodels.stats.outlier

    2022年6月6日
    243
  • pip国内镜像(清华大学镜像)

    网上搜到的pip国内镜像大部分是豆瓣的http://pypi.douban.com/simple/但是根本不全,很多包没有所以推荐清华大学的https://pypi.tuna.tsinghua.edu.cn/simple临时使用可以在使用pip的时候加参数-ihttps://pypi.tuna.tsinghua.edu.cn/simple例如:pipinstall-ihttps://

    2022年4月6日
    562
  • vim编辑器

    vim编辑器

    2022年4月3日
    39
  • 位运算符有哪些_或运算和异或运算

    位运算符有哪些_或运算和异或运算位运算符的计算主要用在二进制中。实际开发中也经常会遇到需要用到这些运算符的时候,同时这些运算符也被作为基础的面试笔试题。所以了解这些运算符对程序员来说是十分必要的。于此,记录下我所理解的运算符:如果以开关开灯论:有这样两个开关,0为开关关闭,1为开关打开。与(&)运算与运算进行的是这样的算法:0&0=0,0&1=0,1&0=0,1&1=1在与运算中两个开关是

    2022年10月10日
    1
  • char与byte的区别

    char与byte的区别很多初学者 包括我 已经学了一年多 java 了 肯会对 char 和 byte 这两种数据类型有所疑惑 相互混淆 今天特地查了好多资料 对 byte 和 char 两种数据类型进行了总结和比较 先将结果与大家分享 nbsp nbsp nbsp nbsp byte nbsp 是字节数据类型 nbsp 是有符号型的 占 1 nbsp 个字节 大小范围为 128 127 char nbsp 是字符数据类型 nbsp 是无符号型的 占 2 字节 Unicode 码 nbsp 大小范围 nbsp 是 0 65

    2025年7月5日
    1
  • Java——DOM方式生成XML

    Java——DOM方式生成XML学完了解析XML,就该学习生成XML文件了。首先学习的是如何使用DOM方式生成XML文件。使用DOM方式生成XML文件有如下几步:首先是创建DOM树(即规定XML文件中的内容):创建DocumentBuilderFactory对象通过DocumentBuilderFactory对象创建DocumentBuilder对象通过DocumentBuilder对象的newDocument()方法创建一

    2022年7月21日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号