mysql-explain
2016-04-18 18:25
751 查看
EXPLAIN Output Columns
Column | Meaning |
---|---|
id | The SELECTidentifier |
select_type | The SELECTtype |
table | The table for the output row |
partitions | The matching partitions |
type | The join type |
possible_keys | The possible indexes to choose |
key | The index actually chosen |
key_len | The length of the chosen key |
ref | The columns compared to the index |
rows | Estimate of rows to be examined |
filtered | Percentage of rows filtered by table condition |
Extra | Additional information |
select_type
The type of SELECT,
which can be any of those shown in the following table.
select_typeValue | Meaning |
---|---|
SIMPLE | Simple SELECT(not using UNIONor subqueries) |
PRIMARY | Outermost SELECT |
UNION | Second or later SELECTstatement in a UNION |
DEPENDENT UNION | Second or later SELECTstatement in a UNION, dependent on outer query |
UNION RESULT | Result of a UNION. |
SUBQUERY | First SELECTin subquery |
DEPENDENT SUBQUERY | First SELECTin subquery, dependent on outer query |
DERIVED | Derived table SELECT(subquery in FROMclause) |
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 UNIONthat belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY) |
type : join type
system:表只有一行/被查询的表是张衍生表且至多只有一行数据
The table has only one row (= system table). This is a special case of the constjoin
type.
explain select * from (select * from ta where a=100) tt -- a为主键
const:按主键查询或按唯一健查询,且查询条件为等于常量
The table has at most one matching row, which is read at the start of the query. Because there is only one row, values from the column in this row can be regarded as constants by the rest of the optimizer.consttables
are very fast because they are read only once.
constis
used when you compare all parts of a
PRIMARY KEYor
UNIQUEindex to constant
values. In the following queries,
tbl_namecan
be used as a
consttable:
SELECT * FROM [code]tbl_nameWHERE
primary_key=1;
SELECT * FROM
tbl_name
WHERE
primary_key_part1=1 AND
primary_key_part2=2;[/code]
SELECT * FROM tb1_name WHERE unique_key=1;
eq_ref (1.等值连接,被连接的表连接的字段为主键或非空唯一性索引)
One row is read from this table for each combination of rows from theprevious tables. Other than the systemand
consttypes,
this is the best possible join type. It is used when all parts of an index are used by the join and the index is a
PRIMARY KEYor
UNIQUE NOT NULLindex.
eq_refcan
be used for indexed columns that are compared using the
=operator.
The comparison value can be a constant or an expression that uses columns from tables that are read before this table. In the following examples, MySQL can use an
eq_refjoin
to process
ref_table:
SELECT * FROM [code]ref_table,
other_table
WHERE
ref_table.
key_column=
other_table.
column;
SELECT * FROM
ref_table,
other_table
WHERE
ref_table.
key_column_part1=
other_table.
column
AND
ref_table.
key_column_part2=1;[/code]
ref(1.被连接的表只有几行满足要求2.被连接的字段是部分索引或唯一索引)
(说白了说是被连接的表中只有几行满足连接要求)All rows with matching index values are read from this table for each combination of rows from the previous tables.
refis
used if the join uses only a leftmost prefix of the key or if the key is not a
PRIMARY KEYor
UNIQUEindex (in other
words, if the join cannot select a single row based on the key value). If the key that is used matches only a few rows, this is a good join type.
refcan
be used for indexed columns that are compared using the
=or
<=>operator.
In the following examples, MySQL can use a
refjoin
to process
ref_table:
explain select * from ta inner join tb on ta.a=tb.b1
CREATE TABLE `tb` (
`a1` int(11) DEFAULT NULL,
`b1` int(11) DEFAULT NULL,
KEY `b1` (`b1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
explain select * from tb where b1=1
fulltext:被连接的的字段是全文索引字段
The join is performed using a FULLTEXTindex.
ref_or_null(ref
+null)
This join type is like ref,
but with the addition that MySQL does an extra search for rows that contain
NULLvalues.
This join type optimization is used most often in resolving subqueries. In the following examples, MySQL can use a
ref_or_nulljoin
to process
ref_table:
SELECT * FROM [code]ref_table
WHERE
key_column=
exprOR
key_columnIS NULL;[/code]
range(索引范围查询)
Only rows that are in a given range are retrieved, using an index to select the rows. The keycolumn
in the output row indicates which index is used. The
key_lencontains
the longest key part that was used. The
refcolumn
is
NULLfor this type.
rangecan
be used when a key column is compared to a constant using any of the
=,
<>,
>,
>=,
<,
<=,
IS NULL,
<=>,
BETWEEN,
or
IN()operators:
SELECT * FROM [code]tbl_name
WHERE
key_column= 10;
SELECT * FROM
tbl_name
WHERE
key_columnBETWEEN 10 and 20;
SELECT * FROM
tbl_name
WHERE
key_columnIN (10,20,30);
SELECT * FROM
tbl_name
WHERE
key_part1= 10 AND
key_part2IN (10,20,30);[/code]
index
The indexjoin type is the same as
ALL,
except that the index tree is scanned. This occurs two ways:
If the index is a covering index for the queries and can be used to satisfy all data required from the table, only the index tree is scanned. In this case, the
Extracolumn
says
Using index. An index-only scan
usually is faster than
ALLbecause
the size of the index usually is smaller than the table data.
A full table scan is performed using reads from the index to look up data rows in index order.
Uses indexdoes not appear in the
Extracolumn.
MySQL can use this join type when the query uses only columns that are part of a single index.
ALL
A full table scan is done for each combination of rows from the previous tables. This is normally not good if the table is the first table not marked const,
and usually very bad
in all other cases. Normally, you can avoid
ALLby
adding indexes that enable row retrieval from the table based on constant values or column values from earlier tables.
Extra
Impossible WHERE noticed after reading const
tables
MySQL has read all const(and
system)
tables and notice that the
WHEREclause
is always false.
CREATE TABLE `liketest` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL,
`title` varchar(128) NOT NULL,
`memo` varchar(2000) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `title` (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
explain select * from liketest where id=1; (主键查询,但是表中没有数据,所有where 条件永远不成立)
相关文章推荐
- MYSQLdump参数详解
- 使用mysql存储过程添加数据
- web.py操作mysql的数据
- 更改ubuntu mysql data目录位置
- Mysql insert xxx on duplicate key update
- MySQL引擎
- mysql 服务不见了的解决办法
- MySQL函数
- MySQL——修改root密码的4种方法(以windows为例)
- mysql批量写入
- 【小镇的技术天梯】lanmp服务器集群搭建(5)haproxy给mysql节点负载均衡
- MySQL权限及备份还原
- 那些年,在MySQL上遇到的坑
- ubuntu卸载及安装mysql
- MySQL5.7.9免安装版配置方法
- mysql数据库在Navicat Premium连接的时候出现1862错误
- mysql max_allowed_packet查询和修改
- 使用JMeter创建数据库(Mysql)测试
- MySQL存储过程
- MySQL 增删查改