查看explain中的key_len判断究竟用了哪个索引


本文摘自PHP中文网,作者藏色散人,侵删。

查看explain中的key_len判断究竟使用了哪个索引?

在一张表里有多个索引 , 我们where字段里条件有多个索引的值 , 那么究竟使用的哪个呢?

推荐:《mysql视频教程》

我们可以使用explain来查看 , 其中的key_len字段可以看得出来

比如下面这条sql

1

2

3

4

5

6

7

explain select * from ent_calendar_diary where email='xxxx' and diary_id=1784;

  

+----+-------------+--------------------+------------+-------+-------------------------+---------+---------+-------+------+----------+-------+

| id | select_type | table              | partitions | type  | possible_keys           | key     | key_len | ref   | rows | filtered | Extra |

+----+-------------+--------------------+------------+-------+-------------------------+---------+---------+-------+------+----------+-------+

|  1 | SIMPLE      | ent_calendar_diary | NULL       | const | PRIMARY,idx_email_stime | PRIMARY | 4       | const |    1 |   100.00 | NULL  |

+----+-------------+--------------------+------------+-------+-------------------------+---------+---------+-------+------+----------+-------+

possible_keys里面有两个索引字段 , 但是看key_len 是4个字节

1ed1d228bb215f4af3b35d0543c7cc3.png

备注,key_len 只指示了WHERE中用于条件过滤时被选中的索引列,是不包含 ORDER BY/GROUP BY

int类型并且not null 是4个字节 , 因此上面的sql是使用的主键索引

1

2

3

4

5

6

explain select * from ent_calendar_diary where email='xxxx';

+----+-------------+--------------------+------------+------+-----------------+-----------------+---------+-------+------+----------+-------+

| id | select_type | table              | partitions | type | possible_keys   | key             | key_len | ref   | rows | filtered | Extra |

+----+-------------+--------------------+------------+------+-----------------+-----------------+---------+-------+------+----------+-------+

|  1 | SIMPLE      | ent_calendar_diary | NULL       | ref  | idx_email_stime | idx_email_stime | 767     | const |  111 |   100.00 | NULL  |

+----+-------------+--------------------+------------+------+-----------------+-----------------+---------+-------+------+----------+-------+

这个是767个字节 , varchar(255) not null 255 * 3 +2正好符合 , 因此是使用的email那个普通索引

1

2

3

4

5

6

7

8

9

10

CREATE TABLE `ent_calendar_diary` (

`diary_id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`email` varchar(255) NOT NULL,

`title` varchar(100) NOT NULL,

`summary` varchar(500) NOT NULL DEFAULT '',

`stime` bigint(11) NOT NULL DEFAULT '0',

`ctime` int(10) unsigned NOT NULL DEFAULT '0',

PRIMARY KEY (`diary_id`),

KEY `idx_email_stime` (`email`,`stime`)

) ENGINE=InnoDB AUTO_INCREMENT=1809 DEFAULT CHARSET=utf8

以上就是查看explain中的key_len判断究竟用了哪个索引的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

查看explain中的key_len判断究竟用了哪个索引

详解 mysql 5.7 优化:explain 执行计划

mysql中explain的使用以及性能分析

在mysql中的explain中的using where和using index

mysql中explain用法和结果分析(详解)

mysql中explain作用详解

mysql中explain作用

mysql数据库性能优化神器――explain关键字

关于mysql explain中key_len的计算方法讲解

mysql explain的作用是什么?

更多相关阅读请进入《explain》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...