Mysql query statement analysis explain detailed

explain:
You can simulate the SQL optimizer to execute SQL statements, so that developers can know their own SQL situation.
Optimization method official website: optimization.html

explain use:
explain + sql statement

select t.* from teacher t,course c,teacherCart tc where…
explain the type analysis:

① id: The
id value is the same, and the order is executed from top to bottom; the query table order, the number of table data is executed from less to more (Cartesian product)
id is different, from the inner to the outer layer, the larger the id value, the first to query the
same id There are also differences, big first and then small, the same part, from top to bottom


②select_type
query type
primary main query
subquery subquery
simple simple query
derived derived query, using the temporary table
selec cname from(select...);
there is a union table, the left is the derived table


③table  table
④type  index type
system>const>eq_ref>ref>range>index>all
system There is only one data
const SQL that can only find one data, used for primary key and unique key
eq_ref unique index, query for each index key , Returns a row of matching data (there is only one)
common in unique index and primary key index
ref: non-unique index, returns all rows that match
range: where is followed by a range query
in, between and
index: query index column
all: query All data (not index, scan the entire table, scan all data)
⑤possible_keys
may use index null/index column, prediction is sometimes inaccurate, normal phenomenon
⑥The
index actually used by key is null, indicating that the index is not used
⑦key_len
index length. It is used to determine whether the matching index is fully used. The composite index uses several levels of index.
utf8 one character occupies 3 bytes, if the index field is null, one byte will be used for identification
1 (null) 2 (field variable length)
⑧ref
pay attention to the distinction, indicating the field referenced by the current table (const)
⑨rows
is indexed The number of data to optimize the query (number of rows)
⑩extra
Using filesort is expensive in
performance and requires an additional search before sorting. Sorting and searching will not cause the above error. The best left prefix, does not cross columns. Where and order by install compound indexes in order. Do not use
using temporary across columns or out of order. The
performance loss is large. Temporary tables are often used in order by. There is already a table but not applicable. The
query column has the same name as the grouped column
using index.
Index coverage, performance improvement; the reason is that the source file is not read, only the data is obtained from the index file (no need to return to the table query)
using where
need to return to the table query where the following conditional column is not in the index column
impossible where
where clause is forever impossible

Guess you like

Origin blog.csdn.net/weixin_43452467/article/details/113120645