~ Mysql database where the Explain Description

 

 

For mysql execution plan can be added before the select Explain to implement, it can tell us how your statement performance.

The following is a detailed description of explain, are also official, a reference in the future.

id SELECT identifier. This is a SELECT query sequence number
select_type

SELECT type, which can be any of the following:

  • SIMPLE : Simple SELECT (not using UNION or subqueries)
  • PRIMARY : the outermost SELECT
  • UNION : The second or rear UNION SELECT statement
  • UNION DEPENDENT : UNION in the second SELECT statement or the back, depending on outside of inquiry
  • The RESULT UNION : UNION results
  • SUBQUERY : the first sub-query SELECT
  • SUBQUERY DEPENDENT : the first sub-query SELECT, depending on outside inquiry
  • DERIVED : export table SELECT (FROM clause subqueries)
table

Output line table referenced

type

Join type. Various types of coupling are given below, in accordance with the type ordered from best to worst type:

  • System : Only one line of table (= system table). This is a special case of the const join type.
  • const : Table at most one matching row, which will be read at the start of the query. Because only one row, column values in this row may be considered to optimize the remaining portion is constant. const table quickly because they are read only once!
  • eq_ref : For each combination of rows from the front of the table, reads a line from the table. This is probably the best connection type, except const type.
  • REF : For each combination of rows from the preceding table, all rows matching the index values read from this table.
  • ref_or_null : The connection type as ref, but added the line MySQL can search specifically contain NULL values.
  • index_merge : the coupling type indication index combined optimization method.
  • unique_subquery : The type of the alternative form of ref IN subqueries of the following: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subquery is an index lookup function, can completely replace the subquery, more efficient.
  • index_subquery : This type is similar to the coupling unique_subquery. IN subquery can be replaced, but only for the following sub-query in the form of a non-unique index: value IN (SELECT key_column FROM single_table WHERE some_expr)
  • Range : only retrieves a row given range, using an index to select the rows.
  • index : the coupling with ALL the same type, except that only the index tree is scanned. This is usually faster than ALL, because the index file is usually smaller than the data file.
  • ALL : For each combination of rows from the previous table, a full table scan.
possible_keys

It indicates which indexes MySQL could use to find the rows in the table

key Display key (index) MySQL actually decided to use. If you do not select an index, the key is NULL.
key_len Display MySQL decided to use key lengths. If the key is NULL, the length is NULL.
ref Shows which columns or constants are selected together with a key from a table using the row.
rows Display the number of rows MySQL believes it must examine the query execution. Of data between a plurality of lines can be estimated by multiplying the number of rows to be processed.
filtered It shows the percentage of the estimated value of the number of rows filtered through conditions.
Extra

