欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-02-24 14:54:12
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 表名

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