当前位置:网站首页>[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  Insert picture description here

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  Insert picture description here

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 

 Insert picture description here

(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  Insert picture description here

(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  Insert picture description here 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  Insert picture description here

(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

 Insert picture description here

(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  Insert picture description here

(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  Insert picture description here

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 

 Insert picture description here

  • 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

 Insert picture description here

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 了  Insert picture description here

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  Insert picture description here

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 temporarysql 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

原网站

版权声明
本文为[wu55555]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101400464018.html

随机推荐