有一个 ?
遇到这样一个疑问:当where中In一个索引字段,那么在查询中还会使用到索引吗?

SELECT * FROM table_name WHERE column_index in (expr)
上面的sql语句检索会使用到索引吗?带着这个问题,在网上查找了很多文章,但是有的说 in 会导致放弃索引,全表扫描;有的说Mysql5.5之前的版本不会走,之后的innodb会走索引…

越看越迷糊,那答案到底是怎样的呢?

未有实践是检验真理的唯一方式!

拿出我们的利刃——EXPLAIN,去剖析 SELECT 语句,一探究竟!

EXPLAIN 的用法
在 SELECT 语句前加上 EXPLAIN 就可以了 ,例如:

EXPLAIN SELECT * FROM table_name [WHERE Clause]
EXPLAIN 的输出
EXPLAIN 命令的输出内容为一个表格形式,表的每一个字段含义如下:

列名 解释
id SELECT 查询的标识符. 每个 SELECT 都会自动分配一个唯一的标识符
select_type SELECT 查询的类型
table 查询的是哪个表
partitions 匹配的分区
type join 类型
possible_keys 此次查询中可能选用的索引
key 此次查询中确切使用到的索引
ref 哪个字段或常数与 key 一起被使用;与索引比较的列
rows 显示此查询一共扫描了多少行, 这个是一个估计值
filtered 表示此查询条件所过滤的数据的百分比
extra 额外的信息
select_type
查询类型 解释
SIMPLE 表示此查询不包含 UNION 查询或子查询
PRIMARY 表示此查询是最外层的查询
UNION 表示此查询是 UNION 的第二或随后的查询
DEPENDENT UNION UNION 中的第二个或后面的查询语句, 取决于外面的查询
UNION RESULT UNION 的结果
SUBQUERY 子查询中的第一个 SELECT
DEPENDENT SUBQUERY 子查询中的第一个 SELECT,取决于外面的查询。子查询依赖于外层查询的结果
MATERIALIZED Materialized subquery
table
表示查询涉及的表或衍生表 。 这也可以是以下值之一:

<unionM,N>:该行指的是具有和id值的行 的 M并集 N。
:该行是指用于与该行的派生表结果id的值 N。派生表可能来自FROM子句中的子查询 。
:该行是指该行的物化子查询的结果,其id 值为N。
partitions
查询将匹配记录的分区。该值适用NULL于未分区的表。

type
联接类型。 提供了判断查询是否高效的重要依据依据。通过 type 字段,我们判断此次查询是全表扫描还是索引扫描等。 从最佳类型到最差类型:

system: 该表只有一行(=系统表)。这是const联接类型的特例 。

const: 针对主键或唯一索引的等值查询扫描,最多只返回一行数据。const 查询速度非常快,因为它仅仅读取一次即可 。

SELECT * FROM tbl_name WHERE primary_key=1;

SELECT * FROM tbl_name
WHERE primary_key_part1=1 AND primary_key_part2=2;
eq_ref: 此类型通常出现在多表的 join 查询,表示对于前表的每一个结果,都只能匹配到后表的一行结果。并且查询的比较操作通常是 =,查询效率较高

SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;

SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;
ref : 此类型通常出现在多表的 join 查询,针对于非唯一或非主键索引,或者是使用了最左前缀规则索引的查询。ref可以用于使用=或<=> 运算符进行比较的索引列。

SELECT * FROM ref_table WHERE key_column=expr;

SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;

SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;
ref_or_null: 这种连接类型类似于 ref,但是除了MySQL会额外搜索包含NULL值的行。此联接类型优化最常用于解析子查询。

SELECT * FROM ref_table
WHERE key_column=expr OR key_column IS NULL;
unique_subquery: 只是一个索引查找函数,它完全替代了子查询以提高效率。

value IN (SELECT primary_key FROM single_table WHERE some_expr)
index_subquery:此连接类型类似于 unique_subquery。它代替IN子查询,但适用于以下形式的子查询中的非唯一索引。

