MySQL High --- index optimization analysis (Explain a performance analysis)

First, the concept

Use EXPLAINkeywords can simulate SQL query optimizer to perform, so they know how to handle your MySQL is a SQL statement. Analysis of performance bottlenecks in your query or table structure.

Second, use:

Explain + SQL statement.

Three, Explain field interpretation

Explain returned after the execution information:Here Insert Picture Description

1, id: three cases

1, the same id, execution order from top to bottom.
Here Insert Picture Description
the same id, execution order from top to bottom, with table column observations indicated order of execution t1-> t3-> t2.

2, different id, if the query is a child, id serial number is incremented, id value the greater the higher the priority, the first to be executed.
Here Insert Picture Description
If the subquery id number is incremented, the greater the value of the id performing higher priority, with table column apparent order of execution is t3-> t1-> t2.

3, id same different, exist.
Here Insert Picture Description
If the same id, may be considered to be the same group, the execution order from top to bottom. In all groups, id larger value the higher execution priority. Therefore, the order of execution t3-> derived2 (table derived, it can be said temporary table) -> t2.

2, select_type: type represents the query is mainly used to distinguish between normal query, the union query, sub query of complex queries.

SIMPLE Simple select query, the query does not contain subqueries or UNION
PRIMARY If the query contains any complex sub-section, the outermost query were marked as Primary
SUBQUERY It contains a subquery in the SELECT list or WHERE
DERIVED Subquery contained in the FROM list is marked DERIVED (derivative) MySQL recursively execute these sub-queries, the results in a temporary table.
UNION If after the second SELECT appears UNION, were labeled UNION; UNION if included in the FROM clause of a subquery, SELECT will be marked as the outer layer: DERIVED
UNIONRESULT Get the results from the table SELECT UNION

3, table: This data is based on what tables are.

4、type:查询的访问类型。是较为重要的一个指标

结果值从最好到最坏依次是:

system->const->eq_ref->ref->range->index->ALL

一般来说,得保证查询至少达到range级别,最好能达到ref。

1、system:
表只有一行记录(等于系统表),这是const类型的特列,平时不会出现,这个也可以忽略不计
2、const:
表示通过索引一次就找到了,const用于比较primarykey或者unique索引。因为只匹配一行数据,所以很快。如将主键置于where列表中,MySQL就能将该查询转换为一个常量。、
Here Insert Picture Description
3、eq_ref:
唯一性索引扫描,对于每个索引键,表中只有一条记录与之匹配。常见于主键或唯一索引扫描。
Here Insert Picture Description
4、ref:
非唯一性索引扫描,返回匹配某个单独值的所有行.本质上也是一种索引访问,它返回所有匹配某个单独值的行,然而,它可能会找到多个符合条件的行,所以他应该属于查找和扫描的混合体。
Here Insert Picture Description
建立索引后:
Here Insert Picture Description
5、range:
只检索给定范围的行,使用一个索引来选择行。key列显示使用了哪个索引一般就是在你的where语句中出现了between、<、>、in等的查询这种范围扫描索引扫描比全表扫描要好,因为它只需要开始于索引的某一点,而结束语另一点,不用扫描全部索引。
Here Insert Picture Description
Here Insert Picture Description
6、index:
出现index是sql使用了索引但是没用通过索引进行过滤,一般是使用了覆盖索引或者是利用索引进行了排序分组。
Here Insert Picture Description
7、ALL:
FullTableScan,将遍历全表以找到匹配的行。
Here Insert Picture Description


5、possible_keys:显示可能应用在这张表中的索引,一个或多个。查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询实际使用。

6, key: the index of actual use. If NULL, then do not use the index.

Published 72 original articles · won praise 16 · Views 140,000 +

Guess you like

Origin blog.csdn.net/qq_43229543/article/details/104063246