MySQL之explain的type列(二)

2014-11-24 10:55:03 · 作者: · 浏览: 1
+ 可以看出,返回结果中的字段值都以“值 AS 字段名”的形式直接出现在优化后的 select 语句中。修改一下查询: mysql> explain select * from a where id in(1,2)\G
...

type: range ... 当返回结果超过 1 条时, type 便不再为 const 了。重新建表及插入数据: create table a (id int not null); insert into a value(1),(2),(3); mysql>

explain select * from a where id=1\G
...

type: ALL 目前表中只有一条 id=1 的记录,但 type 已为 ALL ,因为只有唯一性索引才能保证表中最多只有一条记录,只有这样 type 才有可能为 const 。为 id 加普通索引后, type 变为 ref ,改为加唯一或主键索引后, type 便变为 const 了。