range: 表示使用索引范围查询, 通过索引字段范围获取表中部分数据记录。这个类型通常出现在 =,<>,>,>=,<,<=,IS NULL,<=>,BETWEEN,IN() 操作中。

当 type 是 range 时,那么 EXPLAIN 输出的 ref 字段为 NULL,并且 key_len 字段是此次查询中使用到的索引的最长的那个 。

SELECT * FROM tbl_name
WHERE key_column = 10;

SELECT * FROM tbl_name
WHERE key_column BETWEEN 10 and 20;

SELECT * FROM tbl_name
WHERE key_column IN (10,20,30);

SELECT * FROM tbl_name
WHERE key_part1 = 10 AND key_part2 IN (10,20,30);
index: 表示全索引扫描(full index scan)和 ALL 类型类似,只不过 ALL 类型是全表扫描,而 index 类型则仅仅扫描所有的索引,而不扫描数据。

index 类型通常出现在: 所要查询的数据直接在索引树中就可以获取到,而不需要扫描数据。当是这种情况时,Extra 字段 会显示 Using index

ALL: 表示全表扫描,这个类型的查询是性能最差的查询之一。

我们的查询不应该出现 ALL 类型的查询,因为这样的查询在数据量大的情况下,对数据库的性能是巨大的灾难。如一个查询是 ALL 类型查询,那么一般来说可以对相应的字段添加索引来避免 。

possible_keys
表示 MySQL 在查询时,能够使用到的索引。

即使有些索引在 possible_keys 中出现,但是并不表示此索引会真正地被 MySQL 使用到。MySQL 在查询时具体使用了哪些索引,由 key 字段决定。

key
是 MySQL 在当前查询时所真正使用到的索引。

key_len
表示查询优化器使用了索引的字节数。

这个字段可以评估组合索引是否完全被使用,或只有最左部分字段被使用到。key_len 的计算规则如下:

字符串
char(n): n 字节长度
varchar(n): 如果是 utf8 编码, 则是 3n + 2字节; 如果是 utf8mb4 编码, 则是 4n + 2 字节
数值类型
TINYINT: 1字节
SMALLINT: 2字节
MEDIUMINT: 3字节
INT: 4字节
BIGINT: 8字节
时间类型
DATE: 3字节
TIMESTAMP: 4字节
DATETIME: 8字节
字段属性: NULL 属性 占用一个字节。如果一个字段是 NOT NULL 的, 则没有此属性
rows
查询优化器根据统计信息,估算 SQL 要查找到结果集需要扫描读取的数据行数。这个值非常直观显示 SQL 的效率好坏,原则上 rows 越少越好。

这个 rows 就是 mysql 认为必须要逐行去检查和判断的记录的条数。举个例子来说,假如有一个语句 select * from t where column_a = 1 and column_b = 2; 全表假设有 100 条记录,column_a 字段有索引(非联合索引),column_b没有索引。column_a = 1 的记录有 20 条, column_a = 1 and column_b = 2 的记录有 5 条。

Extra
EXplain 中的很多额外的信息会在 Extra 字段显示,常见的有以下几种内容:

Using filesort:当 Extra 中有 Using filesort 时,表示 MySQL 需额外的排序操作,不能通过索引顺序达到排序效果。一般有 Using filesort,都建议优化去掉,因为这样的查询 CPU 资源消耗大。
Using index:“覆盖索引扫描”,表示查询在索引树中就可查找所需数据,不用扫描表数据文件,往往说明性能不错
Using temporary:查询有使用临时表,一般出现于排序,分组和多表 join 的情况,查询效率不高,建议优化
Using where: WHERE子句用于限制哪些行与下一个表匹配或发送给客户端 。
得出结论
说到最后,那 WHERE column_index in (expr) 到底走不走索引呢? 答案是不确定的。

走不走索引是由 expr 来决定的,不是一概而论走还是不走。

