When it comes to MySQL optimization, the first thing we need to know is how our current SQL statement is executed in the actual database before we can talk about how to optimize it. In MySQL, we are provided with a very useful keyword for simulating statement execution: EXPLAIN. EXPLAIN can be used to view the execution effect of SQL statements, which can help select better indexes and optimize query statements, and write better optimized statements. So today we will talk about some basic usage and applications of this keyword. 1. UsageThe usage of EXPLAIN is very simple: mysql> EXPLAIN SELECT * FROM user; Simply put, add the EXPLAIN keyword in front of the original SQL statement, or add the EXPLAIN keyword followed by the SQL statement you want to check. 2. Output results The output of the EXPLAIN statement is the data we want and the focus of our analysis. +----+-------------+-------+------------+------+---------------+------+---------+------+------+------+------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+------+------+ | 1 | SIMPLE | user | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL | +----+-------------+-------+------------+------+---------------+------+---------+------+------+------+------+ The EXPLAIN statement gives us a total of 10 columns of data. Next, let's take a look at the meanings of some data columns that are more important in performance optimization. 1.idThis is the sequence number of the select query. 2.select_typeWhen our SQL statement is a non-select statement (ie delete, update...), the value of this field is the corresponding operation type (delete, update...). mysql> EXPLAIN INSERT INTO user VAULES(2,'ahong','31'); The output select_type at this time is our corresponding INSERT: +----+-------------+-------+------------+------+---------------+------+---------+------+------+------+------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+------+------+ | 1 | INSERT | user | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | +----+-------------+-------+------------+------+---------------+------+---------+------+------+------+------+ When the SQL statement is a select statement, it corresponds to some detailed select types, which can be as follows: SIMPLE: Simple SELECT (does not use UNION or subqueries, etc.) PRIMARY: the outermost SELECT UNION: The second or subsequent SELECT statement in a UNION DEPENDENT UNION: The second or subsequent SELECT statement in a UNION depends on the outer query UNION RESULT: The result of the UNION. SUBQUERY: The first SELECT in a subquery DEPENDENT SUBQUERY: The first SELECT in a subquery, which depends on the outer query DERIVED: The SELECT of the derived table (a subquery in the FROM clause) Here is an example of the simplest possible SIMPLE query: mysql> EXPLAIN SELECT * FROM user; +----+-------------+-------+------------+------+---------------+------+---------+------+------+------+------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+------+------+ | 1 | SIMPLE | user | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL | +----+-------------+-------+------------+------+---------------+------+---------+------+------+------+------+ 3.tableShows which table the data accessed in this operation is about. 4.partitionsDisplays the partitions used by the table. If you want to count the amount of company orders for ten years, you can divide the data into ten partitions, one for each year. This can greatly improve query efficiency. 5.type This is the most important column. Shows which class the connection uses and whether or not an index is used. It is the key to analyzing query performance. system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL The meanings of these situations are as follows:
Generally speaking, you must ensure that the query reaches at least the range level, preferably the ref level, otherwise performance issues may occur. 6.possible_keyDisplays the index columns that may be used by the query statement. The value may be one, multiple or null. 7.key The key column shows the index column actually used by the query statement. If null, the index is not used. mysql> explain select * from user where age = 1; The following results will be obtained: +----+-------------+-------+------------+------+---------------+------+---------+-------+------+------+------+------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+------+------+------+ | 1 | SIMPLE | user | NULL | ref | age | age | 5 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+------+------+------+ 8.key_lenDisplays the length of the index used by the current query statement. The shorter the length, the better without losing accuracy. 9.refThe column of the previous table being referenced. 10.rowsBased on the table and query, MySQL estimates the number of rows it must examine to return the final result. The larger the value of this column, the worse the query efficiency. 11.filteredA percentage value, used together with the value of the rows column, can estimate the result set of the previous table in the query execution plan (QEP) to determine the number of iterations of the join operation. Small tables drive large tables, reducing the number of joins. 12.extraThere are several types of additional information about how MySQL parses queries: The values contained in Extra are:
The above is the detailed content of the usage example of MySQL EXPLAIN statement. For more information about MySQL EXPLAIN statement, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Three steps to solve the IE address bar ICON display problem
>>: Detailed use cases of vue3 teleport
This article is from the Apache Spark Meetup held...
1. Referential Integrity Referential integrity re...
1. Change the transparency to achieve the gradual...
1. Write a split script (splitNginxLog.sh) * Beca...
Table of contents 1. The role of array: 2. Defini...
illustrate: There are two main types of nginx log...
1. Linux under VMware Workstation: 1. Update sour...
To put it simply, website construction is about &q...
Preface During the stress test, if the most direc...
Effect The pictures in the code can be changed by...
State Hooks Examples: import { useState } from ...
After reinstalling the system today, I reinstalle...
About CSS3 variables When declaring a variable, a...
Table of contents The principle of Vue asynchrono...
Pull the image docker pull mysql View the complet...