MySQL (Advanced, Interview) - (MySQL Optimization 1) Explain detailed explanation

Table of contents

 1. What does EXPLAIN do? Use the EXPLAIN keyword to simulate the optimizer's execution of SQL query statements, so you can know how MySQL processes your SQL statements. Analyze the performance bottlenecks of your query statements or table structures.

2. The role of Explain

3. Explain the meaning and function of each field 

3. SQL execution sequence

4. Speaking in human language----Explain the meaning of each field case analysis

4.1、id

4.2、select_type 

4.3、 table

4.4、 type

4.5. possible_keys

4.6. key

4.7. key_len

4.8. ref

4.9. When querying the rows index, roughly estimate the number of rows required to read the required records. The smaller the rows, the better;

4.10. Extra



 1. What does EXPLAIN do?
   Using the EXPLAIN keyword can simulate the optimizer's execution of SQL query statements, thereby knowing how MySQL processes your SQL statements. Analyze the performance bottlenecks of your query statements or table structures.

2. The role of Explain

Of course, it guides us to write better SQL statements and improve query speed. In fact, to put it bluntly, let us write query SQL that uses indexes as much as possible, and the total number of data queried is as small as possible, thereby improving the query speed.

 By adding explain before the select+sql statement, we can see:
(1) The reading order of the table, id (table is the table corresponding to the id)
(2) The operation type of the data reading operation, select type
(3) Which indexes can be used, possible_keys
(4) Which indexes are actually used, key
(5) References between tables, ref
(6) How many rows in each table are queried by the optimizer, rows

3. Explain the meaning and function of each field 

ExplainExplanation of each field
serial number Field name Field function description
1 id The sequence number of the query, including a group of numbers, indicates the order in which the select clause or operation table is executed in the query
**Both cases**
The id is the same, but the execution order is different from top to bottom
. The larger the id value, the higher the priority. execute first
2 select_type Query type, mainly used to distinguish between ordinary queries, joint queries, subqueries, etc. complex queries
1. simple - simple select query, the query does not contain subqueries or UNION
2. primary - if the query contains any complex subqueries part, the outermost query is marked
3. subquery - the subquery contained in the select or where list
4. derived - the subquery contained in the from list is marked as derived (derived), MySQL will recursively execute these subqueries Query and put the results into a temporary table
5. Union - if the second select appears after UNION, it is marked as UNION. If union is included in the subquery of the from clause, the outer select is marked as derived
6 , union result: the result of UNION
3 table The table referenced by the output row
4 type

Display the connection type, show what type is used in the query, sort from best to worst type
1. system : There is only one row in the table (= system table). This is a special case of const connection type.
2. const : means that it can be found through the index once. const is used to compare the primary key or unique index. Because only one row of data is matched, if the primary key is placed in the where list, mysql can convert the query into a constant
3. eq_ref : unique index scan, for each index key, only one record in the table matches it. Commonly used in unique index or primary key scan
4. ref : non-unique index scan, returns all rows matching a single value, which is essentially an index access. It returns all rows matching a single value, and may find too many There are qualified rows, which is a mixture of search and scan.
5. Range : Retrieve only a given range of rows and use an index to select the rows. The key column shows which index is used. Generally, it is a query with ranges such as between and in in the where statement. This range scan index scan is better than a full table scan because it starts at a certain point in the index and ends at another point without a full table scan. 6. The difference between
index : index and all is that the index type only traverses the index tree. Usually faster than all because index files are much smaller than data files.
7. all : Traverse the entire table to find matching rows

system > const > eq_ref > ref > range > index > all


Note: It is generally guaranteed that the query reaches at least the range level, and preferably reaches the ref.

