聚集索引与辅助索引
create table t3(
id int primary key auto_increment,
name varchar(16),
age int
)engine=innodb;
insert into t3(name,age) values
(“egon”,18),
(“tom”,18),
(“lili”,18);
±—±-----±-----+
| id | name | age |
±—±-----±-----+
| 1 | egon | 18 | 1:| 1 | egon | 18 |
| 2 | tom | 18 | 2:| 2 | tom | 18 |
| 3 | lili | 18 | 3:| 3 | lili | 18 |
±—±-----±-----+
select * from t3 where id = 2;
±—±-----±-----+
| id | name | age |
±—±-----±-----+
| 1 | egon | 18 | “egon”: 1
| 2 | tom | 18 | “tom”: 2
| 3 | lili | 18 | “lili”:3
±—±-----±-----+
create index idx_name on t3(name);
select * from t3 where name=“egon”;
innodb存储引擎索引分类:
1、hash索引:更适合等值查询,不适合范围查询
2、B+树索引
聚集索引/聚簇索引-》以主键字段的值作为key创建的索引(一张表中只有一个,通常应该是id字段)
辅助索引:针对非主键字段创建的索引(一张表中可以有多个)
聚集索引与辅助索引的异同
相同点:
都是B+树结构,意味着非叶子节点只放key
而叶子节点放key:value
不同之处
聚集索引叶子节点key对应的那个value值是一整行完整的记录
辅助索引叶子节点key对应的那个value值是其所对应的主键字段值
覆盖索引:在当前你命中的索引结构中就能能到你想要的所有数据,称之为覆盖了索引
select * from t3 where id = 3; -- 覆盖了索引
select name,id from t3 where name="egon"; -- 覆盖了索引
回表操作/查询:在当前你命中的索引结构中不能拿到你想要的所有数据,那么需要根据其
对应的主键字段去聚集索引里重新查一遍,直到拿到完整记录,称之为回表操作
select name,id,age from t3 where name="egon";