1. What is the MySQL execution plan
To have a good understanding of the execution plan, you need to have a simple understanding of the basic structure of MySQL and the basic principles of query.
The functional architecture of MySQL itself is divided into three parts, namely the application layer, the logical layer, and the physical layer. Not only MySQL, but most other database products are divided according to this architecture.
- The application layer is mainly responsible for interacting with the client, establishing links, remembering the link status, returning data, and responding to requests. This layer deals with the client.
- The logic layer is mainly responsible for query processing, transaction management and other database function processing. Take query as an example.
After first receiving the query SQL, the database will immediately allocate a thread to process it. In the first step, the query processor will optimize the SQL query. After optimization, an execution plan will be generated , and then handed over to the plan executor for execution.
The plan executor needs to access the lower-level transaction manager and the storage manager to operate the data. Their respective divisions of labor are different. Finally, the query structure information is obtained by calling the file of the physical layer, and the final result is responded to the application layer.
- The physical layer, the files stored on the actual physical disk, mainly include sub-text data files and log files.
Through the above description, generating an execution plan is an indispensable step for executing a SQL. The performance of a SQL can be seen intuitively by viewing the execution plan. The execution plan provides various query types and levels. View and as a basis for performance analysis.
2. How to analyze the execution plan
MySQL provides us with the explain keyword to visually view the execution plan of a SQL.
The explain shows how MySQL uses indexes to process select statements and join tables, which can help choose better indexes and write more optimized queries.
Next we use explain to make a query, as follows:
mysql> explain select * from payment;
+----+-------------+---------+------------+------+---------------+------+---------+------+-------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+-------+----------+-------+
| 1 | SIMPLE | payment | NULL | ALL | NULL | NULL | NULL | NULL | 16086 | 100.00 | NULL |
+----+-------------+---------+------------+------+---------------+------+---------+------+-------+----------+-------+
1 row in set, 1 warning (0.01 sec)
There are 12 columns in the query structure. Understanding the meaning of each column is crucial to understanding the execution plan. The following is explained in the form of a table.
column name | illustrate |
---|---|
id | SELECT identifier, which is the query sequence number of the SELECT. |
select_type | SELECT type, which can be any of the following:
|
table | the table referenced by the output row |
partitions | If the query is based on a partitioned table, displays the partition the query will access. |
type | Join type. The various join types are given below, ordered from best to worst:
Generally speaking, it is necessary to ensure that the query reaches at least the range level, preferably ref. |
possible_keys | Indicates which index MySQL can use to find rows in this table |
key | Shows the keys (indexes) 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. Shorter lengths are better without loss of accuracy |
ref | Shows which column or constant is used with key to select rows from the table. |
rows | 显示MySQL认为它执行查询时必须检查的行数。多行之间的数据相乘可以估算要处理的行数。 |
filtered | 显示了通过条件过滤出的行数的百分比估计值。 |
Extra | 该列包含MySQL解决查询的详细信息
|
根据上述表格,可以在执行计划分析上提供很好的帮助。