当前位置:网站首页>[advanced MySQL] optimize SQL by using the execution plan explain (2)
[advanced MySQL] optimize SQL by using the execution plan explain (2)
2022-06-10 14:04:00 【wu55555】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 16 God , Click to see the event details
0. introduction
In actual development , We often need to target some of the more complex SQL To optimize , First we need to understand SQL Specific implementation and process of , But how do you know ? Know how to optimize our sql What about efficiency ?
Today we are going to talk about it , How to use explain To optimize SQL
1. What is? explain? How to use ?
explain, That is, the implementation plan , yes mysql Provided to simulate optimizer execution sql The instruction of the statement , With it we can know sql The implementation effect of , It should be noted that explain Is simulated execution , It's not really execution , Therefore, the effect analysis cannot completely restore the real execution effect .
explain The use of instructions is explain+ Executes sql, such as
explain select * from user_test.order;
Copy code Results show
stay explain Official documents We can know ,explain The output of the statement contains the following information :
| Name | meaning |
|---|---|
| id | The unique identifier of the statement |
| select_type | Query type |
| table | Table name |
| partitions | Matching partition |
| type | Connection type (left join,right join etc. ) |
| possible_keys | Possible index |
| key | Index of actual selection |
| key_len | Length of index |
| ref | Which column of the index is referenced |
| rows | Estimate the number of data rows to scan |
| filtered | Percentage of data matching query criteria |
| extra | Extensions , Additional information , No fixed value , According to the execution of sql Display different information |
2. Detailed explanation of output information
Let's explain the meaning of these output information in detail
2.1 id The unique identifier of the statement
sql The serial number of , Express sql And the execution order of clauses or operation tables , The larger the number, the earlier the execution , The same number is executed from top to bottom , Illustrate with examples , We execute the following statement
EXPLAIN SELECT
*
FROM
user_test.`order`
WHERE
id IN (SELECT id FROM user_test.`user`)
Copy code According to our normal expectations , Subquery select id from user_test.user It must be done first , And then carry out external targeting order Table in the query , So let's look at the results of the execution plan
Two sentences of id All are 1, But according to the top-down rule , First, execute the command for user Table in the query , Then execute the operation for order Table in the query , In line with our expectations
2.2 select_type Query type
Query type , It is used to distinguish between normal query, joint query or sub query , We know from official documents that , Query types are divided into the following types: :
| Query type | meaning |
|---|---|
| SIMPLE | Simple query , It doesn't contain union Or subquery |
| PRIMARY | If the query contains any complex subqueries , The outermost query is marked Primary |
| UNION | The second and later select Appear in the union after , Marked as union |
| DEPENDENT UNION | stay union The second or subsequent query in , And external queries depend on union Result |
| UNION RESULT | from union Of the search results in the temporary table select |
| SUBQUERY | stay select perhaps where Subqueries included in the list |
| DEPENDENT SUBQUERY | stay select perhaps where Subqueries included in the list , And external queries depend on select Result |
| DERIVED | Derived tables , Means contained in from Clause in the subquery select |
| UNCACHEABLE SUBQUERY | Cannot cache subqueries |
| UNCACHEABLE UNION | Cannot cache union |
Let's use the specific sql To illustrate these query types (1)SIMPLE Simple query , It doesn't contain union Or subquery
explain select * from user_test.order
Copy code (2)PRIMARY The outermost query
EXPLAIN SELECT
o.id,
o.NO,
t.NAME
FROM
( SELECT id, NAME FROM user_test.USER WHERE user_name = '1'
UNION
SELECT id, NAME FROM user_test.USER WHERE PASSWORD = '1' ) t
LEFT JOIN user_test.ORDER o ON o.creator = t.id
Copy code You can see that there are complex subquery statements , So the outermost layer is for the table o(order The table alias ) The query type of is PRIMARY. There is also a table for <derived2> The query for is marked as PRIMARY, This query is for the derived table
(3)UNION The second and later select Appear in the union after , Marked as union We can see from the above example , in the light of user The second query of the table select id,name from user_test.user where password='1' Marked as union, It's because of this select Appear in the union after , And it was the third one to appear select 了
(4) UNION RESULT from union Of the search results in the temporary table select
From the above example, we can see that union A temporary watch <union2,3> Query for , In fact, it means that our table will be formed after query t, This is for union Queries for temporary tables are marked as UNION RESULT
(5)DERIVED Derived tables , Means contained in from Clause in the subquery select In the above example select id,name from user_test.user where user_name='1' Subqueries are annotated as DERIVED, This is because the table is in from In the following subquery select
(6)DEPENDENT UNION stay union The second or subsequent query in , And external queries depend on union Result
EXPLAIN SELECT
*
FROM
user_test.user
WHERE
id IN (
SELECT creator FROM user_test.ORDER WHERE address LIKE ' guiyang %'
UNION
SELECT creator FROM user_test.ORDER WHERE id=2)
Copy code As you can see from the execution results , Clause SELECT creator FROM user_test.ORDER WHERE id=2 Because it belongs to union Clause , The union The resulting results are directly related to external queries , The results of external queries depend on this union Result If you don't understand clearly here , Let's take a counter example , Let's figure out the difference ,
EXPLAIN SELECT
*
FROM
( SELECT creator AS id FROM user_test.ORDER WHERE address LIKE ' guiyang %'
UNION
SELECT creator AS id FROM user_test.ORDER WHERE id = 2 ) t
Copy code Aforementioned sql We made a little adjustment , Its outer layer is also a query statement select * from , But the difference is that it doesn't where To limit its query scope , That is to say, whether you union How the internal changes , The data it queries is still the entire union result, Unaffected , But if you have id in Conditions , In fact, the result is obviously limited , suffer union Result constraints , The difference is subtle
(7)subquery stay select perhaps where Subqueries included in the list
EXPLAIN SELECT
*
FROM
user_test.ORDER
WHERE
amount > (SELECT avg( amount ) FROM user_test.ORDER)
Copy code You can see where After the subquery select avg(amount) from user_test.order Marked as subquery
(8)dependent subquery stay select perhaps where Subqueries included in the list , And external queries depend on select Result Understand the above dependent union, Then understand dependent subquery I'll understand better , In fact, in our case 6 The first clause in SELECT creator FROM user_test.ORDER WHERE address LIKE ' guiyang %' It has been marked as dependent subquery, This is because the results of this sub query directly affect the results of external queries
(9)UNCACHEABLE SUBQUERY Cannot cache subqueries
EXPLAIN SELECT
*
FROM
user_test.ORDER
WHERE
amount > (SELECT avg( amount ) FROM user_test.ORDER where amount > @@max_connections)
Copy code @@max_connections yes mysql Parameters of , When... Is used in a statement mysql When parameters are , The result will not be cached , So we can see that the query clause is marked as UNCACHEABLE SUBQUERY, Of course, it's not the only way to use mysql The statement of the parameter will be marked as UNCACHEABLE, It should be based on sql Sentence to analyze
(10)UNCACHEABLE UNION Cannot cache union
EXPLAIN SELECT
*
from (
SELECT creator FROM user_test.ORDER WHERE address LIKE ' guiyang %'
UNION
SELECT creator FROM user_test.ORDER WHERE amount>@@max_connections) t
Copy code The conclusion is the same as above ,@@max_connections yes mysql Parameters of , When... Is used in a statement mysql When parameters are , The result will not be cached , So we can see that the query clause is marked as UNCACHEABLE UNION
2.3 table Table name
Accessing a table , If the table alias is declared , The alias will be displayed . It may also be a temporary table 、 Derived tables or union Merge tables , As our example above shows <union2,3>,<derived2>
Derived tables :<derivedN> form ,N Represents a query that produces a derived table queryId Merge tables :<unionN1,N2> form ,N2,N2 To participate in union Of inquiry queryId
2.4 partitions Matching partition
mysql Partition function is provided in , Table data can be partitioned according to certain rules , For example, partition according to the creation time , In this way, data that has been created for a long time can be divided into cold data areas , This kind of data is accessed less , Less resources are allocated , Create a hot data zone with a short time , This kind of data is accessed frequently , Allocate more resources , So as to realize the separation of hot and cold data , Improve query efficiency
So if the table has the partition function enabled , It will show that sql Partition involved , If the partition is not enabled , It will be empty
2.5 type Connection type
Connection type / Access type , It means that we should sql How to access the data , The most common is full table scanning , It is simply to facilitate the whole table to find the data we want , This method is very inefficient
So we introduced the concept of index , Based on Index , We divide the connection types into the following
system: There is only one line in the table , Generally, it only appears in some system tables , Business tables rarely appear
const: The table has at most one matching row
Read at the beginning of the query . Because there's only one line , The values of the columns in this row can be treated as constants by the rest of the optimizer . Constant tables are very fast , Because they are read only once
explain select * from user_test.order where id=2
Copy code - eq_ref: Use a unique index for data lookup
When the primary key index or unique index is used for connection, it will be used eq_ref, As shown below
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;
Copy code - ref: Non unique index is used to search data
It is used when a non unique index is used for connection eq_ref, As shown below
SELECT * FROM ref_table WHERE key_column=expr;
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;
Copy code fulltext: Data lookup using full-text indexing
ref_or_null: A certain field requires an association condition , Also needed null Situation of value
SELECT * FROM ref_table
WHERE key_column=expr OR key_column IS NULL;
Copy code - index_merge: Data lookup that requires a combination of multiple indexes
SELECT * FROM tbl_name WHERE key1 = 10 OR key2 = 20;
SELECT * FROM tbl_name
WHERE (key1 = 10 OR key2 = 20) AND non_key = 30;
SELECT * FROM t1, t2
WHERE (t1.key1 IN (1,2) OR t1.key2 LIKE 'value%')
AND t2.key1 = t1.some_col;
SELECT * FROM t1, t2
WHERE t1.key1 = 1
AND (t2.key1 = t1.some_col OR t2.key2 = t1.some_col2);
Copy code - unique_subquery: Associate subqueries with unique indexes
order In the table no It's the only index
explain select no from user_test.order where no in
(select no from user_test.products)
Copy code - index_subquery: Using indexes to associate subqueries
order In the table no It's the index
explain select no from user_test.order where no in
(select no from user_test.products)
Copy code - range: Using index queries limits the scope
such as :order In the table no and amout Fields are all indexes , Applicable operators :=,like 'xxx%',>,<,>=,<=,between and,is null,or in
explain select no from user_test.order where no ='ss'
explain select no from user_test.order where no like 'ss%'
explain select no from user_test.order where amount > 10;
explain select no from user_test.order where amount < 10;
explain select no from user_test.order where amount BETWEEN 10 and 200;
Copy code - index: Full index scan
When the data we need can be found in the index , Or when you need to use an index to sort, the connection type is index, such as : When order In the table no When the field is an index , We only query this field , Its type Namely index
explain select no from user_test.order
Copy code - ALL: Full table scan
The efficiency of the above access types from high to low is : system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
We expect the higher the efficiency, the better , It also means sql More efficient execution of , It is generally required that at least range Level , It's better to achieve ref Level
2.6 possible_keys Possible index
Displays the indexes that may be used in this table , If the columns involved in the query are index columns, they may be listed and displayed , But it is not necessarily used in queries
explain select no from user_test.order where no in
(select no from user_test.products)
Copy code As shown in the figure below ,no_index The index is used in the query , So it's listed
2.7 key Index of actual selection
The index actually used in the query
Such as 2.6 The example in shows , because order Table and products In the table no_index Corresponding field no Are used by the query , therefore key All for no_index. Let's make some changes to the example : from order There are more than columns found in no, Plus address
EXPLAIN SELECT no,address
FROM
user_test.ORDER
WHERE
NO IN (
SELECT NO
FROM
user_test.products)
Copy code Because in no The index tree cannot be taken out at one time address Data. , You still need to go back to the table to query , So the actual use of key There is no no_index 了
2.8 key_len Length of index
The number of bytes used in the index
2.9 ref Which column of the index is referenced
Shows which column of the index is used , As the following example
EXPLAIN SELECT NO
FROM
user_test.ORDER
WHERE
NO IN (
SELECT NO
FROM
user_test.products)
Copy code Yes order Table in the query , Its query conditions no in It uses products Tabular no Field
2.10 rows Estimate the number of data rows to scan
This is an estimate , Very important parameters , We can learn from this reference sql The execution needs to find multiple rows of data , As long as we can find the results we want , The less the value, the better
2.11 filtered Percentage of data matching query criteria
Percentage of data matching query criteria
2.12 extra Expand information
Some extra information , This information includes
| Extended information | meaning |
|---|---|
| using filesort | explain mysql Cannot sort by index |
| using temporary | sql Temporary tables are created and used |
| using index | The current index satisfies the coverage index , That is, the queried data is the index column , You can get the desired column directly from the index tree |
| using where | Used where sentence |
| using join buffer | Connection caching is used |
| impossible where | The result is always unsatisfied where Conditions |
summary
I believe that through the above explanation , We have a deep understanding of the implementation plan , You should experience the use scenarios of executing the plan , And apply it to your usual development tuning .
Focus on Columns , Learn more about what's new
边栏推荐
- [notes] notes on C language array pointer, structure + two-dimensional array pointer
- 新功能|Mail GPU Counter模块新增GPU图元处理和GPU Shader Cycles
- About native SQL and database methods in PHP framework
- 焱融看|混合云环境下,如何实现数据湖最优存储解决方案
- C#多线程学习笔记一
- odoo 权限管理(访问权限及记录规则)结合实用,升级角色管理
- Docker部署一个Redis集群
- [operation tutorial] how to correctly use the Hikvision demo tool to configure the channel to go online?
- 【C语言】指针函数与函数指针、数组函数
- SnackBar usage details
猜你喜欢

