mysql数据库优化之 如何选择合适的列建立索引

1. 在where 从句,group by 从句,order by 从句,on 从句中出现的列;

2. 索引字段越小越好;

3. 离散度大的列放到联合索引的前面;比如:

    select * from payment where staff_id = 2 and customer_id = 236;

    针对上面的查询是  index(sftaff_id, customer_id) 好?还是index(customer_id, staff_id)好?

    因为customer_id的离散度更大,因此用后面的更合适!!

那么问题来了。怎么判断离散度呢,可以使用 select count(distinct customer_id), count(distinct staff_id) from 表名

谁的值大,说明这一些列的离散度更高!

posted on 2019-04-26 13:26  与落霞齐飞12138  阅读(392)  评论(0编辑  收藏  举报

导航