当前位置:网站首页>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】
List of articles
One 、Explain Tool is introduced
EXPLAIN Keyword can simulate optimizer execution SQL sentence , Analyze the estimated execution of your query statements .
staySELECTAdd... Before the statementexplainkeyword ,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
selectThe 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 :
simple: Simple query , The query does not contain subqueries and union.primary: The outermost layer of complex queries select.subquery: Included in select Subqueries in .derived: Included in from Subquery in Clause ,MySQL The results are stored in a temporary table , Also known as derived tables (derived table)union: stay union The second and subsequent select
2.3、table
This column indicates explain Which table is being accessed by a row of .
- 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 .- 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.
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 .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 .system:system yes const The special case of , When there is only one tuple in the table, it is system.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.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 .range: Range scan usually appears in in(), between ,> ,<, >= And so on . Use an index to retrieve a given range of rows .- 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 .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 :
- String type :
char(n)andvarchar(n),5.0.3 In later versions ,n Both represent the number of characters , Not the number of bytes , If it isutf-8, A number or letter takes up 1 Bytes , A Chinese character occupies 3 Bytes .char(n): If you save Chinese characters, the length is3nbytevarchar(n): If you save Chinese characters, the length is3n + 2byte , Plus 2 Bytes are used to store string length , becausevarcharIs a variable length string- value type
tinyint:1 bytesmallint:2 byteint:4 bytebigint:8 byte- Time type
date:3 bytetimestamp:4 bytedatetime:8 byte
Be careful:
- If the field is allowed to be NULL, need 1 Is the byte record NULL
- 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 :
Using index: Use overlay index .Using where: Use where Statement to process the result , And the columns of the query are not covered by the index .Using index condition: The columns of the query are not completely covered by the index ,where In the condition is a leading range .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 .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 .Select tables optimized away: When using some aggregate functions ( such as max、min) To access a field with an index , This result will appear .
边栏推荐
- Netcore3.1 JSON web token Middleware
- Acrel-EMS综合能效平台在校园建设的意义
- 公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
- Some basic functions of enterprise projects are developed, and important things are saved to online first a
- 两个数相互替换
- 巩固表达式C# 案例简单变量运算
- 2022.07.03 (LC 6108 decryption message)
- 2022.07.03 (LC 6109 number of people who know secrets)
- 同事的接口文档我每次看着就头大,毛病多多。。。
- Cross domain request
猜你喜欢

lambda expressions

如何有效对直流列头柜进行监测

Microservice

Face recognition 5- insight face padding code practice notes

兩個數相互替換

Parsing of XML

How to effectively monitor the DC column head cabinet
![P3304 [sdoi2013] diameter (diameter of tree)](/img/5c/984675bf4517481f80f54657c6c7ad.png)
P3304 [sdoi2013] diameter (diameter of tree)

Hisilicon 3559 universal platform construction: YUV422 pit stepping record

Illustrated network: what is gateway load balancing protocol GLBP?
随机推荐
abc 258 G - Triangle(bitset)
业务实现-日志写到同一个行数据里面
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
Detailed explanation of openharmony resource management
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
Significance of acrel EMS integrated energy efficiency platform in campus construction
Verilog tutorial (11) initial block in Verilog
基本放大电路的学习
AcWing164. 可达性统计(拓扑排序+bitset)
Learning of basic amplification circuit
Microservice
XML的解析
Face recognition 5- insight face padding code practice notes
Tester's algorithm interview question - find mode
华为200万年薪聘请数据治理专家!背后的千亿市场值得关注
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
Advanced template
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
2022.07.03 (LC 6109 number of people who know secrets)
Netcore3.1 JSON web token Middleware