5 possible_keys Indicates which index MySQL can use to find the table ( possibly used indexes )
6 key Displays the keys (indexes) that MySQL actually decided to use . If no index is selected, the key is NULL. If a covering index is used in a query, the index overlaps with the select field of the query.
7 key_len Represents the number of bytes used in the index. This column calculates the length of the index used in the query . The shorter the length , the better without losing accuracy . If the key is NULL, the length is NULL. This field is shown as the maximum possible length of the index field, not the actual length used.
8 ref Shows which column of the index is used , if possible a constant, which columns or constants are used to query the value on the index column
9 rows Based on table statistics and index selection, roughly estimate the number of rows that need to be read to find the required records.
10 Extra Contains additional information that is not suitable for display in other columns, but is very important
1. Using filesort : Indicates that mysql will apply an external index sort to the data. Rather than reading in index order within the table. The inability to use indexes to complete the sorting operation in MySQL is called "file sorting"
2. Using temporary : A temporary table is used to save intermediate results. MySQL uses temporary tables when sorting query results. Commonly used in sorting order by and group query group by.
3. Using index : Indicates that the corresponding select operation uses a covering index to avoid accessing the data rows of the table. If using where appears at the same time, the table name index is used to perform index key value lookup; if using where does not appear at the same time, the table name index is used to read data instead of performing query actions.
4. Using where : Indicates the use of where filtering
5. Using join buffer : Uses the connection cache
6. Impossible where : The value of the where clause is always false and cannot be used to obtain any tuples
7. Select tables optimized away : When there is no group In the case of the by clause, optimizing the Min and max operations based on the index or optimizing the count (*) for the MyISAM storage engine does not have to wait until the execution stage for calculation. The optimization is completed during the query execution plan generation stage.
8. distinct: Optimize the distinct operation and stop searching for the same value after finding the first matching tuple.


 

3. SQL execution sequence

     If you want to optimize SQL, you must clearly know the execution order of SQL. In this way, you can get twice the result with half the effort by using explain!

3.1. Complete SQL statement

select distinct 
        <select_list>
from
    <left_table><join_type>
join <right_table> on <join_condition>
where
    <where_condition>
group by
    <group_by_list>
having
    <having_condition>
order by
    <order_by_condition>
limit <limit number>

3.2 SQL execution sequence
 

1、from <left_table><join_type>
2、on <join_condition>
3、<join_type> join <right_table>
4、where <where_condition>
5、group by <group_by_list>
6、having <having_condition>
7、select
8、distinct <select_list>
9、order by <order_by_condition>
10、limit <limit_number>

4. Speaking in human language----Explain the meaning of each field case analysis

4.1、id

The digital sequence number of the select query indicates the order in which the tables are operated when executing the select clause or multi-table joint query in the query ;
· (1) The IDs are the same, and the order of execution is from top to bottom.

Query the entire t2 table from the three tables t1, t2, and t3. The query conditions are: t1.id=t2.id, t1.id=t3.id, and other fields of t1 are empty; the optimizer treats the conditions inside the same
where , the default is to read from right to left;
so the execution order of the three tables is: t1 -> t3 -> t2;

(2) IDs are different. The larger the ID, the higher the execution priority.

If it is a subquery, the sequence number of the id will be incremented, and the subquery inside will be executed first, similar to the idea of ​​recursion; the statements in () have priority.

Query all of the t2 table from the t2 table. The query condition is id, and the query condition is three levels of nesting:
Query the id from the table t1 - id is the id of the t3 table queried from the table t3 - other fields of the t3 table is empty;
using recursive thinking, it is easy to judge the execution order of the table: t3 -> t1 -> t2

(3) There are situations where the IDs are the same and different at the same time. Those with different IDs and larger IDs will be executed first, and those with the same ID will be executed from top to bottom.

A concept needs to be introduced here, derived virtual table query (Derived):
partial information is intercepted from the original table, and the resulting set can form a new table. This table does not actually exist and is called as a virtual table; this kind of query It’s called a derived virtual table query;

(Query the ID of table t3 from table t3, and the query condition is that other fields are empty) Use the result set of the query condition of table t3 in brackets as a virtual table s1, and query table t2 from virtual table s1 and table t2
. All of them, the query condition is s1.id=t2.id;
therefore, the query sequence is: t3 -> derived2 -> t2
derived2 refers to the virtual table s1 formed by deriving the result set of table t3 when id=2;

4.2、select_type 

Query types, there are a total of the following 6 table query types:

· (1) SIMPLE
simple query, the query does not include subqueries and unions (joint queries);

· (2)
If the PRIMARY query contains a subquery, the outermost query is marked as PRIMARY;

· (3)
Subquery of SUBQUERY in select or where list

· (4) DERIVED
typical syntax: from (subquery) s1,
the subquery included in the from list is marked as DERIVED (derived), and the result set after the execution of this subquery is placed in a virtual table s1;

· (5) UNION
If the second select appears after union, it is marked as union multi-table query;
if union is included in the query of from clause, that is, the select attribute from (subquery 1 union subquery 2) s1, this The outer selection is marked as DERIVED;

