温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-03-30 16:28:44 来源:亿速云 阅读:209 作者:小新 栏目:MySQL数据库

如何查看explain中的key_len判断究竟用了哪个索引?很多人都不太了解,今天小编为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。

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

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

比如下面这条sql

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个字节

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

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

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

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那个普通索引

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判断究竟用了哪个索引就分享到这里了,当然并不止以上和大家分析的办法,不过小编可以保证其准确性是绝对没问题的。希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI