[Mysql] Mysql performance optimization

explain the execution plan parameters in detail

mysql> explain select * from kt_course order by create_time desc;
+----+-------------+-----------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table     | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-----------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | kt_course | ALL  | NULL          | NULL | NULL    | NULL |   29 | Using filesort |
+----+-------------+-----------+------+---------------+------+---------+------+------+----------------+
1 row in set

select_type

1) SIMPLE: Simple SELECT, not practical UNION or subquery.
2) PRIMARY: the outermost SELECT.
3) UNION: The second layer uses UNION after SELECT.
4) DEPENDENT UNION: The second SELECT in the UNION statement depends on the external subquery.
5) UNION RESULT: The result of UNION.
6) SUBQUERY: The first SELECT in the subquery.
7) DEPENDENT SUBQUERY: The first SELECT in the subquery depends on the query outside.
8) DERIVED: SELECT of the exported table (subquery of FROM clause)

table

Show which table the data in this row is about

type

typeThe field is more important, it identifies whether the query is efficient, whether it is used 全表扫描or 索引扫描waiting

This is an important column that shows what type of connection is used.
Connection types from best to worst: const, eq_reg, ref, range, index, and ALL

Type: Tell us the access method used for the table, mainly including the following centralized types.

1) all: full table scan.
2) const: read constant, at most there will be only one record match, because it is a constant, in fact only need to read once.
3) eq_ref: There will be at most one matching result, which is generally accessed through the primary key or unique key index.
4) fulltext: full-text index search.
5) index: full index scan.
6) index_merge: Use two (or more) indexes in the query at the same time, then merge the index results (merge), and then read the table data.
7) index_subquery: The returned result field combination in the subquery is an index (or index combination), but not a primary key or unique index.
8) rang: Index range scan.
9) ref: The query referenced by the index of the driven table in the Join statement.
10) ref_or_null: The only difference from ref is the query that adds an empty value in addition to the query referenced by the index.
11) system: system table, there is only one row of data in the table;
12) unique_subquery: the combination of the returned result field in the subquery is the primary key or unique constraint.

possible_keys

Show the indexes that may be applied in this table. If it is empty, there is no possible index. You can select a suitable statement from the WHERE statement for the relevant domain

key

The actual index used. If NULL, no index is used. In rare cases, MYSQL will choose an under-optimized index. In this case, you can use USE INDEX (indexname) in the SELECT statement to force an index or use IGNORE INDEX (indexname) to force MYSQL to ignore the index

key_len

The length of the index used. Without losing accuracy, the shorter the length, the better

key_len calculation method  https://www.cnblogs.com/gomysql/p/4004244.html

ref

Show which column of the index was used, if possible, a constant

rows

The mysql query optimizer estimates the number of data rows that need to be scanned and read by sql to find the result set based on statistical information. This value can intuitively reflect the efficiency of sql. In principle, the smaller the value of rows, the better

Extra

Additional information on how MYSQL parses queries. Will be discussed in Table 4.3, but the bad examples you can see here are Using temporary and Using filesort, meaning that MYSQL cannot use indexes at all, and the result is that retrieval will be slow

Extra field explanation

Extra: Extra detailed information about each step of the query, mainly the following.

Distinct: Find the distinct value. When mysql finds the first matching result, it will stop the query of this value and switch to other values ​​later.

Full scan on NULL key: An optimization method in subqueries, mainly in the use of null values ​​that cannot be accessed by index.

Range checked for each record (index map: N): According to the description of the MySQL official manual, when MySQL Query Optimizer does not find a good index that can be used, if the column values ​​of the previous table are known, some indexes can be used. For each row combination in the previous table, MySQL checks whether the range or index_merge access method can be used to request the row.

SELECT tables optimized away: When we use certain aggregation functions to access a field that has an index, MySQL Query Optimizer will directly locate the required data rows at once through the index to complete the entire query. Of course, the premise is that there can be no GROUP BY operation in Query. When using MIN () or MAX ().

Using filesort: When ORDER BY operation is included in Query, and the index cannot be used to complete the sort operation, MySQL Query Optimizer has to choose the corresponding sorting algorithm to achieve.

Using index: The required data can be obtained in Index only, no need to go to the table to get the data.

Using index for group-by: Data access is the same as Using index, the required data only need to read the index, when using GROUP BY or DISTINCT clause in Query, if the grouping field is also in the index, the information in Extra will be Using index for group-by.

Using temporary: When MySQL must use temporary tables in certain operations, Using temporary appears in the Extra information. Mainly common in operations such as GROUP BY and ORDER BY.

Using where: If you don't read all the data in the table, or you can get all the required data just by indexing, Using where information will appear.

Using where with pushed condition: This is a message that only appears in the NDBCluster storage engine, and it also needs to be turned on by enabling the Condition Pushdown optimization function before it can be used. The control parameter is engine_condition_pushdown.

Impossible WHERE noticed after reading const tables: MySQL Query Optimizer judges that there may be no result through the collected statistical information.

No tables: Use FROM DUAL in the Query statement or do not include any FROM clause.

Not exists: In some left joins, the MySQL Query Optimizer optimizes the method used by changing the composition of the original Query, which can partially reduce the number of data accesses.

join optimization

Requirements: Statistics of the same orgid data in the org_knowledge_department_active_week table (16.64 million) and the org_knowledge_department_study_month table (2.8 million).

org_knowledge_department_active_week structure information

org_knowledge_department_study_month structure information

 

EXPLAIN
SELECT
  *
FROM
  org_knowledge_department_active_week t1
  STRAIGHT_JOIN org_knowledge_department_study_month t2
    ON t1.orgid = t2.orgid

This SQL statement execution process:

  1. Read a row of data R from table t1
  2. Take the field orgid from the data R and look it up in table t2
  3. Take out the data that meets the conditions in table t2 and form a row with R as part of the result set
  4. Repeat steps 1 to 3 until the end of t1 table

This algorithm is Index nested-Loop Join (NLJ): Nested search, and uses the index of the driven table.

t1 table

t2 table

 

join algorithm:

Index nested-Loop Join (NLJ):

 

Block Nested-Loop Join (INL):

 

in conclusion

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 61 original articles · won praise 2 · Views 7303

Guess you like

Origin blog.csdn.net/hebaojing/article/details/103192776