MySQL Statement Performance Analysis

The MySQL execution plan is to put the keyword EXPLAIN before a SELECT statement, MySQL explains how it will process the SELECT, providing information about how the tables are joined and in what order. With the help of EXPLAIN you can know:

  • When must an index be added to a table to get a faster SELECT method of finding records using the index.
  • whether the optimizer joins tables in an optimal order



 The meaning of each attribute is as follows:

 

id : the serial number of the query

select_type : The type of query, mainly including ordinary query, union query and sub query.

table : The name of the table in the database being accessed.

type : The type used by the union query.

possible_keys : Indicates which index MySQL can use to find the row in this table. If this value is empty, it means there is no associated index. To improve performance at this time, you can check the WHERE clause to see if some fields are referenced, or check whether the fields are suitable for indexing.

key : Displays the key that MySQL actually decided to use. If no index is selected, the key is NULL.

key_len : Displays the key length that MySQL decided to use. If the key is NULL, the length is NULL. Note that this value reflects which part of a multiple primary key is actually used by MySQL.

ref : Shows which field or constant is used with key.

rows : This value indicates how much data MySQL has to traverse to find the desired result set, which is inaccurate on InnoDB.

Extra : If it is Only index , it means that information can only be retrieved by the information in the index tree, which is faster than scanning the entire table; if it is where used , it means that the where restriction is used, but the index is not enough; if it is impossible where , it means that the result is impossible to exist according to the collected statistical information. In addition, Extra has the following possible values: Using filesort  means that when orderby is included and the index cannot be used for sorting, the corresponding sorting algorithm must be used. using temporary uses temporary tables, common in orderby and group by. The select tables optimized way uses aggregate functions, and MySQL has fast positioning. Usually MAX, MIN, COUNT(*) and other functions.

 

It should be noted that the access type displayed by type is a more important indicator, and the result values ​​are: system (system table), const (read constant), eq_ref (at most one matching result, usually accessed through the primary key) , ref (used by the drive table index), fulltext (full-text index retrieval), ref_or_null (index query with null value), index_merge (merge index result set), unique_subquery (field returned in subquery is a unique combination or index), index_subquery (the subquery returns the index, but not the primary key), range (index range scan), index (full index scan), ALL (full table scan).

 

In general, ensure that the query reaches at least the range level, preferably the ref level. ALL is a full table scan, which is the worst case. In this case, the index is often useless.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326422222&siteId=291194637