独立的列
一句话概括 where 子句中 条件列得统一放左边
例如 where aid+1=5 这纯属捣乱
还有 where TO_DAYS(CURRENT_DATE)-TO_DAYS(date_col)<=10;
前缀索引和索引选择性
场景应用 字符列太长 带来的索引慢的问题
对于 BLOB text 或者更长的 varchar 都是要使用前缀索引的
从两个角度分析
情景 城市名称记录
完整列的选择性
select count(*) as cnt,city from city GROUP BY city ORDER BY cnt desc limit 10;
测出 这个city列的大致分布
通过
select COUNT(*) AS cnt,LEFT(city,7) as pref FROM city GROUP BY pre ORDER BY cnt DESC LIMIT 10;
比较两个代表值是否接近
⑴ select COUNT(DISTINCT city)/COUNT(*) FROM city;
0.0312
计算不同长度选择性
⑵ select COUNT(DISTINCT LEFT(city,3))/COUNT(*) AS sel3,COUNT(DISTINCT LEFT(city,4))/COUNT(*) AS sel4,COUNT(DISTINCT LEFT(city,5))/COUNT(*) AS sel5,
COUNT(DISTINCT LEFT(city,6))/COUNT(*) AS sel6 ,COUNT(DISTINCT LEFT(city,7))/COUNT(*) AS sel7 FROM city;
结合 1 2 语句判断接近这个0.0312的均可以使用。但实际上并非如此
如select COUNT(*) AS cnt,LEFT(city,4) AS pref FROM city group by pref ORDER BY cnt DESC LIMIT 5;
sel3 | sel4 | sel5 | sel6 | sel7 |
0.0239 | 0.0293 | 0.0305 | 0.0309 | 0.0310 |
前缀索引不能用于 ORDER BY GROUP BY 也不能做覆盖扫描
关于多列索引
不要用多列独立索引
关于索引列的位置排序
经验法则: 将选择性最高的列放到索引列最前列。
select count(*),sum(groupid=10137),sum(userid=1288826),sum(anom=0) from message\G;