问题: mysql 执行计划查看,走索引的一个疑问?
描述:

先上表结构:
CREATE TABLE table (
xxx varchar(20) NOT NULL,
yyy varchar(20) NOT NULL,
zzz datetime NOT NULL,
aaa varchar(10) NOT NULL,
PRIMARY KEY (xxx,yyy,zzz),
KEY base (xxx,yyy)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-------------------------------------------
先看一下表的索引,
执行 show index from table

Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment
table 0 PRIMARY 1 xxx A 1012 NULL NULL BTREE
table 0 PRIMARY 2 yyy A 1012 NULL NULL BTREE
table 0 PRIMARY 3 zzz A 11134 NULL NULL BTREE
table 1 base 1 xxx A 1012 NULL NULL BTREE
table 1 base 2 yyy A 1012 NULL NULL BTREE

-------------------------------------------

问题来了:

explain select xxx from table where zzz='2017-12-22 00:00:00'

执行计划如下:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE quote_kline_1day index NULL base 164 NULL 11123 Using where; Using index

为什么用了base这个索引,base是xxx和yyy的联合非聚集索引,我where条件是zzz,为什么会走base索引呢?

------------------华丽的分割线----------------------

explain select xxx from table where xxx='whatever'

执行计划如下:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE quote_kline_1day ref PRIMARY,base PRIMARY 82 const 29 Using where; Using index

为什么这里的extra会有using where,应该是只有using index才对啊,因为where条件是xxx,是联合聚集索引的前导列,而且select的是xxx,应该是索引覆盖的,不需要回表的

1 2 下一页
上一篇mysql 字段是json格式如何做 IN 查询?
下一篇pdo执行多行sql语句时的问题
明星图片
相关文章
《 mysql 执行计划查看,走索引的一个疑问?》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)