· (6) UNION RESULT
SELECT to obtain results from the UNION table 

4.3、 table

 It refers to the table corresponding to the id, and the execution order of the table is determined by the id; it
also refers to which table the data in this row relates to;

4.4、 type

 When displaying queries, which query type is used? The following 7 types are often encountered in daily work. The performance from best to worst is:
system > const > eq_ref > ref > range > index > ALL
generally needs to guarantee the query. The type level reaches range, preferably ref, and avoid using ALL.

· (1) System
query means that there is only one row of data in the table. This is a special case of const and does not usually appear. Because there is only one row of information, it is not called big data, so it is of little significance;

· (2)const

Constant query means that it is found through the index Index once, and is used to compare primary key=constant and unique=constant indexes;
if the primary key is placed in the where list, mysql has the function of automatic type conversion, and the query will be automatically converted as a constant

Insert image description here

 d1 is a virtual table, and the parentheses have priority. Table t1 is executed first, where id = 1 is a constant query const;
virtual table d1 has only one row of data, so the query type is system query system;

· (3)eq_ref

Unique index scan. For each index key, there is only one row of data in the table, which is common in primary keys.

Insert image description here

 There are no parentheses after where. The MySQL optimizer executes from left to right. Therefore, table t2 is first executed, and type is ALL. The full table query corresponds to 639 rows of information. Then table t1 is queried.
Type is eq_ref, and the id in table 1 only corresponds to 1 row of information. , unique index scan;

· (4) ref
non-unique index scan, for each index key, there may be multiple rows of data in the table;

 In the query table t1, there are 7 unique fields col1, and the rest are all duplicates;
there are 284 rows of records that meet the query condition col1='ac', so it is a ref non-unique index scan;

 · (5) Range
query, the list after where is the query between, <, >, in, etc.;

Insert image description here

 The keywords between and type are range searches;

Insert image description here

The keywords in and type are range searches;
★ The keyword like is also a range search and will be written later;

· (6) index
full index scan, FULL INDEX SCAN, only traverses the index tree, usually faster than ALL;
(index and ALL are both full table queries, but index reads from the index, ALL reads from the full table );

· (7) ALL
full table scan, FULL TABLE SCAN, traverses the entire table to find matching rows; it is the slowest, avoid using it;

4.5. possible_keys

 If there is an index on the queried field, one or more indexes will be listed , but they may not be actually used during the query;

4.6. key

 The actual index used , if it is NULL, the index is not used. Common possible reasons:
· (1) No index is created
· (2) The SQL statement is written incorrectly and the index is invalid;
· (3) When possible_key is also NULL, it means that it is used Not indexed

4.7. key_len

You can see the number of index fields through key_len, 74 refers to 1, 78 refers to 2, and 140 refers to 3;

4.8. ref

Displays which field is being indexed, which can be a const constant;
ps: The ref in type refers to a non-unique index scan. For index fields, there may be multiple duplicate values;

Insert image description here

Query all from table t1 and table t2, query conditions: t1.col1=t2.col1, t1.col2='ac';
in the same where list, the optimizer executes from right to left, the index has two fields;
priority Execute the field value 'ac', which is a const constant;
then execute the field col1 in table t2 in the database shared, that is, shared.t2.col1;

4.9.
When querying the rows index, roughly estimate the number of rows required to read the required records. The smaller the rows, the better;

4.10. Extra

 Additional information includes the following three types:
· (1) Using filesort
indicates that the index index created and prepared for use has not been used, and file sorting has been performed;
there may be a problem with the writing of the SQL statement, which conflicts with the previously established index index. ;

· (2) Using temporary
uses a temporary table to save intermediate results, indicating that the established index is not fully used;
it is common in sorting order by and group query group by;

· (3)
Covering Index is used in the Using index select operation, which shows that the efficiency of SQL execution is good!
Covering Index:
eg: First create an index, index_field a_field b;
then select field a, field b on table where field a=..., field b=...
that is, first create an index with certain fields Index, and then query the fields in the index. The where list is the value of the index field; that is, when the index field value is XXX, query the field; this is the most efficient way to select.
··· If using where appears at the same time, it indicates that the index is used to perform the search for the index key value;
··· If using where does not appear at the same time, it indicates that the index is not used to perform the search for the index key value, but is only used to read data;
 

Guess you like

Origin blog.csdn.net/u010445301/article/details/126220108