57

工作中遇到的99%SQL优化,这里都能给你解决方案 - 小强的进阶之路 - 博客园

 5 years ago
source link: https://www.cnblogs.com/xiaoqiang-code/p/11455879.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

工作中遇到的99%SQL优化,这里都能给你解决方案

前几篇文章介绍了mysql的底层数据结构和mysql优化的神器explain。后台有些朋友说小强只介绍概念,平时使用还是一脸懵,强烈要求小强来一篇实战sql优化,经过周末两天的整理和总结,sql优化实战新鲜出炉, 大家平时学习和工作中,遇到的90% 的sql优化都会介绍到,介意篇幅过长,分成3篇文章哈。

CREATE TABLE `employees` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(24) NOT NULL DEFAULT '' COMMENT '姓名',
  `age` int(20) NOT NULL DEFAULT '0' COMMENT '年龄',
  `position` varchar(20) NOT NULL DEFAULT '' COMMENT '职位',
  `hire_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '入职时间',
  PRIMARY KEY (`id`),
  KEY `idx_name_age_position` (`name`,`age`,`position`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='员工表';

insert into employees(name,age,position,hire_time) values('LiLei', 22, 'manager', NOW())
insert into employees(name,age,position,hire_time) values('HanMeimei', 23, 'dev', NOW())
insert into employees(name,age,position,hire_time) values('Lucy', 23, 'dev', NOW())

索引的字段类型是varchar(n):2字节存储字符串长度,如果是utf-8, 则长度是3n+2

EXPLAIN select * from employees where name='LiLei';

explain解析

EXPLAIN select * from employees where name='LiLei' AND age = 22;

explain解析

EXPLAIN select * from employees where name='LiLei' AND age = 22 AND position = 'manager';

explain解析

最左前缀法则

如果索引是多列,要最受最左前缀法则。指的是查询从索引的最左前列开始并且不跳过索引中的列。以下两条sql根据最左前缀法则,都不会走索引。

EXPLAIN select * from employees where age = 22 AND position='manager';
EXPLAIN select * from employees where position ='manager';

explain解析

不要在索引列上做任何操作(计算、函数、类型转换),会导致索引失效而转向全表扫描。

EXPLAIN select * from employees where name='LiLei';

explain解析

EXPLAIN select * from employees where left(name, 3)='LiLei';

explain解析

给hire_time增加一个普通索引:

alter table `employees` ADD INDEX `idx_hire_time`(`hire_time`) USING BTREE;
EXPLAIN select * from employees where date(hire_time) = '2019-08-25';

还原最初索引状态

ALTER TABLE `employees` DROP INDEX `idx_hire_time`;

存储引擎不能使用索引中范围条件右边的列

-- EXPLAIN SELECT * FROM employees WHERE name ='LiLei' AND age=22 AND position ='manager';
EXPLAIN SELECT * FROM employees WHERE name ='LiLei' AND age>22 AND position ='manager';

explain解析
看到key_len这个索引长度是78, 也就是只使用到了前两个字段name和age,postition没有使用到索引的。

尽量使用覆盖索引(只访问索引的查询(索引列包含查询列)),减少selelct * 语句。

EXPLAIN SELECT name,age,position FROM employees WHERE name ='LiLei' AND age=22 AND position ='manager';

explain解析

mysql在使用不等于(! = 或者 <>)的时候无法使用索引会导致全表扫描

EXPLAIN SELECT * FROM employees WHERE name !='LiLei' ;

explain解析

is null,is not null也无法使用索引

EXPLAIN SELECT * FROM employees WHERE name is null;

explain解析

like以通配符开头(‘$abc’)mysql索引失效会变成全表扫描操作

EXPLAIN SELECT * FROM employees WHERE name LIKE '%Lei';

explain解析

字符串不加单引号索引失效

EXPLAIN SELECT * FROM employees WHERE name ='1000';
EXPLAIN SELECT * FROM employees WHERE name =1000;

不加单引号的字符串,mysql底层会使用cust函数将其转换为字符串,此时索引失效。

or&in少使用

少用or或in,用它查询时,mysql不一定使用索引,mysql内部优化器会根据索引比例、表大小等多个因素整体评估是否使用索引。

EXPLAIN SELECT * FROM employees WHERE name ='LiLei' or name='HanMeimei';

范围查询优化

给年龄添加单值索引

ALTER TABLE `employees`ADD INDEX `idx_age`(`age`) USING BTREE;
EXPLAIN select * from employees where age > 1 and age <= 2000;

没有走索引原因:mysql内部优化器会根据检索比例、表大小等多个因素整体评估是否使用索引。
这个例子没有走索引可能是因为单次数据量查询过大导致优化器最终选择不走索引。
优化方法:可以将大的范围拆分成多个小范围。

还没关注我的公众号?

  • 扫文末二维码关注公众号【小强的进阶之路】可领取如下:
  • 学习资料: 1T视频教程:涵盖Javaweb前后端教学视频、机器学习/人工智能教学视频、Linux系统教程视频、雅思考试视频教程;
  • 100多本书:包含C/C++、Java、Python三门编程语言的经典必看图书、LeetCode题解大全;
  • 软件工具:几乎包括你在编程道路上的可能会用到的大部分软件;
  • 项目源码:20个JavaWeb项目源码。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK