MySQL explain type详解

对于MySQL执行计划的获取,我们可以通过explain方式来查看,explain方式看似简单,实际上包含的内容很多,尤其是输出结果中的type类型列。理解这些不同的类型,对于我们SQL优化举足轻重。

一、EXPLAIN 语句中type列的值

类型 含义
system 表只有一行
const 表最多只有一行匹配,通用用于主键或者唯一索引比较时
eq_ref
每次与之前的表合并行都只在该表读取一行,这是除了system,const之外最好的一种,特点是使用=,而且索引的所有部分都参与join且索引是主键或非空唯一键的索引

ref 如果每次只匹配少数行,那就是比较好的一种,使用=或<=>,可以是左覆盖索引或非主键或非唯一键
fulltext 全文搜索
ref_or_null 与ref类似,但包括NULL
index_merge
表示出现了索引合并优化(包括交集,并集以及交集之间的并集),但不包括跨表和全文索引。

这个比较复杂,目前的理解是合并单表的范围索引扫描(如果成本估算比普通的range要更优的话

unique_subquery
在in子查询中,就是value in (select...)把形如“select unique_key_column”的子查询替换。

PS:所以不一定in子句中使用子查询就是低效的!

index_subquery 同上,但把形如”select non_unique_key_column“的子查询替换
range  常数值的范围
index
a.当查询是索引覆盖的,即所有数据均可从索引树获取的时候(Extra中有Using Index);

b.以索引顺序从索引中查找数据行的全表扫描(无 Using Index);

c.如果Extra中Using Index与Using Where同时出现的话,则是利用索引查找键值的意思;

d.如单独出现,则是用读索引来代替读行,但不用于查找

all 全表扫描

二、连接类型部分示例

1、all

-- 环境描述

([email protected]) [sakila]> show variables like ‘version‘;

+---------------+--------+

| Variable_name | Value  |

+---------------+--------+

| version     | 5.6.26 |

+---------------+--------+

MySQL采取全表遍历的方式来返回数据行,等同于Oracle的full table scan

([email protected]) [sakila]> explain select count(description) from film;

+----+-------------+-------+------+---------------+------+---------+------+------+-------+

| id | select_type | table | type | possible_keys | key  | key_len | ref | rows | Extra |

+----+-------------+-------+------+---------------+------+---------+------+------+-------+

| 1 | SIMPLE    | film  | ALL  | NULL      | NULL | NULL   | NULL | 1000 | NULL  |

+----+-------------+-------+------+---------------+------+---------+------+------+-------+

2、index

MySQL采取索引全扫描的方式来返回数据行,等同于Oracle的full index scan

([email protected]) [sakila]> 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: 1000

Extra: Using index

1 row in set (0.00 sec)

3、  range

索引范围扫描,对索引的扫描开始于某一点,返回匹配值域的行,常见于between、<、>等的查询

等同于Oracle的index range scan

([email protected]) [sakila]> explain select * from payment where customer_id>300 and customer_id<400\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: 2637

Extra: Using where

1 row in set (0.00 sec)

([email protected]) [sakila]> explain select * from payment where customer_id in (200,300,400)\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: 86

Extra: Using index condition

1 row in set (0.00 sec)

4、ref

非唯一性索引扫描或者,返回匹配某个单独值的所有行。常见于使用非唯一索引即唯一索引的非唯一前缀进行的查找

([email protected]) [sakila]> explain select * from payment where customer_id=305\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: 25

Extra:

1 row in set (0.00 sec)

idx_fk_customer_id为表payment上的外键索引,且存在多个不不唯一的值,如下查询

([email protected]) [sakila]> select customer_id,count(*) from payment group by customer_id

-> limit 2;

+-------------+----------+

| customer_id | count(*) |

+-------------+----------+

|       1 |     32 |

|       2 |     27 |

+-------------+----------+

-- 下面是非唯一前缀索引使用ref的示例

([email protected]) [sakila]> create index idx_fisrt_last_name on customer(first_name,last_name);

Query OK, 599 rows affected (0.09 sec)

Records: 599  Duplicates: 0  Warnings: 0

([email protected]) [sakila]> select first_name,count(*) from customer group by first_name

-> having count(*)>1 limit 2;

+------------+----------+

| first_name | count(*) |

+------------+----------+

| JAMIE    |      2 |

| JESSIE  |      2 |

+------------+----------+

2 rows in set (0.00 sec)

([email protected]) [sakila]> explain select first_name from customer where first_name=‘JESSIE‘\G

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

id: 1

select_type: SIMPLE

table: customer

type: ref

possible_keys: idx_fisrt_last_name

key: idx_fisrt_last_name

key_len: 137

ref: const

rows: 2

Extra: Using where; Using index

1 row in set (0.00 sec)

([email protected]) [sakila]> alter table customer drop index idx_fisrt_last_name;

Query OK, 599 rows affected (0.03 sec)

Records: 599  Duplicates: 0  Warnings: 0

--下面演示出现在join是ref的示例

([email protected]) [sakila]> explain select b.*,a.* from payment a inner join

-> customer b on 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: 599

Extra: NULL

*************************** 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: 13

Extra: NULL

2 rows in set (0.01 sec)

5、eq_ref

类似于ref,其差别在于使用的索引为唯一索引,对于每个索引键值,表中只有一条记录与之匹配。

多见于主键扫描或者索引唯一扫描。

([email protected]) [sakila]> explain select * from film a join film_text b

-> on a.film_id=b.film_id;

+----+-------------+-------+--------+---------------+---------+---------+----------+------+-------+

| id | select_type | table | type   | possible_keys | key   | key_len | ref    | rows | Extra     |

+----+-------------+-------+--------+---------------+---------+---------+----------+------+-------+

|  1 | SIMPLE   | b     | ALL   | PRIMARY    | NULL  | NULL  | NULL   | 1000 | NULL  |

|  1 | SIMPLE   | a     | eq_ref | PRIMARY    | PRIMARY | 2   | sakila.b.film_id | 1 | Using where |

+----+-------------+-------+--------+---------------+---------+---------+-----------+------+------+

([email protected]) [sakila]> explain select title from film where film_id=5;

+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+

| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra |

+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+

|  1 | SIMPLE      | film  | const | PRIMARY  | PRIMARY | 2   | const |    1 | NULL  |

+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+

6、const、system:

当MySQL对查询某部分进行优化,这个匹配的行的其他列值可以转换为一个常量来处理。

如将主键或者唯一索引置于where列表中,MySQL就能将该查询转换为一个常量

([email protected]) [sakila]> create table t1(id int,ename varchar(20) unique);

Query OK, 0 rows affected (0.05 sec)

([email protected]) [sakila]> insert into t1 values(1,‘robin‘),(2,‘jack‘),(3,‘henry‘);

Query OK, 3 rows affected (0.00 sec)

Records: 3  Duplicates: 0  Warnings: 0

([email protected]) [sakila]> explain select * from (select * from t1 where ename=‘robin‘)x;

+----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+

| id | select_type | table      | type   | possible_keys | key   | key_len | ref   | rows | Extra |

+----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+

|  1 | PRIMARY | <derived2> | system | NULL | NULL  | NULL    | NULL  |    1 | NULL  |

|  2 | DERIVED  | t1     | const  | ename   | ename | 23      | const |    1 | NULL  |

+----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+

2 rows in set (0.00 sec)

7、type=NULL

MySQL不用访问表或者索引就可以直接得到结果

([email protected]) [sakila]> explain select sysdate();

+----+-------------+-------+------+---------------+------+---------+------+------+----------------+

| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |

+----+-------------+-------+------+---------------+------+---------+------+------+----------------+

|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |

+----+-------------+-------+------+---------------+------+---------+------+------+----------------+

1 row in set (0.00 sec)

时间: 2024-07-28 12:49:49

MySQL explain type详解的相关文章

MySQL中EXPLAIN命令详解

explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了: 如: explain select surname,first_name form a,b where a.id=b.id EXPLAIN列的解释: table:显示这一行的数据是关于哪张表的 type:这是重要的列,显示连接使用了何种类型.从最好到最差的连接类型为const.eq_reg.ref.range.in

mysql联合索引详解

所有的MySQL列类型能被索引.在相关的列上的使用索引是改进SELECT操作性能的最好方法. 一.前缀索引 对于CHAR和VARCHAR列,你可以索引列的前缀.这更快并且比索引整个列需要较少的磁盘空间.在CREATE TABLE语句中索引列前缀的语法看起来像这样: KEY index_name (col_name(length))下面的例子为name列的头10个字符创建一个索引: mysql> CREATE TABLE test (name CHAR(200) NOT NULL,KEY inde

MySQL数据库优化详解(收藏)

MySQL数据库优化详解 mysql表复制 复制表结构+复制表数据mysql> create table t3 like t1;mysql> insert into t3 select * from t1;mysql索引 ALTER TABLE用来创建普通索引.UNIQUE索引或PRIMARY KEY索引ALTER TABLE table_name ADD INDEX index_name (column_list)ALTER TABLE table_name ADD UNIQUE (colu

MySQL配置文件mysql.ini参数详解、MySQL性能优化

MySQL配置文件mysql.ini参数详解.MySQL性能优化 my.ini(Linux系统下是my.cnf),当mysql服务器启动时它会读取这个文件,设置相关的运行环境参数. my.ini分为两块:Client Section和Server Section.   Client Section用来配置MySQL客户端参数.   要查看配置参数可以用下面的命令: show variables like '%innodb%'; # 查看innodb相关配置参数 show status like

MySQL关闭过程详解和安全关闭MySQL的方法

这篇文章主要介绍了MySQL关闭过程详解和安全关闭MySQL的方法,在了解了关闭过程后,出现故障能迅速定位,本文还给出了安全关闭MySQL的建议及方法,需要的朋友可以参考下 www.qdmm.com/BookReader/114529,58420799.aspx www.qdmm.com/BookReader/114529,58484600.aspx www.qdmm.com/BookReader/114529,58486256.aspx www.qdmm.com/BookReader/1145

如何查看mysql数据库的引擎/MySQL数据库引擎详解

一般情况下,mysql会默认提供多种存储引擎,你可以通过下面的查看: 看你的mysql现在已提供什么存储引擎:mysql> show engines; 看你的mysql当前默认的存储引擎:mysql> show variables like '%storage_engine%'; 你要看某个表用了什么引擎(在显示结果里参数engine后面的就表示该表当前用的存储引擎):mysql> show create table 表名; MySQL数据库引擎详解 作为Java程序员,MySQL数据库

cmd下 mysql操作命令大全详解

启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库:show databases; 选择数据库:use databaseName; 列出表格:show tables: 显示表格列的属性:show columns from tableName: 建立数据库:source fileName.txt; 匹配字符:可以用通配符_代表任何一个字符,%代表任何字符串; 增加一个字段

mysql 联合索引详解

mysql 联合索引详解   联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分.例如索引是key index (a,b,c). 可以支持a | a,b| a,b,c 3种组合进行查找,但不支持 b,c进行查找 .当最左侧字段是常量引用时,索引就十分有效. 两个或更多个列上的索引被称作复合索引.利用索引中的附加列,您可以缩小搜索的范围,但使用一个具有两列的索引 不同于使用两个单独的索引.复合索引的结构与电话簿类似,人名由

MySQL的binlog详解(转)

MySQL的binlog详解 什么是binlog binlog日志用于记录所有更新了数据或者已经潜在更新了数据(例如,没有匹配任何行的一个DELETE)的所有语句.语句以"事件"的形式保存,它描述数据更改. binlog作用 因为有了数据更新的binlog,所以可以用于实时备份,与master/slave复制 和binlog有关参数 log_bin 设置此参数表示启用binlog功能,并指定路径名称 log_bin_index 设置此参数是指定二进制索引文件的路径与名称 binlog_