SELECT * FROM a WHERE id in (1,23,456,7,8)
– id 是主键,查询是走索引的。type = range,key = PRIMARY
SELECT * FROM a WHERE id in (SELECT b.a_id FROM b WHERE some_expr)
– id 是主键,如果 some_expr 是一个索引查询,那么 select a 将走索引;
– some_expr 不是索引查询,那么 select a 将全表扫描;
上面是两个通用案例,但到底对不对了,还是自己去实践最好了,拿起EXPLAIN去剖析吧~
https://giphy.com/channel/9i8z5z
https://giphy.com/channel/onq3hf
https://giphy.com/channel/0qf65o
https://giphy.com/channel/ba8bka
https://giphy.com/channel/bovkfu
https://giphy.com/channel/5fw29x
https://giphy.com/channel/f4nx29
https://giphy.com/channel/l2uo1f
https://giphy.com/channel/f420w4
https://giphy.com/channel/d4lfov
https://giphy.com/channel/1i9hzf
https://giphy.com/channel/6ja35z
https://giphy.com/channel/01pp6c
https://giphy.com/channel/ujli5d
https://giphy.com/channel/e7c9ki
https://giphy.com/channel/ymhh7h
https://giphy.com/channel/gg2224
https://giphy.com/channel/c7dw1t
https://giphy.com/channel/vzr1cv
https://giphy.com/channel/cjb0bj
https://giphy.com/channel/k0ks86
https://giphy.com/channel/2qzz96
https://giphy.com/channel/7nogfz
https://giphy.com/channel/o1ie3q
https://giphy.com/channel/9yi2p4
https://giphy.com/channel/2xwvn9
https://giphy.com/channel/s8gt9i
https://giphy.com/channel/1eoe6e
https://giphy.com/channel/f8dyy0
https://giphy.com/channel/kuv0dl
https://giphy.com/channel/3g6n9q
https://giphy.com/channel/vn57uu
https://giphy.com/channel/20bfc8
https://giphy.com/channel/k2cg31
https://giphy.com/channel/ya1638
https://giphy.com/channel/se4kma
https://giphy.com/channel/t40jat
https://giphy.com/channel/h32kxq
https://giphy.com/channel/qyb82t
https://giphy.com/channel/ek64be
https://giphy.com/channel/rk9ui0
https://giphy.com/channel/801r2l
https://giphy.com/channel/6giqwj
https://giphy.com/channel/oney1m
https://giphy.com/channel/3860s1
https://giphy.com/channel/a6jiij
https://giphy.com/channel/32tart
https://giphy.com/channel/j04u01
https://giphy.com/channel/3gozp4
https://giphy.com/channel/wdbcj7
https://giphy.com/channel/s981ls
https://giphy.com/channel/60kwme
https://giphy.com/channel/0m2dx3
https://giphy.com/channel/1vwqfe
https://giphy.com/channel/yg18ow
https://giphy.com/channel/nr3v3w
https://giphy.com/channel/jb59cb
https://giphy.com/channel/qzj8ka
https://giphy.com/channel/949277
https://giphy.com/channel/x8z9g1
https://giphy.com/channel/ar2ksv
https://giphy.com/channel/8iz0yg
https://giphy.com/channel/81rt3z
https://giphy.com/channel/z2su4t
https://giphy.com/channel/rsrd4m
https://giphy.com/channel/hi2zhp
https://giphy.com/channel/61ozgj
https://giphy.com/channel/jk6dkr
https://giphy.com/channel/071f1z
https://giphy.com/channel/rx3qw2
https://giphy.com/channel/0k6j06
https://giphy.com/channel/62fw2e
https://giphy.com/channel/7h5xer
https://giphy.com/channel/wf04fq
https://giphy.com/channel/y32wno
https://giphy.com/channel/uy6u44
https://giphy.com/channel/695umc
https://giphy.com/channel/2xwxwm
https://giphy.com/channel/63vz45
https://giphy.com/channel/e0lwnu
https://giphy.com/channel/3555df
https://giphy.com/channel/batykt
https://giphy.com/channel/14mnea
https://giphy.com/channel/qhq0tr
https://giphy.com/channel/6gmzez
https://giphy.com/channel/yqwxx8
https://giphy.com/channel/krk67q
https://giphy.com/channel/6rh4p7
https://giphy.com/channel/327498
https://giphy.com/channel/mck3ed
https://giphy.com/channel/9zst4s
https://giphy.com/channel/2zrpso
https://giphy.com/channel/nwumn4
https://giphy.com/channel/fwvv7k
https://giphy.com/channel/99ohqp
https://giphy.com/channel/rkcv1t
https://giphy.com/channel/pfzn9n
https://giphy.com/channel/qe03wo
https://giphy.com/channel/i1nh11
https://giphy.com/channel/73l59u
https://giphy.com/channel/3r32hi
https://giphy.com/channel/8awbm8
https://giphy.com/channel/v5yon4
https://giphy.com/channel/qasxxx
https://giphy.com/channel/sa3dmc
https://giphy.com/channel/5rpgj0
https://giphy.com/channel/6ow00m
https://giphy.com/channel/it6jdi
https://giphy.com/channel/cxf2op
https://giphy.com/channel/hu4kqc
https://giphy.com/channel/rbem7n
https://giphy.com/channel/xgakz4
https://giphy.com/channel/z56cjh
https://giphy.com/channel/wowoof
https://giphy.com/channel/czk132
https://giphy.com/channel/2dydnf
https://giphy.com/channel/z40eqe
https://giphy.com/channel/2775rh
https://giphy.com/channel/g8xlww
https://giphy.com/channel/cz7j9d
https://giphy.com/channel/89p8jr
https://giphy.com/channel/p0zcqb
https://giphy.com/channel/num70j
https://giphy.com/channel/oixa9i
https://giphy.com/channel/6n8dgh
https://giphy.com/channel/4asyig
https://giphy.com/channel/sh1kx3
https://giphy.com/channel/tc8kja
https://giphy.com/channel/cdasm8
https://giphy.com/channel/m20ggl
https://giphy.com/channel/qt3aa8
https://giphy.com/channel/ge870v
https://giphy.com/channel/9o0v76
https://giphy.com/channel/nm530u
https://giphy.com/channel/0w5ka3
https://giphy.com/channel/jytl1k
https://giphy.com/channel/6ck26l
https://giphy.com/channel/y774hq
https://giphy.com/channel/xdpxnm
https://giphy.com/channel/pg9607
https://giphy.com/channel/e5cm4t
https://giphy.com/channel/joyp15
https://giphy.com/channel/72h6xr
https://giphy.com/channel/89y5gz
https://giphy.com/channel/u3s1r6
https://giphy.com/channel/0ocoef
https://giphy.com/channel/i4318r
https://giphy.com/channel/ccfoo4
https://giphy.com/channel/ex93gm
https://giphy.com/channel/wu85u7
https://giphy.com/channel/a3xhx5
https://giphy.com/channel/f7ywog
https://giphy.com/channel/iurjcj
https://giphy.com/channel/kdtq9d
https://giphy.com/channel/k74l84
https://giphy.com/channel/2ronxg
https://giphy.com/channel/zqxkhi
https://giphy.com/channel/053zir
https://giphy.com/channel/p68zzx
https://giphy.com/channel/bd638s
https://giphy.com/channel/xaqfho
https://giphy.com/channel/zhhzbh
https://giphy.com/channel/zyh82u
https://giphy.com/channel/kuhl1t
https://giphy.com/channel/89q0oa
https://giphy.com/channel/8g5zjr
https://giphy.com/channel/hsjz05
https://giphy.com/channel/59m168
https://giphy.com/channel/m542vh
https://giphy.com/channel/o3wpfr
https://giphy.com/channel/9moude
https://giphy.com/channel/esle66
https://giphy.com/channel/zy9yq0
https://giphy.com/channel/el782v
https://giphy.com/channel/4po0aa
https://giphy.com/channel/j5kjqk
https://giphy.com/channel/a52ihi
https://giphy.com/channel/dmxo2f
https://giphy.com/channel/pn3eh9
https://giphy.com/channel/m2f4xh
https://giphy.com/channel/eef3el
https://giphy.com/channel/t7jtm7
https://giphy.com/channel/zq493o
https://giphy.com/channel/cb98sf
https://giphy.com/channel/nngxfz
https://giphy.com/channel/tt510y
https://giphy.com/channel/tyqjr0
https://giphy.com/channel/654mee
https://giphy.com/channel/d9gpdf
https://giphy.com/channel/cce7do
https://giphy.com/channel/2c5spr
https://giphy.com/channel/6b84ch
https://giphy.com/channel/05uvd6
https://giphy.com/channel/0uulmj
https://giphy.com/channel/ml68dk
https://giphy.com/channel/kttcb5
https://giphy.com/channel/45vjre
https://giphy.com/channel/5qaeyi
https://giphy.com/channel/ybihxp
https://giphy.com/channel/n80mod
https://giphy.com/channel/dplul5
https://giphy.com/channel/oe0f4y
https://giphy.com/channel/71dv73
https://giphy.com/channel/260d8u
https://giphy.com/channel/5ueuvv
https://giphy.com/channel/qxih9p
https://giphy.com/channel/kkstie
https://giphy.com/channel/s5agyr
https://giphy.com/channel/1x97hx
https://giphy.com/channel/0ysj4s
https://giphy.com/channel/c68lc8
https://giphy.com/channel/mdjvnn
https://giphy.com/channel/58k2vv
https://giphy.com/channel/rzosri
https://giphy.com/channel/fxf1l1
https://giphy.com/channel/y1kkgg
https://giphy.com/channel/93xk66
https://giphy.com/channel/8j53gg
https://giphy.com/channel/j87bal
https://giphy.com/channel/uwonpc
https://giphy.com/channel/732bii
https://giphy.com/channel/cwdg2f
https://giphy.com/channel/o9hez9
https://giphy.com/channel/2az1gx
https://giphy.com/channel/ym6pfd
https://giphy.com/channel/a7hq78
https://giphy.com/channel/y198qs
https://giphy.com/channel/ztlv8k
https://giphy.com/channel/qyg21k
https://giphy.com/channel/9qjpyr
https://giphy.com/channel/bkg8o7
https://giphy.com/channel/bxrsx5
https://giphy.com/channel/m670mi
https://giphy.com/channel/99wwdf
https://giphy.com/channel/4h88a5
https://giphy.com/channel/15pr7q
https://giphy.com/channel/11bbww
https://giphy.com/channel/tnwfsk
https://giphy.com/channel/nyomf6
https://giphy.com/channel/hzv3nq
https://giphy.com/channel/t3srlu
https://giphy.com/channel/p5skr3
https://giphy.com/channel/399juj
https://giphy.com/channel/uta4d2
https://giphy.com/channel/72egme
https://giphy.com/channel/f37v2y
https://giphy.com/channel/xmd1nc
https://giphy.com/channel/h7vg43
https://giphy.com/channel/j4jzuu
https://giphy.com/channel/qqcz8r
https://giphy.com/channel/i1kc36
https://giphy.com/channel/56p1hq
https://giphy.com/channel/f6n2n0

