当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql数据库优化之 如何选择合适的列建立索引

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

2019年04月27日  | 移动技术网IT编程  | 我要评论
1. 在where 从句,group by 从句,order by 从句,on 从句中出现的列; 2. 索引字段越小越好; 3. 离散度大的列放到联合索引的前面;比如: select * from payment where staff_id = 2 and customer_id = 236; 针 ...

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 表名

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网