1、在where从句、group by 从句、order by从句、on从句中出现的列
2、索引字段的长度越小越好
索引长度直接影响索引文件的大小,影响增删改的速度,并间接影响查询速度(占用内存多).
针对列中的值,从左往右截取部分,来建索引
a:截的越短, 重复度越高,区分度越小, 索引效果越不好
b:截的越长, 重复度越低,区分度越高, 索引效果越好,但带来的影响也越大–增删改变慢,并间影响查询速度.
所以, 要在 区分度 + 长度 两者上,取得一个平衡.
3、离散度大的列放到联合索引的前面
select * from payment where staff_id = 2 and customer_id =584;
是index(staff_id,customer_id)好?还是index(customer_id,staff_id)好
由于customer_id 的离散度跟大,所以应该使用index(customer_id,staff_id)
select count(distinct customer_id),count(distinct staff_id) from payment;
查询出来的两个列的唯一值,值越大说明离散度越高
https://blog.csdn.net/maikelsong/article/details/55252346