MySQL查询优化利刃-EXPLAIN相关推荐

  1. MySQL查询优化之explain的深入解析

    在分析查询性能时,考虑EXPLAIN关键字同样很管用.EXPLAIN关键字一般放在SELECT查询语句的前面,用于描述MySQL如何执行查询操作.以及MySQL成功返回结果集需要执行的行数.expla ...

  2. MySQL 优化之 EXPLAIN 关键字

    MySQL查询优化之explain的深入解析 0. 准备 首先执行如下的 sql 语句: CREATE TABLE IF NOT EXISTS `article` (`id` int(10) unsi ...

  3. MySQL查询优化系列文章

    MySQL查询优化之explain的深入解析 mysql嵌套查询和联表查询优化方法 MySQL查询优化:LIMIT 1避免全表扫描提高查询效率 Mysql使用索引实现查询优化 mysql数据库查询优化 ...

  4. MySQL高级-索引是个什么东西?explain到底怎么用-MySQL查询优化大全

    目录 一.引出问题-MySQL的查询优化: 二.性能下降的原因: 三.索引到底是什么?怎么用? 1.索引操作 查看索引: 删除索引: 创建索引: 说明: 索引命名规范: 2.索引优势: 3.索引劣势: ...

  5. MySQL查询优化-explain

    2019独角兽企业重金招聘Python工程师标准>>> 一.MySQL 查询优化器是如何工作的         MySQL 查询优化器有几个目标,但是其中最主要的目标是尽可能地使用索 ...

  6. mysql查询优化explain命令详解

    转载自 mysql查询优化explain命令详解 mysql查询优化的方法有很多种,explain是工作当中用的比较多的一种检查方式.explain翻译即解释,就是看mysql语句的查询解释计划,从解 ...

  7. mysql explain output_MySQL查询优化之explain的深入解析【转载】

    在分析查询性能时,考虑EXPLAIN关键字同样很管用.EXPLAIN关键字一般放在SELECT查询语句的前面,用于描述MySQL如何执行查询操作.以及MySQL成功返回结果集需要执行的行数.expla ...

  8. mysql 查询结果怎么解读_MySQL查询优化之explain的深入解析

    在分析查询性能时,考虑EXPLAIN关键字同样很管用.EXPLAIN关键字一般放在SELECT查询语句的前面,用于描述MySQL如何执行查询操作.以及MySQL成功返回结果集需要执行的行数.expla ...

  9. mysql中的EXPLAIN

    执行计划就是sql的执行查询的顺序,以及如何使用索引查询,返回的结果集的行数 EXPLAIN SELECT * from A where X=? and Y=? id :是一个有顺序的编号,是查询的顺 ...

最新文章

  1. 13委托和事件在观察者模式中的应用
  2. php开发app接口教学,php开发App接口
  3. ubuntu apache2配置详解(含虚拟主机配置方法)
  4. pythonrequests下载大文件_Python3 使用requests模块显示下载大文件显示进度
  5. 使用FFmpeg进行视频抽取音频,之后进行语音识别转为文字
  6. 华为海思MPP媒体处理软件开发学习(基础)
  7. mysql索引有几种使用索引的好处_mysql索引的类型和优缺点
  8. 程序员的职业生涯之我见
  9. winform中listView
  10. 软件设计师 -主观题总结
  11. 像QQ一样输入表情图像
  12. byte 转 int 为什么要0xFF?
  13. HNCU1324:算法2-2:有序线性表的有序合并(线性表)
  14. 高性能服务器中的C10K问题
  15. 【转】如何把Matlab中的m文件转化成C语言代码
  16. 鸿蒙生态发布会,新日XC3亮相华为鸿蒙生态大会,这场合作值得期待!
  17. Win11 在线安装QT5.15.2教程
  18. python词云词频分析_Python词云(词频统计,掩膜显示)
  19. dns 性能测试 dnsperf
  20. Win 10 下无法安装.net framework 3.5,错误代码0x800F081F的解决方案

热门文章

  1. 1byte、1KB、4KB,1MB、1GB用16进制表示的范围。任意地址范围求字节数
  2. 关于对比损失(contrasive loss)的理解(相似度越大越相似的情况):
  3. css如何去掉或修改浏览器默认滚动条
  4. 论文阅读_对比学习_SimCSE
  5. box2d 碰撞检测_Box2d新系列 第四章 碰撞模块
  6. ESP UART 介绍
  7. 单元测试|unittest生成测试报告
  8. 包围盒----碰撞检测
  9. 大型项目前端架构浅谈(8000字原创首发)
  10. strcat函数用法的一点看法