Explain performance analysis

1. MySQL common bottleneck

CPU:

  • Compare, correlate, sort, and group large amounts of data in SQL; the biggest pressure is on comparison

I:

  • The instance memory cannot meet the needs of caching data or sorting, resulting in a lot of physical IO.
  • The query execution efficiency is low, scanning too many data rows.

lock:

  • Inappropriate lock settings lead to thread blocking and performance degradation.
  • Deadlock, cross-call resources between threads, resulting in deadlock, the program is stuck.

Server hardware performance bottlenecks:

  • top, free, iostat and vmstat to view the system performance status

2. Explain performance analysis

1. Concept

Using the EXPLAIN keyword can simulate the optimizer executing SQL query statements, so you know how MySQL processes your SQL statements. Analyze the performance bottleneck of your query or table structure.

Usage : Explain + SQL statement.

Information returned after Explain is executed:
Insert picture description here

What can you do:

  • Table reading order
  • Which indexes can be used
  • Operation type of data read operation
  • Which indexes are actually used
  • References between tables
  • How many rows in each table are queried by the optimizer

2. Explain preparation

CREATE TABLE t1(id INT(10) AUTO_INCREMENT,content VARCHAR(100) NULL , PRIMARY KEY (id));
CREATE TABLE t2(id INT(10) AUTO_INCREMENT,content VARCHAR(100) NULL , PRIMARY KEY (id));
CREATE TABLE t3(id INT(10) AUTO_INCREMENT,content VARCHAR(100) NULL , PRIMARY KEY (id));
CREATE TABLE t4(id INT(10) AUTO_INCREMENT,content VARCHAR(100) NULL , PRIMARY KEY (id));
INSERT INTO t1(content) VALUES(CONCAT('t1_',FLOOR(1+RAND()*1000)));
INSERT INTO t2(content) VALUES(CONCAT('t2_',FLOOR(1+RAND()*1000)));
INSERT INTO t3(content) VALUES(CONCAT('t3_',FLOOR(1+RAND()*1000)));
INSERT INTO t4(content) VALUES(CONCAT('t4_',FLOOR(1+RAND()*1000)));

3. id

The sequence number of the select query, which contains a set of numbers, indicating the order in which the select clause or operation table is executed in the query.
Insert picture description here
Note: If the id is the same, it can be considered as a group, and it will be executed sequentially from top to bottom; in all groups, the larger the id value, the higher the priority, the earlier it will be executed; Derive = DERIVED

Concern : Each number of the id number represents an independent query. The fewer the number of query trips for a sql, the better.

4. select_type

select_type represents the type of query, which is mainly used to distinguish complex queries such as ordinary queries, joint queries, and subqueries.

select_type attribute meaning
SIMPLE Simple select query, the query does not contain subqueries or UNION
PRIMARY If the query contains any complex sub-parts, the outermost query is marked as Primary
DERIVED The subqueries included in the FROM list are marked as DERIVED (derived), MySQL will recursively execute these subqueries, and put the results in a temporary table.
SUBQUERY The subquery is included in the SELECT or WHERE list
DEPEDENT SUBQUERY A subquery is included in the SELECT or WHERE list, the subquery is based on the outer layer
UNCACHEABLE SUBQUERY Cannot use cached subquery
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 will be marked as: DERIVED
UNION RESULT SELECT to get results from UNION table

4.1 SIMPLE

SIMPLE stands for single table query;
Insert picture description here
4.2 PRIMARY

If the query contains any complex sub-parts, the outermost query is marked as Primary.
Insert picture description here
4.3 DERIVED

The subqueries included in the FROM list are marked as DERIVED (derived), MySQL will recursively execute these subqueries, and put the results in a temporary table.

4.4 SUBQUERY

The subquery is included in the SELECT or WHERE list.
Insert picture description here
4.5 DEPENDENT SUBQUERY

A subquery is included in the SELECT or WHERE list, and the subquery is based on the outer layer.
Insert picture description here
Both are conditions behind where, subquery is a single value, and dependent subquery is a set of values.

4.6 UNCACHEABLE SUBQUREY
Insert picture description here
When @@ is used to refer to system variables, the cache will not be used.

4.7 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 will be marked as: DERIVED.

Insert picture description here
4.8 UNION RESULT

SELECT to get the result from the UNION table.

5. table

Which table is this data based on.

6. type

type is the access type of the query. It is a more important indicator, and the result values ​​from best to worst are:

system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index >ALL

In general, you must ensure that the query reaches at least the range level, preferably ref.

掌握: system > const > eq_ref > ref > range > index >ALL

6.1 system

The table has only one row of records (equal to the system table), which is a special column of const type, which does not appear in usual times, and this can also be ignored

6.2 const

Indicates that it was found by indexing once, and const is used to compare the primary key or unique index . Because only one row of data is matched, soon if the primary key is placed in the where list, MySQL can convert the query to a constant.

Insert picture description here
6.3 eq_ref

Unique index scan. For each index key, only one record in the table matches it. Commonly used in primary key or unique index scans. It can be understood that a company has only one CEO and many developers.

Insert picture description here
6.4 ref

