InnoDB默认使用B+Tree数据结构
InnoDB主键索引是聚集索引 :即叶子节点包含完整的数据记录
因为B+Tree的特性,所以建表时,必须建主键,并推荐主键为整形自增,可以减少运算,提高性能;
索引类型可以选择Hash,速度并不慢,原理跟java的HashMap相似,只是不可范围查找,并有hash冲突;
联合索引数据结构
联合索引是根据顺序从小到大排序的,排完第一个排第二个,依次排序,所以,如果你条件里没有第一个条件,则不走索引,此为最左原则;
建表时注意事项:
1、建主键,并推荐主键为整形自增
2、varchar default '',不要使用NULL,下图可见NULL是占空间的,并且count时不会包括null行
3、字段长度贴近现实使用的长度
4、索引要建在同时满足下面条件的字段上
Ⅰ、值非大量重复
Ⅱ、常在where,group by ,order by 中使用
线上优化
1、大表索引添加
#先创建临时表t_sys_test_temp
create table t_sys_test_temp like t_sys_test;
#添加索引
alter table t_sys_test_temp add index age_index(age);#往临时表里添加数据
INSERT into t_sys_test_temp(id,name,age,card_id,address,create_time,update_time,manager_id,deleted,sex)
select id,name,age,card_id,address,create_time,update_time,manager_id,deleted,sex from t_sys_test#重命名表
rename table t_sys_test to temp2 ,t_sys_test_temp to t_sys_test ;
drop table temp2;
未完待续。。。