产品经理喊你回公司加班优化SQL了

139 阅读2分钟

本文已参与【新人创作礼】活动 ,一起开启掘金创作之路

当前测试数据库版本5.7.26

MySQL优化特别是SQL语句优化对于后端开发人员来说可谓是司空见惯的事情,一日不进行SQL优化,便夜不能寐!!(这里有夸大的成分,大家彼此心照不宣就可以了)话不多说,直接开干!!!

1.按需取数
反面案例
SELECT * FROM `test`.`games` where `id` = 800000;
优化栗子
如果你只需要在客户端页面展示id,title,logo三项就只需要查询这三项
SELECT `id`,`title`,`logo` FROM `test`.`games` where `id` = 800000;
2.不要在条件表达式where中进行任何运算操作(主要是指 “=” 号左边)
反面案例
SELECT * FROM `test`.`games` where `id` + 100 = 800000;
SELECT * FROM `test`.`games` where `id` % 2 = 1;
3.查询时最好避免字段数据类型的隐式转换
反面案例
SELECT `id`,`title`,`logo` FROM `test`.`games` where `title` = 5566;

# title 字段在数据库里面定义的类型varchar(100) 而查询条件里面直接使用int类型 
        导致查询速度变慢 切记不要踩坑(前人踩坑是为了后人少踩坑甚至不踩坑 前辈辛苦了!!!)
4.释放联合索引(或多列索引、组合索引)的最大光芒(物尽其用)
未使用联合索引前
select `id`,`title`,`status`,`catid`,`logo` from `news` where `status` = '1' and `catid` not in ('507') order by `updated_at` desc, `id` desc limit 10 offset 20;
耗时15.55s
使用联合索引后
索引名 idx_sta_update_id 索引字段(status, updated_at, id
select `id`,`title`,`status`,`catid`,`logo` from `news` where `status` = '1' and `catid` not in ('507') order by `updated_at` desc, `id` desc limit 10 offset 20;
耗时870μs
前后SQL语句查询速度提高不止一星半点儿(15.55s -> 870μs 提速方面自行脑补)

1秒(s) = 1000毫秒(ms) 1毫秒=1000微秒(μs)

5.完美错开不走索引的场景
-> 1.模糊查询%(1)谭%(2)括弧1前面尽量不使用匹配符%(你不要过来啊!!!)
反例
select `id`,`title`,`status`,`catid`,`logo` from `news` where `article_name` like '%谭%'; 
正例(我是个正经栗子)
select `id`,`title`,`status`,`catid`,`logo` from `news` where `article_name` like '谭%' ;
-> 2.对查询条件字段进行null判断(有想过索引的感受吗?)
反例
select `id`,`title`,`status`,`catid`,`logo` from `news` where `status` is not null; 
正例(我是个正经栗子)这里又要聊到数据库字段的设计问题(其实我也不想聊,但是你不听啊)
设计数据库字段的时候 就要想好了 查询的时候直接指定默认值查询即可 比如
select `id`,`title`,`status`,`catid`,`logo` from `news` where `status`= 0; 

0为指定的默认值

未完待续......