您的位置:首页 > 数据库 > MySQL

MySQL-explain-1

2015-07-16 23:25 676 查看
id

查询标志符,也就是select查询序列号。在查询范围内这是一个连续的数值,如果查询中使用了union操作,
那么id可以是NULL,在这样的情况下,table就会显示类似为<unionM,N>


select_type

SIMPLE

简单的查询,没有使用union或者子查询

PRIMARY

最外层的select查询

UNION

union语句中的第二个或者后面的一个

DEPENDENT UNION

union语句中的第二个或者后面的一个,取决于外层查询

UNION RESULT

unoin的结构

SUBQUERY

第一个子查询

DEPENDENT SUBQUERY

第一个子查询,依赖于外层查询

DERIVED

Derived table SELECT (subquery in FROM clause)

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)

table

查找数据所用的表,他也可以是下列两种<unionM,N>:
表数据的连接
<derivedN>:
The row refers to the derived table result for the row with an id value of N. A derived table may result, for example, from a subquery in the FROM clause.


partitions

查询中的记录应该匹配的分区,只有当分区键被使用时才会显示,对于不是分区表的,返回NULL


type

system

表只有一行,这是const类型的特例

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. const tables are very fast because they are read only once.

const is used when you compare all parts of a PRIMARY KEY or UNIQUE index to constant values. In the following queries, tbl_name can be used as a const table:

SELECT * FROM tbl_name WHERE primary_key=1;

SELECT * FROM tbl_name

WHERE primary_key_part1=1 AND primary_key_part2=2;

Using where

限制那些行去匹配

using index

表示并不读数据文件,只从索引文件获取数据

下面这句比较好理解:

If the Extra column also says Using where, it means the index is being used to perform lookups of key values. Without Using where, the optimizer may be reading the index to avoid reading data rows but not using it for lookups. For example, if the index is a covering index for the query, the optimizer may scan it without using it for lookups.

Using filesort

表示使用了文件排序,出现在使用order by的场合。这种情况下是需要优化的,需要考虑是不是可以添加合适的索引,来优化查询。

比如这种情况

explain select * from shop where  name='23' order by city;


因为创建索引时,是先按照id排序的,所以在extra中出现了filesort,这时就可以考虑创建索引

index *(name, city)


range

说明在一定范围内检索。比如用到了in操作符,或者 大于小于操作符。从某种情况还,说明索引创建的还马马虎虎。

explain select count(*) from shop where id=n and time < unix_timestamp();


这个例子如果你创建了索引

index * (id , time)


我感觉,很有可能就是range类型。

possible_keys

标识可能用会到的索引。在实际查询中列出的索引可能并没有被使用。如果这个列为NULL,则查询没有用到索引。这个时候,你需要去检查where子句中用到的查询列是否适合创建索引。

key

标识mysql查询中使用的索引。如果mysql使用了possible_keys中的索引去查询,则使用的索引就会列到key列中。也存在一种可能,就是key中列举的索引并没有出现在possible_key中。

This can happen if none of the possible_keys indexes are suitable for looking up rows, but all the columns selected by the query are columns of some other index. That is, the named index covers the selected columns, so although it is not used to determine which rows to retrieve, an index scan is more efficient than a data row scan.

对于innodb数据库,如果查询中使用的主键,那么次要索引可能会覆盖查询列,因为innodb存储了主键在每一个次要索引。如果

key是空,则mysql没有发现索引去执行。

强制MySQL去使用或者忽略在possible_keys中列出的索引,可以在查询中使用FORCE INDEX,USE INDEX或者IGNORE INDEX

对于MyISAM和NDB表,执行analyze table 帮助优化者选择更合适的索引。

For NDB tables, this also improves performance of distributed pushed-down joins. 对于MyISAM数据表,myisamchk –analyze跟analyze table效果一致。

key_len

mysql使用的索引的长度,计算单位是字节。在gbk编码中,一个汉字占两个字节,utf-8中占用三个字节。如果key为NULL的话,key_len也应该为NULL。字段使你了解到在查询中使用了多重索引的多少部分。

个人感觉,这个也是衡量使用了多少索引。比较好理解

这里写代码片`这里写代码片`


简单说明一个例子:

CREATE TABLE `shop` (
`id` int(10) ,
`city` varchar(20) DEFAULT '',
`name` varchar(20) DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


执行下面的explain:

explain select * from shop where city='12' and name='23';


key_len返回值为131

ref

显示哪些列或者常量和索引进行比较,从表中检索出数据

rows

显示mysql认为必须检索多少条记录来实现查找,对于innodb这个条数是被预估的,可能并不准确

select count(*) from tablename where user = '';
select count(id) from tablename where user ='';


我想表达一种意思:上两种的比较,通过比较,我觉得第一种更好。

filtered

The filtered column indicates an estimated percentage of table rows that will be filtered by the table condition. That is, rows shows the estimated number of rows examined and rows × filtered / 100 shows the number of rows that will be joined with previous tables. This column is displayed if you use EXPLAIN EXTENDED.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql explain