Use analysis of EXPLAIN for query optimization

  • 2020-05-17 06:44:03
  • OfStack

How do we know the overhead of executing an SQL query? MySQL provides us with the EXPLAIN keyword. Add the EXPLAIN keyword to your select statement. MySQL will explain how it handles the SELECT query, providing information about how the tables are joined and the order in which they are joined, the number of records scanned, and so on, which you can use to optimize your SQL query.
EXPLAIN select id, username from userinfo where username like '%peng%';
We prefixed the query with the EXPLAIN keyword, and we got the following report.


id: 1
select_type: SIMPLE
table: userinfo
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
rel: NULL
rows: 6
Extra: Using where

Roughly explain the meaning of each data item:
id: SELECT identifier, SELECT query sequence number;
select_type: type SELECT, which can be SIMPLE (simple query), PRIMARY (outermost select), etc.
table: the table used
type: connection type
possible_keys: available indexed columns
key: the actual index column used
key_len: key length
rel: use which column or constant with key1 to start selecting rows from the table
rows: number of rows checked
Extra: this column contains the details for MySQL to resolve the query

For a more detailed explanation, please refer to the MySQL manual. There is no point in copying the contents of the manual again. Once you have the reference data, you can more accurately optimize the MySQL query.


Related articles: