Explain output analysis of high-performance MySQL

MySQL The Explain output analysis

background

Previous article written MySQL transaction and lock this article we talk about the MySQL Explain, estimated that more or less everyone will come into contact with this job or interview. Perhaps not work in actual use, but regardless of their learning or an interview, you need to grasp.

Explain can be used in SELECT, DELETE, INSERT, REPLACE, and UPDATE statements, the results of the implementation of detailed information for each table used will be displayed in each line. Simple statement may result only one line, but there will be many complex query rows of data.

Explain the use of

In front of the SQL statement with explain, such as: EXPLAIN SELECT * FROM a;

for example


CREATE TABLE `a` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_uid` (`uid`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

With the above statement EXPLAIN SELECT * FROM a;

Results are as follows

 

We can see by the picture after the execution will output 12 fields, each field What does it mean? We look at each look

Explain output of the field contents

id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows,filtered,extra

Select_type query type identifier column name meaning id query the table to check the current row of the table partitions of a partition type matches the type of access possible_keys query the index might use index keymysql decided to optimize the query key_len index key length before the ref display the table lookup value in the index key columns of records used in the columns or rows constant query the number of lines scanned, estimated value, not necessarily accurate filtered query table row per table of the percentage of extra additional directory assistance information

Common Fields Details

1.id: identify the entire query sequence is used to select statement, the first implementation in a nested query statements larger id

2.select_type:

• simple: Simple SELECT (not using UNION or subqueries)

• primary: the outermost SELECT

• union: UNION in the second SELECT statement or higher

• dependent union: UNION in the second SELECT statement or higher, depending on the outer query

• union result: UNION results

• subquery: select first in the sub-query SELECT

• dependent subquery: the first sub-query SELECT, dependent on outer query

• derived: derived table - the temporary table is derived from the sub-queries, located in the sub-query from

• uncacheable subquery: not cache the results of the sub-query, you must query an external recalculated each row

• uncacheable union: UNION in the second or later selections are not cached sub-queries

3.table: each row in the table name referenced

4.type: from top to bottom in order to reduce the effect of

• system: const a special case, only one row in the data table

• const: When determining a maximum of only one line matching the time, MySQL optimizer will read it before the inquiry and read only once, so very quickly. Using the primary key query is often const level, very efficient

• eq_ref: Up to return only a qualifying record by using two tables in the associated field time

• ref: by ordinary index that match the query are many lines of type

• fulltext: full-text index

• ref_or_null: with a similar effect ref, but more a condition not null columns

• index_merge: This connection type represents the use of the Index Merge optimization. In this case, key column in the output row index list comprising using, key_len longest partial list comprising the key of the index

• unique_subquery: In the case in the query will replace eq_ref

• range: range scan, an index scan limit. column shows which key to use the index. When use =,> <, <, <=,>,> =, IS NULL,> = <, BETWEEN or IN operator, comparing the keyword column with a constant, the range may be used

• index: Similar full table scan, but when scanning the table rather than in a row by index order. The main advantage is that it avoids the sort, but the cost is still very large. Using index as seen in the Extra column, indicating that the index is being used to cover only the scan data of the index, it is more than a full table scan order according to the index overhead is much smaller

• ALL: full table scan

5.possible_key: MySQL index might take, but do not necessarily use

6.key: index name is really using MySQL

7.rows: Estimated number of scanning lines, inaccurate reference only

8.extra: This column contains a lot of additional information, including whether the files are sorted, whether temporary tables, etc., many people in this field can provide a lot of useful information

summary

Today simple to introduce some output Explain, many times we may rarely contacted, but we still have a lot of time to master. Some things have to remember this in mind, the back will have back, after all, this society is, than who know more.

Guess you like

Origin www.cnblogs.com/xuxiaoming8/p/11628813.html