当前位置:网站首页>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 .
边栏推荐
- 实战模拟│JWT 登录认证
- Fast analysis -- easy to use intranet security software
- Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
- 兩個數相互替換
- 【路径规划】RRT增加动力模型进行轨迹规划
- Illustrated network: what is gateway load balancing protocol GLBP?
- P4281 [AHOI2008]紧急集合 / 聚会(LCA)
- leetcode494,474
- 【C】 (written examination questions) pointer and array, pointer
- Acwing164. Accessibility Statistics (topological sorting +bitset)
猜你喜欢

ORB(Oriented FAST and Rotated BRIEF)

Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?

npm install报错 强制安装

lambda表达式

【C】(笔试题)指针与数组,指针

【雅思阅读】王希伟阅读P3(Heading)

Parsing of XML
![[IELTS reading] Wang Xiwei reading P4 (matching1)](/img/91/1b3f85410035f65acb0c205185f698.png)
[IELTS reading] Wang Xiwei reading P4 (matching1)

leetcode518,377

uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
随机推荐
[IELTS reading] Wang Xiwei reading P3 (heading)
Application of fire fighting system based on 3D GIS platform
打新债开户注册安全吗?有没有风险的?靠谱吗?
He worked as a foreign lead and paid off all the housing loans in a year
雅思考试流程、需要具体注意些什么、怎么复习?
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
js如何实现数组转树
Distributed base theory
Get to know ROS for the first time
IT转测试岗,从迷茫到坚定我究竟付出了什么?
How many triangles are there in the golden K-line diagram?
2022.07.03 (LC 6108 decryption message)
Upload avatar on uniapp
Acrel-EMS综合能效平台在校园建设的意义
Netcore3.1 JSON web token Middleware
如何有效对直流列头柜进行监测
《论文笔记》Multi-UAV Collaborative Monocular SLAM
JS how to realize array to tree
两个数相互替换
Basic points of the game setup of the points mall