當一個表的數據量較大時,咱們須要對這個表作優化,除了分表分庫之外,最多見的就是索引優化了,那作索引優化的原則是什麼呢?優化
在不考慮排序,分組時,也就是SQL語句中只有where的時候,多列並查如排序
select * from payment where staff_id=? and customer_id=?索引
的索引原則,誰的數量多,把誰做爲最左索引,最左索引在MySQL的B+樹結構裏的位置是很重要的。it
select count(distinct staff_id)/count(*) staff_id_selectivity,count(disctinct customer_id)/count(*) customer_id_selectivity,count(*) from payment\Gtable
加入運行結果爲 staff_id_selectivity:0.0001select
customer_id_selectivity:0.0373數據
count(*):16049tab
很明顯customer_id的佔比大,結果爲di
alter table payment add key(customer_id,staff_id)vi