前幾篇文章介紹了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+2mysql
EXPLAIN select * from employees where name='LiLei';
EXPLAIN select * from employees where name='LiLei' AND age = 22;
EXPLAIN select * from employees where name='LiLei' AND age = 22 AND position = 'manager';
若是索引是多列,要最受最左前綴法則。指的是查詢從索引的最左前列開始而且不跳過索引中的列。如下三條sql根據最左前綴法則,都不會走索引。web
EXPLAIN select * from employees where age = 22 AND position='manager'; EXPLAIN select * from employees where position ='manager'; EXPLAIN select * from employees where age=17;
不要在索引列上作任何操做(計算、函數、類型轉換),會致使索引失效而轉向全表掃描。sql
EXPLAIN select * from employees where name='LiLei';
EXPLAIN select * from employees where left(name, 3)='LiLei';
給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';
看到key_len這個索引長度是78, 也就是隻使用到了前兩個字段name和age,postition沒有使用到索引的。mysql優化
儘可能使用覆蓋索引(只訪問索引的查詢(索引列包含查詢列)),減小selelct * 語句。數據結構
EXPLAIN SELECT name,age,position FROM employees WHERE name ='LiLei' AND age=22 AND position ='manager';
mysql在使用不等於(! = 或者 <>)的時候沒法使用索引會致使全表掃描機器學習
EXPLAIN SELECT * FROM employees WHERE name !='LiLei' ;
is null,is not null也沒法使用索引編程語言
EXPLAIN SELECT * FROM employees WHERE name is null;
like以通配符開頭(‘$abc’)mysql索引失效會變成全表掃描操做函數
EXPLAIN SELECT * FROM employees WHERE name LIKE '%Lei';
EXPLAIN SELECT * FROM employees WHERE name ='1000'; EXPLAIN SELECT * FROM employees WHERE name =1000;
不加單引號的字符串,mysql底層會使用cust函數將其轉換爲字符串,此時索引失效。
少用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內部優化器會根據檢索比例、表大小等多個因素總體評估是否使用索引。
這個例子沒有走索引多是由於單次數據量查詢過大致使優化器最終選擇不走索引。
優化方法:能夠將大的範圍拆分紅多個小範圍。
還沒關注個人公衆號?