How to view the execution plan of MySQL?
To view the execution plan of MySQL queries, you can use the keyword EXPLAIN. Simply place the EXPLAIN keyword before the SQL statement and then execute the query. For example:
Can you provide an explanation for the query “SELECT * FROM table_name”?
This will return an execution plan, showing how MySQL will execute the query and providing information about the indexes and operations used by the query optimizer.
The meanings of each column in the execution plan are as follows:
- ID: Displays the unique identifier for the query. For the same query, the ID remains the same.
- select_type: the type of query being performed, such as SIMPLE or SUBQUERY.
- The table displays the names of the tables involved in the query.
- Partitions: Display information about the partitions involved in the query.
- Type: Specifies the access method used to execute queries, such as ALL (full table scan), index (using an index), or range (using a range scan).
- possible_keys: shows the indexes that could possibly be used in a query.
- Key: Displays the index actually used in the query.
- key_len: indicates the length of the index used in the query.
- Reference: Displays the reference column of the index used in the query.
- rows: displays the number of rows scanned in the query.
- Filtered: Displays the filtering ratio between the results of the query and the total number of rows in the table.
- Show additional execution information, such as the use of temporary tables or file sorting.
By analyzing the execution plan, one can understand the performance of a query and optimize it.