mysql explain type

explain执行计划中type字段分为以下几种:

ALL        INDEX        RANGE        REF        EQ_REF        CONST,SYSTEM        NULL

从左至右,性能从最差到最好

type = ALL,全表扫描,MYSQL扫描全表来找到匹配的行

(因为film表中rating不是索引)

mysql> explain extended select * from film where rating > 9\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: film

type: ALL

possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: 1024

filtered: 100.00

Extra: Using where

1 row in set, 1 warning (0.00 sec)

type = index,索引全扫描,MYSQL遍历整个索引来查找匹配的行。(虽然where条件中没有用到索引,但是要取出的列title是索引包含的列,所以只要全表扫描索引即可,直接使用索引树查找数据)

mysql> explain select title from film\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: film

type: index

possible_keys: NULL

key: idx_title

key_len: 767

ref: NULL

rows: 1024

Extra: Using index

1 row in set (0.00 sec)

type = range ,索引范围扫描,常见于<、<=、>、>=、between等操作符(因为customer_id是索引,所以只要查找索引的某个范围即可,通过索引找到具体的数据)

mysql> explain select * from payment where customer_id > 300 and customer_id < 350\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: payment

type: range

possible_keys: idx_fk_customer_id

key: idx_fk_customer_id

key_len: 2

ref: NULL

rows: 1294

Extra: Using where

1 row in set (0.01 sec)

type = ref ,使用非唯一性索引或者唯一索引的前缀扫描,返回匹配某个单独值的记录行。

(1)使用非唯一性索引customer_id单表查询

mysql> explain select * from payment where customer_id = 350\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: payment

type: ref

possible_keys: idx_fk_customer_id

key: idx_fk_customer_id

key_len: 2

ref: const

rows: 23

Extra:

1 row in set (0.00 sec)

(2)使用非唯一性索引联表查询(由于customer_id在a表中不是主键,是普通索引(非唯一),所以是ref)

mysql> explain select b.*, a.* from payment a ,customer b where a.customer_id = b.customer_id\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: b

type: ALL

possible_keys: PRIMARY

key: NULL

key_len: NULL

ref: NULL

rows: 541

Extra:

*************************** 2. row ***************************

id: 1

select_type: SIMPLE

table: a

type: ref

possible_keys: idx_fk_customer_id

key: idx_fk_customer_id

key_len: 2

ref: sakila.b.customer_id

rows: 14

Extra:

2 rows in set (0.00 sec)

type = eq_ref,相对于ref来说就是使用的是唯一索引,对于每个索引键值,只有唯一的一条匹配记录(在联表查询中使用primary key或者unique key作为关联条件)

(在film和film_text中film_id都是主键,即都是唯一索引)

mysql> explain select * from film a ,film_text b where a.film_id = b.film_id\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: b

type: ALL

possible_keys: PRIMARY

key: NULL

key_len: NULL

ref: NULL

rows: 1000

Extra:

*************************** 2. row ***************************

id: 1

select_type: SIMPLE

table: a

type: eq_ref

possible_keys: PRIMARY

key: PRIMARY

key_len: 2

ref: sakila.b.film_id

rows: 1

Extra: Using where

2 rows in set (0.00 sec)

type = const/system,单表中最多只有一条匹配行,查询起来非常迅速,所以这个匹配行中的其他列中的值可以被优化器在当前查询中当做常量来处理。例如根据主键或者唯一索引进行的查询。

mysql> explain select * from film  where film_id = 1\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: film

type: const

possible_keys: PRIMARY

key: PRIMARY

key_len: 2

ref: const

rows: 1

Extra:

1 row in set (0.02 sec)

注释:如果上表中film表中只有一行数据,那么type就是system。

type = NULL,MYSQL不用访问表或者索引就直接能到结果。

mysql> explain select 1 from dual  where 1\G (dual是一个虚拟的表,可以直接忽略)

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: NULL

type: NULL

possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: NULL

Extra: No tables used

1 row in set (0.00 sec)

mysql> select 1+1 from dual;

+-----+

| 1+1 |

+-----+

|   2 |

+-----+

1 row in set (0.05 sec)

explain extended

mysql> explain extended select sum(amount) from customer a ,payment b where 1 = 1 and a.customer_id = b.customer_id and email = ‘[email protected]‘\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: a

type: ALL

possible_keys: PRIMARY

key: NULL

key_len: NULL

ref: NULL

rows: 541

filtered: 100.00

Extra: Using where

*************************** 2. row ***************************

id: 1

select_type: SIMPLE

table: b

type: ref

possible_keys: idx_fk_customer_id

key: idx_fk_customer_id

key_len: 2

ref: sakila.a.customer_id

rows: 14

