多路平衡查找树
动态构建B树
裂变
所有元素都会出现在叶子节点
非叶子节点起到索引作用
叶子形成了一个单向链表。
mysql在原B+树基础上,增加了一个指向相邻节点的链表指针。
一个页默认16k
只能有一个
必须有,只能有一个
叶子节点挂的数据就是一行的数据
叶子节点挂的是id值。
如果有主键,使用主键;
如果没有主键,用第一个唯一索引;
没有主键,生成要给rowid;
根据二级索引找到主键值,再根据聚集索引拿到这一行的数据。
select * from user where id=10;
select * from user where name='Ann';
//先扫描name字段索引,再扫描聚集索引
innodb指针占用6个字节,假设主键使用Bigint,每个用8字节。
n*8+(n+1)6=161024
高度为2,n等于1170;
高度为3,可以存储两千多万数据。
单列索引 /联合索引
show index from table_name;
drop index index_name on table_name;
实操
select * from tb_user;
1 name,该字段可能重复,常见索引
2 phone 非空,唯一,创建唯一索引
3 profession/age/status创建联合索引
4 email创建合适索引提高查询效率
show index from tb_user;
show index from tb_user\G;
create index idx_user_name on tb_user(name);
show index from tb_user;
create unique index idx_user_phone on tb_user(phone);
create index idx_user_pro_ages_sta on tb_user(profession,age,status);
//创建联合索引顺序有讲究。
create index idx_user_email on tb_user(email);