一、执行计划
语法:sql前加explain,如explain
explain select * from employees
各字段的含义:
select查询的序列号,如果id相同,从上往下执行,如果id不同,从下往上执行。
如1,1,2,3
执行顺序为:321(第一个)1(第二个)
主要用来分辨查询的类型,是普通查询还是联合查询还是子查询
官方文档解释:
| `select_type` Value | Meaning |
| :------------------: | :----------------------------------------------------------: |
| SIMPLE | Simple SELECT (not using UNION or subqueries) |
| PRIMARY | Outermost SELECT |
| UNION | Second or later SELECT statement in a UNION |
| DEPENDENT UNION | Second or later SELECT statement in a UNION, dependent on outer query |
| UNION RESULT | Result of a UNION. |
| SUBQUERY | First SELECT in subquery |
| DEPENDENT SUBQUERY | First SELECT in subquery, dependent on outer query |
| DERIVED | Derived table |
| UNCACHEABLE SUBQUERY | A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query |
| UNCACHEABLE UNION | The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY) |
中文解释及例子:
--sample:简单的查询,不包含子查询和union
explain select * from emp;
--primary:查询中若包含任何复杂的子查询,最外层查询则被标记为Primary
explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ;
--union:若第二个select出现在union之后,则被标记为union
explain select * from emp where deptno = 10 union select * from emp where sal >2000;
--dependent union:跟union类似,此处的depentent表示union或union all联合而成的结果会受外部表影响
explain select * from emp e where e.empno in ( select empno from emp where deptno = 10 union select empno from emp where sal >2000)
--union result:从union表获取结果的select
explain select * from emp where deptno = 10 union select * from emp where sal >2000;
--subquery:在select或者where列表中包含子查询
explain select * from emp where sal > (select avg(sal) from emp) ;
--dependent subquery:subquery的子查询要受到外部表查询的影响
explain select * from emp e where e.deptno in (select distinct deptno from dept);
--DERIVED: from子句中出现的子查询,也叫做派生类,
explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ;
--UNCACHEABLE SUBQUERY:表示使用子查询的结果不能被缓存
explain select * from emp where empno = (select empno from emp where deptno=@@sort_buffer_size);
--uncacheable union:表示union的查询结果不能被缓存:sql语句未验证
被查询的表名,有可能是临时表的表名
索引访问类型(指索引查询的效率,如全表查询,范围查找,常数查找)
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
一般情况下,得保证查询至少达到range级别,最好能达到ref
例子:
--all:全表扫描,一般情况下出现这样的sql语句而且数据量比较大的话那么就需要进行优化。
explain select * from emp;
--index:全索引扫描这个比all的效率要好,主要有两种情况,一种是当前的查询时覆盖索引,即我们需要的数据在索引中就可以索取,或者是使用了索引进行排序,这样就避免数据的重排序
explain select empno from emp;
--range:表示利用索引查询的时候限制了范围,在指定范围内进行查询,这样避免了index的全索引扫描,适用的操作符: =, <>, >, >=, <, <=, IS NULL, BETWEEN, LIKE, or IN()
explain select * from emp where empno between 7000 and 7500;
--index_subquery:利用索引来关联子查询,不再扫描全表
explain select * from emp where emp.job in (select job from t_job);
--unique_subquery:该连接类型类似与index_subquery,使用的是唯一索引
explain select * from emp e where e.deptno in (select distinct deptno from dept);
--index_merge:在查询过程中需要多个索引组合使用,没有模拟出来
--ref_or_null:对于某个字段即需要关联条件,也需要null值的情况下,查询优化器会选择这种访问方式
explain select * from emp e where e.mgr is null or e.mgr=7369;
--ref:使用了非唯一性索引进行数据的查找
create index idx_3 on emp(deptno);
explain select * from emp e,dept d where e.deptno =d.deptno;
--eq_ref :使用唯一性索引进行数据查找
explain select * from emp,emp2 where emp.empno = emp2.empno;
--const:这个表至多有一个匹配行,
explain select * from emp where empno = 7369;
--system:表只有一行记录(等于系统表),这是const类型的特例,平时不会出现
可能被使用的索引
一张表可能有多个索引,这里就显示了当前sql有可能走的全部索引
实际被使用的索引
虽然有多个索引,但实际上只走一个索引
被用到的索引字节数。越短越好。
索引哪一列被使用了。如果是主键索引或唯一索引,有可能是const
估算sql执行大概要读取多少行数据,越少越好
--using filesort:说明mysql无法利用索引进行排序,只能利用排序算法进行排序,会消耗额外的位置
explain select * from emp order by sal;
--using temporary:建立临时表来保存中间结果,查询完成之后把临时表删除
explain select ename,count(*) from emp where deptno = 10 group by ename;
--using index:这个表示当前的查询时覆盖索引的,直接从索引中读取数据,而不用访问数据表。如果同时出现using where 表名索引被用来执行索引键值的查找,如果没有,表面索引被用来读取数据,而不是真的查找
explain select deptno,count(*) from emp group by deptno limit 10;
--using where:使用where进行条件过滤
explain select * from t_user where id = 1;
--using join buffer:使用连接缓存,情况没有模拟出来
--impossible where:where语句的结果总是false
explain select * from emp where empno = 7469;
二、sql优化
1.过滤条件是否充足,减少磁盘IO
如:查询empno=1且name="张三"的数据
正确写法:select * from emp where empno = 1 and name=“张三”;
错误写法:select * from emp where empno = 1,然后在业务层过滤name=“张三”
2.不要查询所有字段,如果有必要就去重,减少传输IO
如:查询张三的年龄
正确写法:select age from emp where name=“张三”
错误写法:select * from emp where name=“张三”
count(1)和count(*)或count(其他字段)从效率上没有任何区别
count在数据库层面很难优化。
优化思路:
1.可以把数值缓存到redis,每次添加就+1。删除-1。
2.count一般不需要太准确的数据。可以使用近似值。使用explain获取rows。
1.确保on或者using上的列上有索引
2.group by和order by只涉及一个表上的列,优化器才有可能走索引
3.修改加载顺序,强制先加载小表(只适用于内连接)
如:
select * from a straight_join b where a.id= b.group_id
先加载a,再加载b
尽量转化为关联查询
思路是尽可能使用覆盖索引
select film_id,description from film order by title limit 50,5 explain
优化成↓
select film.film_id,film.description from film inner join (select
film_id from film order by title limit 50,5) as lim using(film_id);
film_id是索引列
尽量使用union all,因为union会有个去重操作,代价很高
#语法
set @rownum=0;
select id,@rownum:=@rownum+1 as rownum from emp limit 10;
#查询获取演过最多电影的10名演员,然后根据出演时间做一个排名
set @rownum=0;
select *,@rownum:=@rownum+1 from(select `type`,count(*) from actor group by `type`) a;
#更新一条语句的时间戳,并知道当前更新的时间戳是多少
update enum_test set `time`=now() where @time:=now();
select @time