当前位置:网站首页>MySQL uses the explain tool to view the execution plan

MySQL uses the explain tool to view the execution plan

2022-07-05 00:28:00 it00zyq

One 、Explain Tool is introduced

EXPLAIN Keyword can simulate optimizer execution SQL sentence , Analyze the estimated execution of your query statements .
stay SELECT Add... Before the statement explain keyword ,MySQL A tag will be set on the query , Execution query will return execution plan information , instead of
Implement this SQL.
Be careful : If from Contains subqueries , The subquery will still be executed , Put the results in a temporary table .

Two 、Explain Field information in

2.1、id

id The number of the column is select The serial number of , Every id Corresponding to one select.id The higher the execution priority is ,id The same goes from top to bottom ,id by NULL Finally, execute .

2.2、 select_type

select_type Indicates the corresponding SQL Is the statement a simple or complex query :

  1. simple: Simple query , The query does not contain subqueries and union.
  2. primary: The outermost layer of complex queries select.
  3. subquery: Included in select Subqueries in .
  4. derived: Included in from Subquery in Clause ,MySQL The results are stored in a temporary table , Also known as derived tables (derived table)
  5. union: stay union The second and subsequent select

2.3、table

This column indicates explain Which table is being accessed by a row of .

  1. When from When there are subqueries in clause ,table The column is <derivenN> Format , Indicates the current query dependency id=N Query for , So first execute id=N Query for .
  2. When there is union when ,UNION RESULT Of table The value of the column <union1,2>,1 and 2 To participate in union Of select That's ok id.

2.4、type

This column represents the association type or access type , namely MySQL Decide how to look up rows in the table , Find the approximate range of data row records .
From the best to the worst are :
system > const > eq_ref > ref > range > index > ALL
Generally speaking , We need to make sure that the query reaches range Level , It's best to achieve ref.

  1. NULL:MySQL Be able to decompose query statements in optimization phase , You no longer need to access tables or indexes during execution . for example : Select the minimum value... In the index column , can
    Complete with a separate lookup index , There is no need to access the table at execution time .
  2. const:MySQL Can optimize some part of the query and convert it into a constant ( You can see show warnings Result ). be used for primary key or unique key When all the columns of are compared with constants , So the table has at most one matching row , Read 1 Time , It's quite fast .
  3. system:system yes const The special case of , When there is only one tuple in the table, it is system.
  4. eq_ref:primary key or unique key All parts of the index are connected and used , At most, only one matching record will be returned . ordinary select This is not the case with queries type.
  5. ref: comparison eq_ref, Don't use unique index , Instead, use the partial prefix of a normal index or a unique index , The index should be compared with a certain value , May be
    Multiple qualified rows found .
  6. range: Range scan usually appears in in(), between ,> ,<, >= And so on . Use an index to retrieve a given range of rows .
  7. index: Scan the full index to get the results , Generally scan a secondary index , This kind of scan does not start from the root node of the index tree to find quickly , Instead of directly
    Traverse and scan the leaf nodes of the secondary index , The speed is still relatively slow , This kind of query usually uses overlay index , The secondary index is generally small , So this
    Species usually ratio ALL faster .
  8. ALL: That is, full scan , Scan all leaf nodes of your clustered index . In general, this requires adding indexes to optimize .

2.5、possible_keys

This column shows which indexes the query might use to find .
explain May appear possible_keys Listed , and key Show NULL The situation of , This is because there is not much data in the table ,MySQL Think index is not helpful for this query , Select full table query . If the column is NULL, There is no relevant index . under these circumstances , It can pass the inspection where Clause to see if you can create an appropriate index to improve query performance , And then use explain See the effect .

2.6、key Column

This column shows MySQL Which index is actually used to optimize access to the table .
If index is not used , Then the column is NULL. If you want to force MySQL To use or neglect possible_keys Index in column , Use in query force
index、ignore index.

2.7、key_len Column

key_len Column shows MySQL The number of bytes used in the index , With this value, you can figure out which columns in the index are specifically used .

key_len Calculation rules :

  1. String type :
    char(n) and varchar(n),5.0.3 In later versions ,n Both represent the number of characters , Not the number of bytes , If it is utf-8, A number or letter takes up 1 Bytes , A Chinese character occupies 3 Bytes .
    char(n): If you save Chinese characters, the length is 3n byte
    varchar(n): If you save Chinese characters, the length is 3n + 2 byte , Plus 2 Bytes are used to store string length , because varchar Is a variable length string
  2. value type
    tinyint:1 byte
    smallint:2 byte
    int:4 byte
    bigint:8 byte
  3. Time type
    date:3 byte
    timestamp:4 byte
    datetime:8 byte

Be careful

  1. If the field is allowed to be NULL, need 1 Is the byte record NULL
  2. The maximum index length is 768 byte , When the string is too long ,MySQL Will do a similar left prefix index processing , Extract the first half of the characters and index them .

2.8、ref Column

ref The column shows in key In the index of the column record , The column or constant used to find the value of the table , Common are :const、 Field name .

2.9、rows

rows The column is MySQL Estimate the number of rows to read and detect , Note that this is not the number of lines in the result set .

2.10、extra

This column shows additional information . Common important values are as follows :

  1. Using index: Use overlay index .
  2. Using where: Use where Statement to process the result , And the columns of the query are not covered by the index .
  3. Using index condition: The columns of the query are not completely covered by the index ,where In the condition is a leading range .
  4. Using temporary:MySQL You need to create a temporary table to process the query . This situation is generally to be optimized , The first is to think of using rope
    Lead to optimization .
  5. Using filesort: Will use external sort instead of index sort , Sort data from memory when data is small , Otherwise, you need to finish sorting on disk . In this case, we should also consider using index to optimize .
  6. Select tables optimized away: When using some aggregate functions ( such as max、min) To access a field with an index , This result will appear .
原网站

版权声明
本文为[it00zyq]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141121359307.html