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)只能使用到多列索引的前兩列