Use of Explain keyword and index optimization

1. What is Explain?

Explain can simulate the optimizer to execute SQL query statements, so as to know how MySQL processes your SQL statements, and analyze the performance bottleneck of the query statement or table structure.

2. What is the function of Explain?

The following information can be obtained:

  • table read order
  • Which indexes can be used
  • The operation type of the data read operation
  • which indexes are actually used
  • References between tables
  • How many rows per table are physically queried
  • SQL statement performance analysis

3. How to use Explain?

Explain+SQL statement

4. What do the fields in the result obtained by Explain mean?

1.id--execution order

The sequence number of the select query, including a set of numbers, indicates the order in which the select clause or the operation table is executed in the query.

Note : Each ID number represents an independent query. The fewer the number of queries in a sql, the better.

2.select_type--query type

Note: DEPENDENT SUBQUERY

  • will seriously consume performance
  • No sub-query will be performed, the external query will be performed first, the result set will be generated, and then the associated query will be performed internally
  • The execution efficiency of the subquery is limited by the number of records in the outer query
  • You can try to change to join query

3. table - which table is being accessed

4.partitions--matched partitions

5.type--access type

NULL : mysql can decompose the query statement during the optimization phase, and does not need to access tables or indexes during the execution phase. For example: selecting the minimum value in the index column can be done by looking up the index alone, without accessing the table at execution time

The resulting values, in order from best to worst, are:

system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

Note: Generally speaking, it is necessary to ensure that the query reaches at least the range level, and it is best to reach the ref level .

① The number of results found through Const is less than or equal to 1

②system is a special case of const, and only one result is found

③eq_ref: All parts of the primary key or unique key index are used by the connection, and at most only one record that meets the conditions will be returned. This is probably the best join type besides const, and simple select queries don't have this type. [In this case, the performance is very good and no optimization is required]

④ref: Compared with eq_ref, it does not use a unique index, but uses a part of the prefix of a normal index or a unique index. The index needs to be compared with a certain value, and multiple eligible rows may be found

We require that the range level is generally reached, and it is best to reach the ref level, but generally as long as it is not the ALL level, it is ok, if it is the ALL level, it needs to be optimized!

6. possible_keys-- possible index

Displays the indexes, one or more, that may be applied to this table. 

If there is an index on the fields involved in the query, the index will be listed, but not necessarily actually used by the query.

7.key--the index actually used

The actual index to use. If it is NULL, no index is used; if a covering index is used in the query, the index overlaps with the select field of the query.

8.key_len--The number of bytes used in the index

The length of the index used in the query can be calculated from this column. The key_len field can help you check whether the index is fully utilized.

How to calculate?

  1. First look at the type + length of the field on the index, such as int=4; varchar(20) =20; char(20) =20
  2. If it is a string field such as varchar or char, different values ​​should be multiplied depending on the character set, for example, utf-8 should be multiplied by 3, GBK should be multiplied by 2
  3. A dynamic string like varchar needs to add 2 bytes
  4. Fields that are allowed to be empty need to add 1 byte

example:

Example:

 We can know which indexes it uses by the length of the field! !

9.ref--Display the columns used in the index

A constant indicating which column of the index is used, if possible. Which columns or constants are used to look up values ​​on indexed columns.

10.rows--The number of rows that must be checked

11.filtered--filter ratio

Indicates the ratio of the number of records left to satisfy the query after the data returned by the storage engine is filtered by the server layer . Note that it is a percentage, not a specific number of records

12.Extra

5. Mysql index optimization

Detailed explanation of index optimization_Baidu's blog-CSDN blog_index optimization

Guess you like

Origin blog.csdn.net/young_man2/article/details/126725634