Analyze the execution plan of inefficient SQL through EXPLAIN

1. First look
Insert picture description here
at the simple explanation of each column in the example as follows :
select_type : indicates the type of SELECT, common values ​​are SIMPLE (simple table, that is, no table join or subquery), PRIMARY (main query, that is, outer Query), UNION (the second or subsequent query statement in UNION), SUBQUERY (the first SELECT in a subquery), etc.
 table : the table of the output result set.
 type : indicates the connection type of the table. The connection type with performance from good to poor is system (there is only one row in the table, that is, the constant table), const (there is at most one matching row in a single table, such as primary key or unique index), eq_ref (for each row in front, only one record is queried in this table, in simple terms, it is the primary key or unique index used in the multi-table connection), ref (similar to eq_ref, the difference is that it does not use primary key or unique index, Instead, use a normal index), ref_or_null (similar to ref, the difference is that the condition contains a query for NULL), index_merge (index merge optimization), unique_subquery (in is followed by a subquery to query the primary key field), index_subquery (and unique_subquery is similar, the difference is that after in is a subquery that queries non-unique index fields), range (range query in a single table), index (for each row in front, the data is obtained by querying the index), all (for the previous Each row of the data is obtained through a full table scan).
 possible_keys : indicates the indexes that may be used when querying.
 key: Indicates the index actually used.
 key_len : the length of the index field.
rows : the number of scanned rows.
Extra : Description and description of the implementation.

Analysis
It can be seen from the type that a full table scan is used. Let's take a look at what type is by creating a unique index on user_code?
Insert picture description here
Type has become const, rows has also become 1, which is more efficient

Guess you like

Origin blog.csdn.net/weixin_38019299/article/details/108201981