当前位置:网站首页>Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
2022-07-28 16:44:00 【u012804784】
High quality resource sharing
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system |
The last article said MySQL Architecture system , come to know MySQL Server The optimizer at the end can generate Explain Implementation plan , The execution plan can help us analyze SQL Statement performance bottleneck , Optimize SQL Query logic , Study together today Explain Specific usage of execution plan .
1. explain Use
Use EXPLAIN Keyword can simulate optimizer execution SQL sentence , Analyze the performance bottleneck of your query statement or structure .
stay select Add... Before the statement explain keyword ,MySQL A tag will be set on the query , Execution query will return execution plan information , Will not implement this SQL.
Like the following one :

What is the use of outputting so many columns ?
In fact, most of them are SQL Performance statistics of statements , First, briefly summarize the general function of each column , Let's talk about it in detail :

2. explain Field details
Let's talk about the specific functions of each column in detail .
1. id Column
id Represents the sequence number of the query statement , Automatically assigned , The order is increasing , The bigger the value is. , The higher the execution priority .

id Phase at the same time , Priority is from top to bottom .

2. select_type Column
select_type Indicates the query type , Common are SIMPLE Simple query 、PRIMARY Main query 、SUBQUERY Subquery 、UNION The joint query 、UNION RESULT Joint provisional table results, etc .

3. table Column
table Express SQL Table name of statement query 、 Table alias 、 Temporary table name .

4. partitions Column
partitions Express SQL Query the partition that matches , If there is no partition NULL.

5. type Column
type Indicates the table connection type or data access type , Is how tables are connected , Or how to access the data .
The specific values are as follows , The order of performance from good to bad is :
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
system
When there is only one row in the table , That is, system table , yes const Type of specials .

const
It means to use primary key or unique index for equivalent query , At most one record is returned . Good performance , Recommended .

eq_ref
Express Table joins The primary key or unique index is used , Below SQL It uses user Table primary key id.

ref
It means to use non unique index for equivalent query .

ref_or_null
It means to use non unique index for equivalent query , And it includes null Row of values .

index_merge
Represents the optimization logic using index consolidation , That is, multiple indexes used .

range
Indicates that the index range query is used .

index
Indicates that the index is used for full table scanning .

ALL
Indicates full table scan , The worst performance .

6. possible_keys Column
Indicates the index column that may be used , Actual queries may not necessarily be used .

7. key Column
Indicates that the index column is used in the actual query .

8. key_len Column
Indicates the number of bytes occupied by the index .

The number of bytes occupied by each type is as follows :
| type | Occupancy space |
|---|---|
| char(n) | n Bytes |
| varchar(n) | 2 Bytes store variable length strings , If it is utf-8, Then the length 3n + 2 |
| tinyint | 1 Bytes |
| smallint | 2 Bytes |
| int | 4 Bytes |
| bigint | 8 Bytes |
| date | 3 Bytes |
| timestamp | 4 Bytes |
| datetime | 8 Bytes |
| Fields are allowed to be NULL | Additional increase 1 Bytes |
9. ref Column
Express where Statement or table connection parameters compared with the index , Common are const( Constant )、func( function )、 Field name .
If no index is used , It is displayed as NULL.



10. rows Column
Indicates execution SQL The number of rows scanned by the statement .

11. filtered Column
Represents the percentage of table rows filtered by criteria .

Used to estimate the number of rows scanned when connecting with other tables ,row x filtered = 252004 x 10% = 25 Line ten thousand
12. Extra Column
Indicates some additional extended information , Not suitable for display in other columns , But it is very important .
Using where
Indicates that where Condition search , But no index is used .

Using index
Indicates that the overlay index is used , That is, the required data can be found on the index , There is no need to query the table twice , Good performance .

Using filesort
Indicates that external sorting is used , That is, the sorting field does not use the index .

Using temporary
Indicates that a temporary table is used , In the following example, a temporary table is used to store query results .

Using join buffer
Indicates when performing table Association , No index is used , The connection cache is used to store temporary results .
In the following example user_id There is no index in both tables .

Using index condition
Indicates use Index push down The optimization features of .

Summary of knowledge points :
This article introduces in detail Explain Usage mode , And the meaning of each parameter . Whether it's a job or an interview , Use Explain Optimize SQL Inquire about , Are all necessary skills , Remember .
Let's learn the next part SQL Other optimization methods of query , Coming soon .

Articles are constantly updated , You can search through wechat 「 One light architecture 」 Read more technical dry goods for the first time .
边栏推荐
- “蔚来杯“2022牛客暑期多校训练营3 ACFHJ
- PHP mb_ Substr Chinese garbled code
- LwIP development | socket | TCP | client
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 j.journey 0-1 shortest path
- asp.net大文件分块上传断点续传demo
- PHP获取小程序码,小程序带参数跳转
- Wechat official account to obtain material list
- 微信公众号获取素材列表
- HyperMesh运行脚本文件的几种方法
- Optimization of network request success rate in IM instant messaging software development
猜你喜欢

Record doc

IM即时通讯开发优化提升连接成功率、速度等

ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境

I can only sell the company after the capital has been "cut off" for two years

Leetcode daily practice - the number of digits in the offer 56 array of the sword finger

栈的介绍与实现(详解)

Introduction and implementation of queue (detailed explanation)

MySQL view event status statements and modification methods

Optimization of network request success rate in IM instant messaging software development

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
随机推荐
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued
Rosen's QT journey 102 listmodel
Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
SCI scientific paper writing Growth Camp (full version)
Detailed explanation of QT qstring
栈的介绍与实现(详解)
Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
2021-04-02
一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
排序1-插入排序与希尔排序
一小时内学会Abaqus脚本编程秘籍
ANSYS二次开发 - MFC界面调用ADPL文件
后台弹出layer提示
“蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数
About mit6.828_ HW9_ Some problems of barriers xv6 homework9
FX3开发板 及 原理图
Ansa secondary development - build ansa secondary development environment on Visual Studio code