Non-unique index scan, returns all rows that match a single value . Essentially, it is an index access. It returns all rows that match a single value. However, it may find multiple rows that meet the criteria , so Should belong to a mixture of search and scan.

Before useless index:
Insert picture description here
After indexing:
Insert picture description here
Insert picture description here
6.5 range

Only retrieve rows in a given range, use an index to select rows. key column shows the use of which index is generally appear in your statement where between、<、>、inother queries that scan index scan range is better than a full table scan, because at some point, and Conclusion another point in the index only needs to start without scanning All indexes.
Insert picture description here

6.6 index

The occurrence of index is that sql uses the index but does not need to filter by the index. Generally, the overlay index is used or the index is used for sorting and grouping .

Insert picture description here
Full Index Scan, the difference between index and ALL is that the index type only traverses the index tree. This is usually faster than ALL, because the index file is usually smaller than the data file.
(In other words, although all and Index both read the full table, index is read from the index, and all is read from the hard disk)

Insert picture description here

6.7 all

Full Table Scan, will traverse the full table to find matching rows.

Insert picture description here

6.8 index_merge

In the query process, multiple index combinations are required, which usually appear in sql with the or keyword.
Insert picture description here

6.9 ref_or_null

For a field that requires both an association condition and a null value. The query optimizer will choose to use ref_or_null to join the query.
Insert picture description here

6.10 index_subquery

Use indexes to correlate subqueries, no longer full table scans.
Insert picture description here

6.11 unique_subquery

The connection type is similar to index_subquery. The unique index in the subquery.
Insert picture description here

Remarks: Generally speaking, it is necessary to ensure that the query reaches at least the range level, preferably ref.

7. possible_keys

Display one or more indexes that may be applied in this table. If there is an index on the field involved in the query, the index will be listed, but it may not be actually used by the query.

8. key

The actual index used. If NULL, no index is used.
If a covering index is used in the query, the index and the select field of the query overlap

9. key_len

Indicates the number of bytes used in the index. This column can be used to calculate the length of the index used in the query. The key_len field can help you check whether the index is fully utilized. The longer the ken_len, the more fully the index is used.
Insert picture description here
Insert picture description here
How to calculate:
①Look at the type and length of the field on the index such as int = 4; varchar (20) = 20; char (20) = 20
②If it is a string field such as varchar or char, it depends on the character set Value, such as utf-8 to multiply by 3, GBK to multiply by 2,
③ varchar such dynamic string to add 2 bytes
④ allow empty field to add 1 byte The
first group: byte length of key_len = age + name byte length = 4 + 1 + (20 * 3 + 2) = 5 + 62 = 67
second group: byte length of key_len = age = 4 + 1 = 5
Insert picture description here
Insert picture description here

10. ref

Shows which column of the index is used, if possible, a constant. Which columns or constants are used to find the value on the index column.
Insert picture description here
Insert picture description here

11. rows

The rows column shows the number of rows that MySQL thinks it must check when it executes the query. The less the better!
Insert picture description here
Insert picture description here

12. Extra

Contains additional information that is not suitable for display in other columns but is very important

12.1 Using filesort

Explain that mysql will use an external index to sort the data, instead of reading according to the index order in the table. The sorting operation in MySQL that cannot be completed with an index is called "file sorting".

The case of filesort appears:
Insert picture description here
after optimization, the case of filesort no longer appears: the
Insert picture description here
sorted fields in the query, if the sorted fields are accessed by index, the sorting speed will be greatly improved.
Insert picture description here

12.2 Using temporary

Using temporary tables to save intermediate results, MySQL uses temporary tables when sorting query results. Commonly used in order by and group by query group by.
Before optimization:
Insert picture description here
Insert picture description here

Insert picture description here

12.3 Using index

Using index indicates that the corresponding select operation uses a covering index (Covering Index) to avoid accessing the data rows of the table (to avoid back to the table) , the efficiency is good! If using where appears at the same time , it indicates that the index is used to perform index key value search; if there is no simultaneous use where, indicating that the index is only used to read data instead of using the index to perform a search. Use the index to sort or group.
Insert picture description here

12.4 Using where

Indicates that the where filter is used

12.5 Using join buffer

Connection cache is used.
Insert picture description here

12.6 impossible where

The value of the where clause is always false and cannot be used to get any tuples.
Insert picture description here

12.7 select tables optimized away

Without the GROUPBY clause, the MIN / MAX operation is optimized based on the index or the COUNT (*) operation is optimized for the MyISAM storage engine. It is not necessary to wait until the execution stage to calculate, and the optimization is completed at the stage of query execution plan generation.

In innodb:
Insert picture description here
In Myisam:
Insert picture description here

Warm up Case;
Insert picture description here

Index interview questions:
Insert picture description here
Insert picture description here
Analysis of the index used by the following sql:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
c3 is used for sorting and not looking for
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
group by. Basically, all sorts are required, and there will be temporary tables if they are inappropriate.
Insert picture description here
Insert picture description here

Published 138 original articles · Like 3 · Visitor 7244

Guess you like

Origin blog.csdn.net/weixin_43719015/article/details/104954010