This column contains the MySQL query to resolve details

  • DISTINCT : MySQL found after a matching line for the current row combination stop searching for more rows.
  • EXISTS Not : the MySQL LEFT JOIN able to query optimization, after a match was found LEFT JOIN standard line, no more rows in the check table of the foregoing combinations of rows.
  • Record the checked for each Range (Map index: #) : the MySQL found no good index may be used, but if the value found from the front of the column of the table is known, the index portion may be used.
  • Filesort the Using : MySQL requires additional one pass to find out how to retrieve the rows in sorted order.
  • Index the Using : from the use of only information in the index tree without having to search further reads the actual line to retrieve column information in the table.
  • The Temporary the Using : In order to solve the query, MySQL needs to create a temporary table to hold the result.
  • WHERE the using : the WHERE clause restricts a row which matches the next table or sent to a client.
  • Sort_union the using (...), the using Union (...), the using INTERSECT (...) : illustrate how these functions merge type join index_merge index scan.
  • Using index for group-by:类似于访问表的Using index方式,Using index for group-by表示MySQL发现了一个索引,可以用来查 询GROUP BY或DISTINCT查询的所有列,而不要额外搜索硬盘访问实际的表。

type:这是重要的列,显示连接使用了何种类型。从最好到最差的连接类型为const、eq_reg、ref、range、index和ALL

         type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
         一般来说,得保证查询至少达到range级别,最好能达到ref。

我们在进行条件查询时,建议使用索引,否则将引起全表扫描,IO的开销和程序的性能都没法保证!

对于mysql的执行计划可以在select前添加Explain来实现,它可以告诉我们你的语句性能如何。

下面是对explain的具体说明,也都是官方的,以后进行参考。

id SELECT识别符。这是SELECT的查询序列号
select_type

SELECT类型,可以为以下任何一种:

  • SIMPLE:简单SELECT(不使用UNION或子查询)
  • PRIMARY:最外面的SELECT
  • UNION:UNION中的第二个或后面的SELECT语句
  • DEPENDENT UNION:UNION中的第二个或后面的SELECT语句,取决于外面的查询
  • UNION RESULT:UNION 的结果
  • SUBQUERY:子查询中的第一个SELECT
  • DEPENDENT SUBQUERY:子查询中的第一个SELECT,取决于外面的查询
  • DERIVED:导出表的SELECT(FROM子句的子查询)
table

输出的行所引用的表

type

联接类型。下面给出各种联接类型,按照从最佳类型到最坏类型进行排序:

  • system:表仅有一行(=系统表)。这是const联接类型的一个特例。
  • const:表最多有一个匹配行,它将在查询开始时被读取。因为仅有一行,在这行的列值可被优化器剩余部分认为是常数。const表很快,因为它们只读取一次!
  • eq_ref:对于每个来自于前面的表的行组合,从该表中读取一行。这可能是最好的联接类型,除了const类型。
  • ref:对于每个来自于前面的表的行组合,所有有匹配索引值的行将从这张表中读取。
  • ref_or_null:该联接类型如同ref,但是添加了MySQL可以专门搜索包含NULL值的行。
  • index_merge:该联接类型表示使用了索引合并优化方法。
  • unique_subquery:该类型替换了下面形式的IN子查询的ref: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subquery是一个索引查找函数,可以完全替换子查询,效率更高。
  • index_subquery:该联接类型类似于unique_subquery。可以替换IN子查询,但只适合下列形式的子查询中的非唯一索引: value IN (SELECT key_column FROM single_table WHERE some_expr)
  • range:只检索给定范围的行,使用一个索引来选择行。
  • index:该联接类型与ALL相同,除了只有索引树被扫描。这通常比ALL快,因为索引文件通常比数据文件小。
  • ALL:对于每个来自于先前的表的行组合,进行完整的表扫描。
possible_keys

指出MySQL能使用哪个索引在该表中找到行

key 显示MySQL实际决定使用的键(索引)。如果没有选择索引,键是NULL。
key_len 显示MySQL决定使用的键长度。如果键是NULL,则长度为NULL。
ref 显示使用哪个列或常数与key一起从表中选择行。
rows 显示MySQL认为它执行查询时必须检查的行数。多行之间的数据相乘可以估算要处理的行数。
filtered 显示了通过条件过滤出的行数的百分比估计值。
Extra

该列包含MySQL解决查询的详细信息

  • Distinct:MySQL发现第1个匹配行后,停止为当前的行组合搜索更多的行。
  • Not exists:MySQL能够对查询进行LEFT JOIN优化,发现1个匹配LEFT JOIN标准的行后,不再为前面的的行组合在该表内检查更多的行。
  • range checked for each record (index map: #):MySQL没有发现好的可以使用的索引,但发现如果来自前面的表的列值已知,可能部分索引可以使用。
  • Using filesort:MySQL需要额外的一次传递,以找出如何按排序顺序检索行。
  • Using index:从只使用索引树中的信息而不需要进一步搜索读取实际的行来检索表中的列信息。
  • Using temporary:为了解决查询,MySQL需要创建一个临时表来容纳结果。
  • Using where:WHERE 子句用于限制哪一个行匹配下一个表或发送到客户。
  • Using sort_union(...), Using union(...), Using intersect(...):这些函数说明如何为index_merge联接类型合并索引扫描。
  • Using index for group-by:类似于访问表的Using index方式,Using index for group-by表示MySQL发现了一个索引,可以用来查 询GROUP BY或DISTINCT查询的所有列,而不要额外搜索硬盘访问实际的表。

type:这是重要的列,显示连接使用了何种类型。从最好到最差的连接类型为const、eq_reg、ref、range、index和ALL

         type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
         一般来说,得保证查询至少达到range级别,最好能达到ref。

我们在进行条件查询时,建议使用索引,否则将引起全表扫描,IO的开销和程序的性能都没法保证!

Guess you like

Origin www.cnblogs.com/guoyachao/p/11225358.html