MYSQL的索引优化
2018-02-28 00:00
155 查看
当一个表的数据量较大时,我们需要对这个表做优化,除了分表分库以外,最常见的就是索引优化了,那做索引优化的原则是什么呢?
在不考虑排序,分组时,也就是SQL语句中只有where的时候,多列并查如
select * from payment where staff_id=? and customer_id=?
的索引原则,谁的数量多,把谁作为最左索引,最左索引在MySQL的B+树结构里的位置是很重要的。
select count(distinct staff_id)/count(*) staff_id_selectivity,count(disctinct customer_id)/count(*) customer_id_selectivity,count(*) from payment\G
加入运行结果为 staff_id_selectivity:0.0001
customer_id_selectivity:0.0373
count(*):16049
很明显customer_id的占比大,结果为
alter table payment add key(customer_id,staff_id)
在不考虑排序,分组时,也就是SQL语句中只有where的时候,多列并查如
select * from payment where staff_id=? and customer_id=?
的索引原则,谁的数量多,把谁作为最左索引,最左索引在MySQL的B+树结构里的位置是很重要的。
select count(distinct staff_id)/count(*) staff_id_selectivity,count(disctinct customer_id)/count(*) customer_id_selectivity,count(*) from payment\G
加入运行结果为 staff_id_selectivity:0.0001
customer_id_selectivity:0.0373
count(*):16049
很明显customer_id的占比大,结果为
alter table payment add key(customer_id,staff_id)
相关文章推荐
- mysql 索引原理及慢查询优化
- 一个mysql索引顺序优化的案例
- MySQL索引原理及慢查询优化
- MySQL性能优化之索引详解
- 用MySql的查询分析语法explain来优化查询和索引
- 加速PHP动态网站 MySQL索引分析和优化
- mysql sql优化和索引摘录
- mysql优化之索引建立的规则
- mysql索引提高优化order by语句用法介绍
- MySQL查询优化技术系列讲座之使用索引
- mysql结构优化--索引优化
- mysql优化-索引
- 理解MySQL——索引与优化
- MySQL优化 索引
- MySQL索引与优化
- 由浅入深探究mysql索引结构原理、性能分析与优化
- Mysql 索引优化
- Mysql索引详解及优化(key和index区别)
- MySQL详解(10)----------索引优化
- 提高mysql千万级大数据SQL查询优化30条经验(Mysql索引优化注意)