filtered: 100.00

Extra:

2 rows in set, 1 warning (0.00 sec)

mysql> show warnings\G

*************************** 1. row ***************************

Level: Note

Code: 1003

Message: select sum(`sakila`.`b`.`amount`) AS `sum(amount)` from `sakila`.`customer` `a` join `sakila`.`payment` `b` where ((`sakila`.`b`.`customer_id` = `sakila`.`a`.`customer_id`) and (`sakila`.`a`.`email` = ‘[email protected]‘))

1 row in set (0.00 sec)

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 17:39:42

mysql explain type的相关文章

mysql explain type连接类型示例

对于MySQL执行计划的获取,我们可以通过explain方式来查看,explain方式看似简单,实际上包含的内容很多,尤其是输出结果中的type类型列.理解这些不同的类型,对于我们SQL优化举足轻重,本文仅描述explian输出结果中的type列,同时给出其演示. 有关explian输出的全描述,可以参考:MySQL EXPLAIN SQL 输出信息描述 一.EXPLAIN 语句中type列的值 type: 连接类型 system 表只有一行 const 表最多只有一行匹配,通用用于主键或者唯一

MySQL explain type详解

对于MySQL执行计划的获取,我们可以通过explain方式来查看,explain方式看似简单,实际上包含的内容很多,尤其是输出结果中的type类型列.理解这些不同的类型,对于我们SQL优化举足轻重. 一.EXPLAIN 语句中type列的值 类型 含义 system 表只有一行 const 表最多只有一行匹配,通用用于主键或者唯一索引比较时 eq_ref 每次与之前的表合并行都只在该表读取一行,这是除了system,const之外最好的一种,特点是使用=,而且索引的所有部分都参与join且索引

mysql explain 中type的归纳

为了更好的理解连接类型(type),将根据查询条件的不同对连接类型进行简单归纳. 表定义如下: 1.id为主键 mysql> show create table key_id; +--------+-------------------------------------------------------------------------------------------------------------------------------------------------------

Mysql Explain 详解

一.语法 explain < table_name > 例如: explain select * from t3 where id=3952602; 二.explain输出解释 +----+-------------+-------+-------+-------------------+---------+---------+-------+------+-------+| id | select_type | table | type  | possible_keys     | key 

MySQL EXPLAIN Output Format(MySQL运维神技)

摘要: DBA经常会用到的explain来查看SQL语句的执行计划,今天小人斗胆,从MySQL 5.7 Reference Manual中把MySQL EXPLAIN Output Format翻译过来.欢迎拍砖 Explain语句提供了一个select语句执行计划的信息. Explain为每个用了select语句的表,返回一行信息.它列出了表中的顺序输出,MySQL会读取他们,然后再处理.MySQL解决了所有使用嵌套循环连接方法.这意味着MySQL会读取第一个表中的一行,然后在第二个表中找到一

MySQL Explain详解(转)

explain SELECT a.* FROM test a,(select id from test where level_id <=4 order by aa_id limit 243000, 100) b where a.id=b.id ;因为延迟关联通过覆盖索引返回所需数据行的主键,再根据主键关联原表获得需要的数据,所以速度比之前快上不少. 覆盖索引(只访问索引的查询,即查询只需要访问索引,而无须访问数据行,最简单的理解,比如翻开一本书,从目录页查找某些内容,但是目录就写的比较详细,我

mysql explain执行计划详解

1).id列SELECT识别符.这是SELECT查询序列号.这个不重要,查询序号即为sql语句执行的顺序 2).select_type列常见的有: A:simple:表示不需要union操作或者不包含子查询的简单select查询.有连接查询时,外层的查询为simple,且只有一个 B:primary:一个需要union操作或者含有子查询的select,位于最外层的单位查询的select_type即为primary.且只有一个 C:union:union连接的两个select查询,第一个查询是de

MySQL Explain 结果解读与实践

Explain 结果解读与实践 基于 MySQL 5.0.67 ,存储引擎 MyISAM . 注:单独一行的"%%"及"`"表示分隔内容,就象分开"第一章""第二章". explain 可以分析 select 语句的执行,即 MySQL 的"执行计划": mysql> explain select 1; +----+-------------+-------+------+--------------

MYSQL explain详解 转自http://blog.csdn.net/zhuxineli/article/details/14455029

标签: WHERE子句用于限制哪一个行匹配下一个如果Extra值不为Using wher查询可能会有一些错误 如果想 2013-11-24 17:55 36299人阅读 评论(5) 收藏 举报  分类: mysql(13)  版权声明:本文为博主原创文章,未经博主允许不得转载. explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 先解析一条sql语句,看出现什么内容 EXPLAINSELECTs.uid,s.username