What does the multi cloud management platform CMP mean? Who can explain it clearly

Primary master-slave table integration process development process

UE5如何将屏幕坐标转为世界坐标和世界方向

Solve the problem that win10 virtual machine and host cannot paste and copy each other

2022危险化学品经营单位主要负责人考试题库及在线模拟考试

New features mail GPU counter module adds GPU primitive processing and GPU shader cycles
![[vue/js] realize local caching of variables and objects through localstorage browser (text + full source code)](/img/4d/d6276955277942f96f11df3e4ecd4c.png)
[vue/js] realize local caching of variables and objects through localstorage browser (text + full source code)

Design tools and skills for beginners to build their own blog
![[cloud computing] what is the relationship between a multi cloud management platform and a public cloud?](/img/8f/f57da7079979ceaef903255bc25432.png)
[cloud computing] what is the relationship between a multi cloud management platform and a public cloud?

2022 Shandong Province safety officer C certificate retraining question bank and online simulation examination
随机推荐
C multithreading learning note 2
【FAQ】運動健康服務REST API接口使用過程中常見問題和解决方法總結
Analysis on the use of coordinatorlayout
【C语言】指针函数与函数指针、数组函数
Net core Tianma XingKong series - Interface Implementation for dependency injection and mutual conversion of database tables and C entity classes
Qualcomm has finally begun to develop its own core architecture after learning from the difficulties of assembling chips to maintain its competitive advantage
5.8G微波雷达模块使用,5.8G微波雷达模块工作原理和介绍
What can the graph of training and verification indicators tell us in machine learning?
【云计算】多云管理平台和公有云两者之间是啥关系?
【离散数学期复习系列】二、一阶逻辑(谓词逻辑)
【笔记】74HC573的一些记录
For the first time, the Pentagon admitted to funding 46 biological facilities in Ukraine. Russia once revealed that only three were safe
组装芯片难保竞争优势,痛定思痛的高通终于开始自研核心架构
什么是CAS 以及 CAS 中的 ABA 问题
618 大促来袭,浅谈如何做好大促备战
列表、数组和张量之间的相互转化
Win10 virtual machine download and installation process
解决跨海高并发崩溃难题?so easy
Ue5 how to convert screen coordinates to world coordinates and World Directions
解决安装gerapy的时候报错:ERROR: Cannot uninstall ‘certifi‘. It is a distutils installed project...