MySQL索引相关--demo
B树与B+树:
基本概念及对比
MySQL索引组织表,与B+树的关系
MySQL为索引组织表(Index Organized Table, IOT) ,索引即数据,数据即索引
索引创建原则:
由慢查评率决定 ----不是所有的慢查都需要优化
单表索引数目不能太多
常用慢查优化:
举例
无条件的查询
select * from …
1.1 order by c1 ----考虑在c1上创建索引
1.2 group by c1 ----考虑在c1上创建索引
1.3 group by c3 order by c4 ----考虑创建(c3, c4)
1.4 limit N ----结果不稳定,limit若要得到恒定结果,必须进行排序
1.5 不用select *,仅select相关列
有where条件
2.1 where name=‘aa’ ----考虑c1加索引
2.2 where name=‘a’ and addr=‘b’ ----考虑加(name, addr)
2.3 where name=‘a’ and num>100 ----考虑加(name, num)
2.4 where num1>100 and num2<1000 ----考虑在选择性高的列加索引
2.5 所有计算类的函数均不要出现在筛选的字段上,(尽管部分函数计算同样支持索引)
2.6 where feature like ‘aa%’ ----前缀索引
2.7 where name is null / is not null ----均不建议使用
where + sort
3.1 where name = ‘a’ order by name -----考虑在name字段加索引
3.2 where name=‘a’ order by addr -----考虑加(name, addr)
3.3 num>100 order by num ----(num)
3.4 num>100 order by name ----(num, name)无效
3.5 num>100 and addr=‘aa’ order by area ----(addr, num, area)只能使用到多列索引的前两列