Mysql 索引优化最佳实践。全值匹配、最左前缀、不在索引列使用函数、覆盖索引等。
示例表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
CREATE TABLE `employees` (
3 `id` int(11) NOT NULL AUTO_INCREMENT,
4 `name` varchar(24) NOT NULL DEFAULT '' COMMENT '姓名',
5 `age` int(11) NOT NULL DEFAULT '0' COMMENT '年龄',
6 `position` varchar(20) NOT NULL DEFAULT '' COMMENT '职位',
7 `hire_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入职时间',
8 PRIMARY KEY (`id`),
9 KEY `idx_name_age_position` (`name`,`age`,`position`) USING BTREE
10 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='员工记录表';
11
12 INSERT INTO employees(name,age,position,hire_time) VALUES('LiLei',22,'manager',NOW());
13 INSERT INTO employees(name,age,position,hire_time) VALUES('HanMeimei',
23,'dev',NOW());
14 INSERT INTO employees(name,age,position,hire_time) VALUES('Lucy',23,'dev',NOW());
|
全值匹配
1
|
EXPLAIN SELECT * FROM employees WHERE name= 'LiLei';
|
1
|
EXPLAIN SELECT * FROM employees WHERE name= 'LiLei' AND age = 22;
|
1
2
|
EXPLAIN SELECT * FROM employees WHERE name= 'LiLei' AND age = 22 AND position ='manage
r';
|
最左前缀
如果索引了多列,要遵守最左前缀法则。指的是查询从索引的最左前列开始并且不跳过索引中的列。
1
2
3
|
EXPLAIN SELECT * FROM employees WHERE name = 'Bill' and age = 31;
EXPLAIN SELECT * FROM employees WHERE age = 30 AND position = 'dev';
EXPLAIN SELECT * FROM employees WHERE position = 'manager';
|
不要在索引列上做函数计算
不在索引列上做任何操作(计算、函数、(自动or手动)类型转换),会导致索引失效而转向全表扫描**
1
2
|
EXPLAIN SELECT * FROM employees WHERE name = 'LiLei'
EXPLAIN SELECT * FROM employees WHERE left(name,3) = 'LiLei';
|
给hire_time增加一个普通索引:
1
2
|
ALTER TABLE `employees` ADD INDEX `idx_hire_time` (`hire_time`) USING BTREE ;
EXPLAIN select * from employees where date(hire_time) ='2018‐09‐30';
|
转化为日期范围查询,有可能会走索引:
1
2
|
EXPLAIN select * from employees where hire_time >='2018‐09‐30 00:00:00' and hire_time <
='2018‐09‐30 23:59:59';
|
还原最初索引状态:
1
|
ALTER TABLE `employees` DROP INDEX `idx_hire_time`;
|
存储引擎不能使用范围条件右边的列
存储引擎不能使用索引中范围条件右边的列
1
2
3
4
|
EXPLAIN SELECT * FROM employees WHERE name= 'LiLei' AND age = 22 AND position ='manage
r';
EXPLAIN SELECT * FROM employees WHERE name= 'LiLei' AND age > 22 AND position ='manage
r';
|
覆盖索引
尽量使用覆盖索引(只访问索引的查询(索引列包含查询列)),减少 select * 语句。
1
2
|
EXPLAIN SELECT name,age FROM employees WHERE name= 'LiLei' AND age = 23 AND position
='manager';
|
1
2
|
EXPLAIN SELECT * FROM employees WHERE name= 'LiLei' AND age = 23 AND position ='manage
r';
|
避免索引失效的判断条件
.mysql在使用不等于(!=或者<>),not in ,not exists 的时候无法使用索引会导致全表扫描
< 小于、 > 大于、 <=、>= 这些,mysql内部优化器会根据检索比例、表大小等多个因素整体评估是否使用索引。
1
|
EXPLAIN SELECT * FROM employees WHERE name != 'LiLei';
|
避免is Null,is Not Null
is null,is not null 一般情况下也无法使用索引
1
|
EXPLAIN SELECT * FROM employees WHERE name is null
|
模糊搜索尽量避免左侧通配符
like以通配符开头(’$abc…’)mysql索引失效会变成全表扫描操作
1
|
EXPLAIN SELECT * FROM employees WHERE name like '%Lei'
|
1
|
EXPLAIN SELECT * FROM employees WHERE name like 'Lei%'
|
问题:解决like’%字符串%‘索引不被使用的方法?
1
|
EXPLAIN SELECT name,age,position FROM employees WHERE name like '%Lei%';
|
字符串不加单引号索引失效
1
2
|
EXPLAIN SELECT * FROM employees WHERE name = '1000';
EXPLAIN SELECT * FROM employees WHERE name = 1000;
|
少用or或in
少用or或in,用它查询时,mysql不一定使用索引,mysql内部优化器会根据检索比例、表大小等多个因素整体评
估是否使用索引,详见范围查询优化
1
|
EXPLAIN SELECT * FROM employees WHERE name = 'LiLei' or name = 'HanMeimei';
|
范围查询优化
给年龄添加单值索引
1
2
|
ALTER TABLE `employees` ADD INDEX `idx_age` (`age`) USING BTREE ;
explain select * from employees where age >=1 and age <=2000;
|
没走索引原因:mysql内部优化器会根据检索比例、表大小等多个因素整体评估是否使用索引。比如这个例子,可能是
由于单次数据量查询过大导致优化器最终选择不走索引。
优化方法:可以将大的范围拆分成多个小范围
1
2
|
explain select * from employees where age >=1 and age <=1000;
explain select * from employees where age >=1001 and age <=2000;
|
还原最初索引状态
1
|
ALTER TABLE `employees` DROP INDEX `idx_age`;
|
总结
假设index(a,b,c)
where语句 |
索引是否被使用 |
where a=3 |
是,使用到a |
where a=3 and b=5 |
是,使用到a,b |
where a=3 and b=4 and c=5 |
是,使用到a,b,c |
where b=3 or (b=3 and c=4) or c=4 |
否 |
where a=3 and c=5 |
使用到a,但是c不可以,b中间断了 |
where a=3 and b>4 and c=5 |
使用到a和b,c不能在范围之后,b断了 |
where a=3 and b like ‘kk%’ and c=4 |
是,使用到a,b,c |
where a=3 and b like ‘%kk’ and c=4 |
是,只用到a |
where a=3 and b like ‘%kk%’ and c=4 |
是,只用到a |
where a=3 and b like ‘k%kk%’ and c=4 |
是,使用到a,b,c |
like KK%相当于=常量,%KK和%KK% 相当于范围
1
2
|
‐‐ mysql5.7关闭ONLY_FULL_GROUP_BY报错
select version(), @@sql_mode;SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
|