对于结构相同的两张表,其中一个有主键,另一个没有主键,但都具有相同的其他索引,使用explain的type结果不同
CREATE TABLE tblA(
id int primary key not null auto_increment,
age INT,
birth TIMESTAMP NOT NULL,
name varchar(200)
);
CREATE INDEX idx_A_ageBirth ON tblA(age,birth,name);
CREATE TABLE tblB(
id int ,
age INT,
birth TIMESTAMP NOT NULL,
name varchar(200)
);
CREATE INDEX idx_B_ageBirth ON tblB(age,birth,name);
explain select * from tblA where age>20
explain select * from tblB where age>20
结果为第一条的type为index,第二条的type为range
为什么第一条的type为index?
两个表的聚合索引都一样,包含age、birth、name,且你的查询条件是age,所以查询的时候是用到了聚合索引,但是你的id第一张表是主键,第二张表是普通字段,而你的select *是查询所有数据。你的第一个查询的字段都有索引,而第二个查询id没有索引,你把第二个查询的select * 改成select age,brirth,name试试就知道了