百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>数据库> 查看explain中的key_len推断毕竟用了哪个索引
分享文章到:

查看explain中的key_len推断毕竟用了哪个索引

发布时间:09/01 来源:未知 浏览: 关键词:
查看explain中的key_len推断毕竟使用了哪个索引?

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

引荐:《mysql视频教程》

我们可以使用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个字节

1ed1d228bb215f4af3b35d0543c7cc3.png

备注,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推断毕竟用了哪个索引的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

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

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

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

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板