|
| 1 | +# mysql执行计划 |
| 2 | + |
| 3 | + 在企业的应用场景中,为了知道优化SQL语句的执行,需要查看SQL语句的具体执行过程,以加快SQL语句的执行效率。 |
| 4 | + |
| 5 | + 可以使用explain+SQL语句来模拟优化器执行SQL查询语句,从而知道mysql是如何处理sql语句的。 |
| 6 | + |
| 7 | + 官网地址: https://dev.mysql.com/doc/refman/5.5/en/explain-output.html |
| 8 | + |
| 9 | +### 1、执行计划中包含的信息 |
| 10 | + |
| 11 | +| Column | Meaning | |
| 12 | +| :-----------: | :--------------------------------------------: | |
| 13 | +| id | The `SELECT` identifier | |
| 14 | +| select_type | The `SELECT` type | |
| 15 | +| table | The table for the output row | |
| 16 | +| partitions | The matching partitions | |
| 17 | +| type | The join type | |
| 18 | +| possible_keys | The possible indexes to choose | |
| 19 | +| key | The index actually chosen | |
| 20 | +| key_len | The length of the chosen key | |
| 21 | +| ref | The columns compared to the index | |
| 22 | +| rows | Estimate of rows to be examined | |
| 23 | +| filtered | Percentage of rows filtered by table condition | |
| 24 | +| extra | Additional information | |
| 25 | + |
| 26 | +**id** |
| 27 | + |
| 28 | +select查询的序列号,包含一组数字,表示查询中执行select子句或者操作表的顺序 |
| 29 | + |
| 30 | +id号分为三种情况: |
| 31 | + |
| 32 | + 1、如果id相同,那么执行顺序从上到下 |
| 33 | + |
| 34 | +```sql |
| 35 | +explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal; |
| 36 | +``` |
| 37 | + |
| 38 | + 2、如果id不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行 |
| 39 | + |
| 40 | +```sql |
| 41 | +explain select * from emp e where e.deptno in (select d.deptno from dept d where d.dname = 'SALES'); |
| 42 | +``` |
| 43 | + |
| 44 | + 3、id相同和不同的,同时存在:相同的可以认为是一组,从上往下顺序执行,在所有组中,id值越大,优先级越高,越先执行 |
| 45 | + |
| 46 | +```sql |
| 47 | +explain select * from emp e join dept d on e.deptno = d.deptno join salgrade sg on e.sal between sg.losal and sg.hisal where e.deptno in (select d.deptno from dept d where d.dname = 'SALES'); |
| 48 | +``` |
| 49 | + |
| 50 | +**select_type** |
| 51 | + |
| 52 | +主要用来分辨查询的类型,是普通查询还是联合查询还是子查询 |
| 53 | + |
| 54 | +| `select_type` Value | Meaning | |
| 55 | +| :------------------: | :----------------------------------------------------------: | |
| 56 | +| SIMPLE | Simple SELECT (not using UNION or subqueries) | |
| 57 | +| PRIMARY | Outermost SELECT | |
| 58 | +| UNION | Second or later SELECT statement in a UNION | |
| 59 | +| DEPENDENT UNION | Second or later SELECT statement in a UNION, dependent on outer query | |
| 60 | +| UNION RESULT | Result of a UNION. | |
| 61 | +| SUBQUERY | First SELECT in subquery | |
| 62 | +| DEPENDENT SUBQUERY | First SELECT in subquery, dependent on outer query | |
| 63 | +| DERIVED | Derived table | |
| 64 | +| UNCACHEABLE SUBQUERY | A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query | |
| 65 | +| UNCACHEABLE UNION | The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY) | |
| 66 | + |
| 67 | +```sql |
| 68 | +--sample:简单的查询,不包含子查询和union |
| 69 | +explain select * from emp; |
| 70 | + |
| 71 | +--primary:查询中若包含任何复杂的子查询,最外层查询则被标记为Primary |
| 72 | +explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ; |
| 73 | + |
| 74 | +--union:若第二个select出现在union之后,则被标记为union |
| 75 | +explain select * from emp where deptno = 10 union select * from emp where sal >2000; |
| 76 | + |
| 77 | +--dependent union:跟union类似,此处的depentent表示union或union all联合而成的结果会受外部表影响 |
| 78 | +explain select * from emp e where e.empno in ( select empno from emp where deptno = 10 union select empno from emp where sal >2000) |
| 79 | + |
| 80 | +--union result:从union表获取结果的select |
| 81 | +explain select * from emp where deptno = 10 union select * from emp where sal >2000; |
| 82 | + |
| 83 | +--subquery:在select或者where列表中包含子查询 |
| 84 | +explain select * from emp where sal > (select avg(sal) from emp) ; |
| 85 | + |
| 86 | +--dependent subquery:subquery的子查询要受到外部表查询的影响 |
| 87 | +explain select * from emp e where e.deptno in (select distinct deptno from dept); |
| 88 | + |
| 89 | +--DERIVED: from子句中出现的子查询,也叫做派生类, |
| 90 | +explain select staname,ename supname from (select ename staname,mgr from emp) t join emp on t.mgr=emp.empno ; |
| 91 | + |
| 92 | +--UNCACHEABLE SUBQUERY:表示使用子查询的结果不能被缓存 |
| 93 | + explain select * from emp where empno = (select empno from emp where deptno=@@sort_buffer_size); |
| 94 | + |
| 95 | +--uncacheable union:表示union的查询结果不能被缓存:sql语句未验证 |
| 96 | +``` |
| 97 | + |
| 98 | +**table** |
| 99 | + |
| 100 | +对应行正在访问哪一个表,表名或者别名,可能是临时表或者union合并结果集 |
| 101 | + 1、如果是具体的表名,则表明从实际的物理表中获取数据,当然也可以是表的别名 |
| 102 | + |
| 103 | + 2、表名是derivedN的形式,表示使用了id为N的查询产生的衍生表 |
| 104 | + |
| 105 | + 3、当有union result的时候,表名是union n1,n2等的形式,n1,n2表示参与union的id |
| 106 | + |
| 107 | +**type** |
| 108 | + |
| 109 | +type显示的是访问类型,访问类型表示我是以何种方式去访问我们的数据,最容易想的是全表扫描,直接暴力的遍历一张表去寻找需要的数据,效率非常低下,访问的类型有很多,效率从最好到最坏依次是: |
| 110 | + |
| 111 | +system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL |
| 112 | + |
| 113 | +一般情况下,得保证查询至少达到range级别,最好能达到ref |
| 114 | + |
| 115 | +```sql |
| 116 | +--all:全表扫描,一般情况下出现这样的sql语句而且数据量比较大的话那么就需要进行优化。 |
| 117 | +explain select * from emp; |
| 118 | + |
| 119 | +--index:全索引扫描这个比all的效率要好,主要有两种情况,一种是当前的查询时覆盖索引,即我们需要的数据在索引中就可以索取,或者是使用了索引进行排序,这样就避免数据的重排序 |
| 120 | +explain select empno from emp; |
| 121 | + |
| 122 | +--range:表示利用索引查询的时候限制了范围,在指定范围内进行查询,这样避免了index的全索引扫描,适用的操作符: =, <>, >, >=, <, <=, IS NULL, BETWEEN, LIKE, or IN() |
| 123 | +explain select * from emp where empno between 7000 and 7500; |
| 124 | + |
| 125 | +--index_subquery:利用索引来关联子查询,不再扫描全表 |
| 126 | +explain select * from emp where emp.job in (select job from t_job); |
| 127 | + |
| 128 | +--unique_subquery:该连接类型类似与index_subquery,使用的是唯一索引 |
| 129 | + explain select * from emp e where e.deptno in (select distinct deptno from dept); |
| 130 | + |
| 131 | +--index_merge:在查询过程中需要多个索引组合使用,没有模拟出来 |
| 132 | + |
| 133 | +--ref_or_null:对于某个字段即需要关联条件,也需要null值的情况下,查询优化器会选择这种访问方式 |
| 134 | +explain select * from emp e where e.mgr is null or e.mgr=7369; |
| 135 | + |
| 136 | +--ref:使用了非唯一性索引进行数据的查找 |
| 137 | + create index idx_3 on emp(deptno); |
| 138 | + explain select * from emp e,dept d where e.deptno =d.deptno; |
| 139 | + |
| 140 | +--eq_ref :使用唯一性索引进行数据查找 |
| 141 | +explain select * from emp,emp2 where emp.empno = emp2.empno; |
| 142 | + |
| 143 | +--const:这个表至多有一个匹配行, |
| 144 | +explain select * from emp where empno = 7369; |
| 145 | + |
| 146 | +--system:表只有一行记录(等于系统表),这是const类型的特例,平时不会出现 |
| 147 | +``` |
| 148 | + |
| 149 | + **possible_keys** |
| 150 | + |
| 151 | + 显示可能应用在这张表中的索引,一个或多个,查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询实际使用 |
| 152 | + |
| 153 | +```sql |
| 154 | +explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10; |
| 155 | +``` |
| 156 | + |
| 157 | +**key** |
| 158 | + |
| 159 | + 实际使用的索引,如果为null,则没有使用索引,查询中若使用了覆盖索引,则该索引和查询的select字段重叠。 |
| 160 | + |
| 161 | +```sql |
| 162 | +explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10; |
| 163 | +``` |
| 164 | + |
| 165 | +**key_len** |
| 166 | + |
| 167 | +表示索引中使用的字节数,可以通过key_len计算查询中使用的索引长度,在不损失精度的情况下长度越短越好。 |
| 168 | + |
| 169 | +```sql |
| 170 | +explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10; |
| 171 | +``` |
| 172 | + |
| 173 | +**ref** |
| 174 | + |
| 175 | +显示索引的哪一列被使用了,如果可能的话,是一个常数 |
| 176 | + |
| 177 | +```sql |
| 178 | +explain select * from emp,dept where emp.deptno = dept.deptno and emp.deptno = 10; |
| 179 | +``` |
| 180 | + |
| 181 | +**rows** |
| 182 | + |
| 183 | +根据表的统计信息及索引使用情况,大致估算出找出所需记录需要读取的行数,此参数很重要,直接反应的sql找了多少数据,在完成目的的情况下越少越好 |
| 184 | + |
| 185 | +```sql |
| 186 | +explain select * from emp; |
| 187 | +``` |
| 188 | + |
| 189 | +**extra** |
| 190 | + |
| 191 | +包含额外的信息。 |
| 192 | + |
| 193 | +```sql |
| 194 | +--using filesort:说明mysql无法利用索引进行排序,只能利用排序算法进行排序,会消耗额外的位置 |
| 195 | +explain select * from emp order by sal; |
| 196 | + |
| 197 | +--using temporary:建立临时表来保存中间结果,查询完成之后把临时表删除 |
| 198 | +explain select ename,count(*) from emp where deptno = 10 group by ename; |
| 199 | + |
| 200 | +--using index:这个表示当前的查询时覆盖索引的,直接从索引中读取数据,而不用访问数据表。如果同时出现using where 表名索引被用来执行索引键值的查找,如果没有,表面索引被用来读取数据,而不是真的查找 |
| 201 | +explain select deptno,count(*) from emp group by deptno limit 10; |
| 202 | + |
| 203 | +--using where:使用where进行条件过滤 |
| 204 | +explain select * from t_user where id = 1; |
| 205 | + |
| 206 | +--using join buffer:使用连接缓存,情况没有模拟出来 |
| 207 | + |
| 208 | +--impossible where:where语句的结果总是false |
| 209 | +explain select * from emp where empno = 7469; |
| 210 | +``` |
| 211 | + |
0 commit comments