当前位置:网站首页>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 .
staySELECT
Add... Before the statementexplain
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 :
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 is3n
bytevarchar(n)
: If you save Chinese characters, the length is3n + 2
byte , Plus 2 Bytes are used to store string length , becausevarchar
Is 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 .
边栏推荐
- uniapp上传头像
- P3304 [SDOI2013]直径(树的直径)
- Netcore3.1 JSON web token Middleware
- P4281 [AHOI2008]紧急集合 / 聚会(LCA)
- 初识ROS
- Complete knapsack problem (template)
- (脚本)一键部署redis任意版本 —— 筑梦之路
- Expand your kubecl function
- Verilog tutorial (11) initial block in Verilog
- Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
猜你喜欢
随机推荐
Introduction to ACM combination counting
How to save your code works quickly to better protect your labor achievements
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
【北京大学】Tensorflow2.0-1-开篇
【C】 (written examination questions) pointer and array, pointer
abc 258 G - Triangle(bitset)
Consolidated expression C case simple variable operation
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
Is it safe to open and register new bonds? Is there any risk? Is it reliable?
城市轨道交通站应急照明疏散指示系统设计
[Peking University] tensorflow2.0-1-opening
lambda表达式
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
Verilog tutorial (11) initial block in Verilog
What did I pay for it transfer to testing post from confusion to firmness?
2022.07.03 (LC 6108 decryption message)
Huawei employs data management experts with an annual salary of 2million! The 100 billion market behind it deserves attention
基本放大电路的学习
Acrel-EMS综合能效平台在校园建设的意义