mysql调优-sql篇

本文详细解析了MySQL查询执行计划中的各个字段,包括id、select_type、table、type等,阐述了SQL优化的重要性,如避免全表扫描、优化子查询、利用索引以及limit分页和union查询的优化。此外,还介绍了count()的优化策略以及如何通过自定义变量提升查询效率。
摘要由CSDN通过智能技术生成

一、执行计划

语法:sql前加explain,如explain
explain select * from employees
在这里插入图片描述
各字段的含义:

  • id

select查询的序列号,如果id相同,从上往下执行,如果id不同,从下往上执行。
如1,1,2,3
执行顺序为:321(第一个)1(第二个)

  • select_type

主要用来分辨查询的类型,是普通查询还是联合查询还是子查询
官方文档解释:

| `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语句未验证
  • table

被查询的表名,有可能是临时表的表名

  • type

索引访问类型(指索引查询的效率,如全表查询,范围查找,常数查找)

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类型的特例,平时不会出现
  • possible_keys

可能被使用的索引
一张表可能有多个索引,这里就显示了当前sql有可能走的全部索引

  • key

实际被使用的索引
虽然有多个索引,但实际上只走一个索引

  • key_len

被用到的索引字节数。越短越好。

  • ref

索引哪一列被使用了。如果是主键索引或唯一索引,有可能是const

  • rows

估算sql执行大概要读取多少行数据,越少越好

  • extra

--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()的优化

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

  • 优化子查询

尽量转化为关联查询

  • 优化limit分页

思路是尽可能使用覆盖索引

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查询

尽量使用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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值