Play MySQL's SQL optimization EXPLAIN execution plan

foreword

Starting today, this series of articles will take your friends to learn database technology. Database technology is an indispensable part of knowledge content in Java development. It is also a very important technology. This series of tutorials comprehensively explains the database system from the shallower to the deeper. It is very suitable for small partners with zero foundation to learn.


The full text is about [1965] words , no nonsense, just pure dry goods that allow you to learn techniques and understand principles! This article has a wealth of cases and videos with pictures, so that you can better understand and use the technical concepts in the article, and can bring you enough enlightening thinking...

1. EXPLAIN execution plan analysis

EXPLAIN can help developers analyze SQL problems. EXPLAIN shows how MySQL uses SQL execution plans, which can help developers write more optimized query statements. To use, just add EXPLAIN before the select statement.

1. For example:

The following is one of the most common query statements, analyzed and demonstrated with EXPLAIN.

EXPLAIN SELECT * FROM student;

result:
insert image description here

2. The column description of the result is as follows:

id : SELECT identifier. This is the SELECT query sequence number. This is not important.

select_type: Indicates the type of the SELECT statement.

  • simple: simple select (do not use union or subquery).
  • primary: The outermost select.
  • The second or subsequent select statement in union:union.
  • Dependent union: The second or subsequent select statement in union depends on the query outside.
  • union result: the result of union.
  • subquery: The first select in the subquery.
  • dependent subquery: The first select in the subquery depends on the outer query.
  • derived: the select of the derived table (subquery of the from clause).

table: Displays which table the query data is about.

type: Interval index, this is an important column, showing what type of connection is used. The connection types are from best to worst.

system > const > eq_ref > ref > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

Generally speaking, it is necessary to ensure that the query reaches at least the range level, and it is best to reach the ref.

  • system : There is only one row in the table, which is a special column of const type, which usually does not appear, and this can also be ignored.
  • const : The data table has at most one matching row, because only one row of data is matched, so it is very fast.
  • eq_ref : The mysql manual says this: "For each combination of rows from the previous table, read one row from this table. This is probably the best join type, except for const types. It is used on all Partially used by join and index is UNIQUE or PRIMARY KEY". eq_ref can be used to compare indexed columns using =.
  • ref : The query condition index is neither UNIQUE nor PRIMARY KEY. ref can be used on indexed columns with = or < or > operators.
  • ref_or_null : This join type is like ref, but with the addition that MySQL can specifically search for rows containing NULL values. This join type of optimization is often used in resolving subqueries.
  • index_merge : This join type indicates that the index merge optimization method is used. In this case, the key column contains the list of indexes used, and key_len contains the longest key element of the index used.
  • unique_subquery : This type replaces the ref of the IN subquery of the following form: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subquery is an index lookup function that can completely replace the subquery and is more efficient.
  • index_subquery : This join type is similar to unique_subquery. IN subqueries can be replaced, but only for non-unique indexes in subqueries of the following form: value IN (SELECT key_column FROM single_table WHERE some_expr)
  • range : retrieve only the given range of rows, using an index to select rows.
  • index : This join type is the same as ALL, except that only the index tree is scanned. This is usually faster than ALL because index files are usually smaller than data files.
  • ALL : For each combination of rows from the previous table, do a full table scan (worst performance).

possible_keys: indicates which index MySQL can use to find rows in this table. If empty, there is no associated index. To improve performance at this time, check the WHERE clause to see if certain fields are referenced, or check that the fields are not suitable for indexing.

key: The index actually used. If NULL, no index is used. If it is primary, it means that the primary key is used.

key_len: The longest index width. If the key is NULL, the length is NULL. The shorter the length, the better without loss of accuracy.

ref: shows which column or constant is used together with key to select row from table.

rows: Shows the number of rows MySQL thinks it must examine to execute the query.

Extra: Execution status description, this column contains the details of MySQL solving the query.

  • Distinct: After MySQL finds the first matching row, it stops searching for more rows for the current row combination.
  • Not exists: MySQL can perform LEFT JOIN optimization on the query, and after finding a row matching the LEFT JOIN standard, it will no longer check more rows in the table for the previous row combination.
  • range checked for each record (index map: #): MySQL did not find a good index that can be used, but found that if the column values ​​​​from the previous table are known, some indexes may be used.
  • Using filesort: MySQL requires an extra pass to figure out how to retrieve the rows in sorted order.
  • Using index: Retrieve the column information in the table from reading the actual rows using only the information in the index tree without further searching.
  • Using temporary: In order to resolve the query, MySQL needs to create a temporary table to hold the results.
  • Using where: The WHERE clause is used to restrict which row matches the next table or is sent to the client.
  • Using sort_union(…), Using union(…), Using intersect(…): These functions illustrate how to merge index scans for the index_merge join type.
  • Using index for group-by: Similar to the Using index method of accessing tables, Using index for group-by means that MySQL has found an index that can be used to query all columns of GROUP BY or DISTINCT queries, instead of additionally searching the hard disk to access the actual surface.

2. Conclusion

Finally, here is a summary of the core points of this article:

  1. This section describes how to use the EXPLAIN execution plan to analyze SQL statements and determine where the SQL statements are slow.
  2. The more important analysis fields are select_type, type, possible_keys, key, ref, rows, these columns need to be mastered.

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/130963960