当前位置:网站首页>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 .
边栏推荐
- Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
- Ansa secondary development - two methods of drawing the middle surface
- Optimization of network request success rate in IM instant messaging software development
- Sort 1-insert sort and Hill sort
- 8051 series MCU firmware upgrade IAP
- Multiple commands produce ‘.../xxx.app/Assets.car‘问题
- Li Hongyi, machine learning 4. Deep learning
- Ansa secondary development - Introduction to interface development tools
- The epidemic dividend disappeared, and the "home fitness" foam dissipated
- flashfxp 530 User cannot log in. ftp
猜你喜欢

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

排序5-计数排序

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

队列的介绍与实现(详解)

ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境

【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)

Leetcode topic

ANSA二次开发 - 抽中面的两种方法

在abaqus中使用PyQt设计GUI

Sort 1-insert sort and Hill sort
随机推荐
微软100题-天天做-第16题
日常开发方案设计指北
laravel
“蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
Splash (rendering JS service) introduction installation
ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)
About standard IO buffers
LeetCode-学会对无序链表进行插入排序(详解)
HM secondary development - data names and its use
WSL+Valgrind+Clion
栈的介绍与实现(详解)
每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...
nowcode-学会删除链表中重复元素两题(详解)
Wechat official account to obtain material list
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
WSL+Valgrind+Clion
ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 a.ancestor lca+ violence count
使用js直传oss阿里云存储文件,解决大文件上传服务器限制