MYSQL performance tuning (two) EXPLAIN/DESC

1. Grammar

EXPLAIN/DESC + [Query SQL];

 

2. Output summary

id: execution priority identification
select_type: query type of each select clause
table: output result set table
partitions: matching partition
type: table access type
possible_keys: possible index used in the query
key: actual index
key_len: The length of the index field
ref: the comparison between the column and the index
rows: the number of scanned rows (estimated number of rows)
filtered: the percentage of rows filtered by table conditions
Extra: description and explanation of the implementation

 

Three, detailed output

1、id

When the id is the same, the execution order is from top to bottom

When the id is different, the greater the id value, the higher the priority, and the earlier it will be executed

 

2、select_type

(1) SIMPLE (simple SELECT, without using UNION or subqueries, etc.)

(2) PRIMARY (the outermost query in the query)

(3) UNION (the second or subsequent SELECT statement in UNION)

(4) SUBQUERY (the first SELECT in the subquery, the result does not depend on the external query)

(5) UNION RESULT (Result of UNION)

(6) DEPENDENT UNION (the second or subsequent SELECT statement in UNION, depending on the query outside)

(7) DEPENDENT SUBQUERY (the first SELECT in the subquery, depends on the outside query)

(8) DERIVED (Subqueries of SELECT and FROM clauses of derived tables)

 

3、table

Display the name of the database table requested by the row, as shown in 2-(7)(8) above, may be the table name, may be abbreviation, may be the result of the first step

 

4、partitions

The partition where the table is located, a value of NULL means not partitioned

 

5、type

'Access type', namely linked list

Common access types (from low to high performance) are:  ALL, index, range, index_merge, ref_or_null,  ref, eq_ref, const, system, NULL

(1)ALL

As the name implies, mysql will perform a full table scan to find matching rows

(2)index

The same as all is to read the entire table. The advantage is to avoid sorting. The difference is that index only traverses the index tree, and the index file is usually smaller than the data file, so it is generally faster than all

(3)、range

Range scan, a restricted index scan. Use an index to retrieve rows in a given range, the key is the index used

Usually appear in the statement =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, IN and other operators

(4)、index_merge

ndex_merge indicates that the index merge optimization method is used. The key column contains a list of indexes used, and key_len contains the longest key element of the index used

(5)、ref_or_null

Similar to ref, but you can search for rows with NULL values

(6)、ref

A non-unique index scan that returns all rows that match a single value. The unique index is not applicable. Instead, the ordinary index or the partial prefix of the unique index is used. The index must be compared with a certain value, and multiple eligible rows may be found.

creator is an ordinary index (non-primary key index or unique index)

(7)、eg_ref

The unique index scan returns only one record that meets the conditions at most. Use unique index or primary key to find, it is the best type of join other than const, simple select query will not appear eg_ref

(8)、 const

const is used to compare all columns of primary key or unique key with constants. When it is determined that there will be at most one matching row, the MySQL optimizer will read it before the query and only read it once, so it is very fast.

If you put the primary key in the where condition, MySQL will convert the query into a constant.

 

(9)、system

system is a special case of const. When the table has only one row, the condition is met, and it does not usually appear

(10)、NULL

MySQL can decompose query statements in the optimization phase, without accessing tables or indexes during the execution phase. For example, to find the maximum or minimum value in the index column, you only need to find the index, and you do not need to access the table during execution

 

6、possible_keys、key、key_len

(1) possible_keys shows which indexes may be used in the query, but not necessarily. If it is NULL, there is no index available

(2) The key shows the index actually used by the query. If it is displayed as NULL, it means that the index is not used. If you want MySQL to force or ignore the index in the possible_keys column, use FORCE INDEX, USE INDEX, or IGNORE INDEX

(3) key_len shows the number of bytes used in the index. You can use this value to calculate which columns in the index are used

Note: Without loss of accuracy, the shorter the length, the better. The value displayed by key_len is the maximum possible length of the index field, not the actual length used, that is, key_len is calculated according to the table definition, not retrieved from the table

 

Calculation:

--Fixed-length data types: such as int, char, datetime, etc., need to have a mark of whether it is empty, this mark occupies one byte (for not null, this byte is not required)

  • tinyint: 1 byte
  • smallint: 2 bytes
  • int: 4 bytes
  • bigint: 8 bytes
  • date: 3 bytes
  • timestamp: 4 bytes
  • datetime: 8 bytes

--Variable length data type: such as varchar, in addition to whether it is empty or not, it also needs length information, which takes up two bytes

--For char, varchar, blob, text, etc., the length of key_len is also related to the character set, latin1 one character occupies one byte, gbk one character occupies two bytes, utf8 one character occupies three bytes, utf8mb4 one character Occupies 4 bytes

 

E.g:

Character set; utf8mb4

`id` int(11) NOT NULL AUTO_INCREMENT,

`dept_id` int(11) DEFAULT NULL ,

PRIMARY KEY (`id`),

UNIQUE KEY `id_index` (`id`,`dept_id`) USING BTREE

ken_len=4 (int 4 bytes, only the primary key index is used, and the unique index is not used)

 `org_id` int(11) DEFAULT NULL

ken_len=4+1 (int 4 bytes plus 1 byte DEFAULT NULL identification)

`creator` varchar(20) DEFAULT NULL

key_len=83=20*4+2+1 (The creator field is empty by default [DEFAULT NULL] mark plus 1 byte, variable length data type plus 2 bytes record length)

 

7、ref

ref indicates that when the query uses an index, the column to be queried is connected to the index. When it is a constant, ref is const

8、rows

Rows means MySQL estimates the number of rows that must be checked when executing a query

9、filtered

Filtered represents the percentage of the number of rows of the returned result to the number of rows that need to be read, the larger the value, the better

Filtered is only applicable when the type is ALL or index, because the record to be queried can be found by using the index, filtered=100

Note: the filtered value is inaccurate in many cases

10、Extra

(1)distinct

After MySQL finds the first matching row, it stops searching for more rows for the current row combination

(2)Using index

Only use the information in the index tree without further searching and reading the actual rows to retrieve the information in the table. That is, when all the requested columns of the table are part of the same index

(3)Using where

The mysql server will filter the rows after the storage engine retrieves them

(4)Using temporary

Indicates that MySQL needs to use a temporary table to store the result set (generally need to add index optimization)

(5)Using filesort

Sorting operations that cannot be done using indexes in MySQL are called "file sorting". MySQL will use an external index to sort the results, instead of reading rows from the table in index order

(6)Using join buffer

The index is not used when obtaining the connection conditions, and the connection buffer is needed to store intermediate results (may need to add index optimization)

(7)Impossible WHERE,Impossible HAVING

The where statement and having are always false, which will result in no eligible rows

(8)Select tables optimized away

Use certain aggregate functions (such as max, min) to access a field that has an index

(9)No tables used

The from dual virtual table is used in the query statement or does not contain any from clause

(10) no matching row in const table、 No matching min/max row

The table is empty or there is no matching row in the table based on the unique key query

(11)  Not exists

MySQL can perform LEFT JOIN optimization on the query, and after finding a row that matches the LEFT JOIN condition, it will not check more rows in this table in the previous row combination

E.g:

MySQL will scan a and use the value of a.id to find the row in s. If MySQL finds a matching row in s, it will know that s.id will never be NULL, and will not scan the remaining rows in t2 with the same id value

(12)  Using index condition

 The index will be conditionally filtered first, and after the index is filtered, all data rows that meet the index conditions are found, and then other conditions in the WHERE clause are used to filter these data rows

(13) Start temporary,End temporary

Represents a temporary table using the Duplicate Weedout strategy in the semi-join

(14)Using sort_union(...) , Using union(...) , Using intersect(...)

Indicates that index merge is used. Generally can be optimized by creating a composite index

1) Index intersect merge is the intersection operation of the results obtained by scanning multiple index conditions, that is, the AND operation. Index intersect merge will be used when the following two where conditions or combinations are met

      --The  condition uses all fields in the composite index or the left prefix field (also applicable to single-field indexes)

      -  Any range condition on the main key

2) Index uion merge is a scan of multiple index conditions, and the union operation is performed on the obtained results, that is, the OR operation. Index uion merge will be used when the following two where conditions or combinations are met

     --The condition uses all fields in the composite index or the left prefix field (also applicable to single-field indexes)

     - Any range condition on the main key

    -  any where conditions are in line with the index intersect merge

3) Sort_union multiple conditional scanning for OR operation, but does not conform to the index union merge algorithm, at this time the sort_union algorithm may be used (the difference between sort-union and union is whether to sort before returning the result)

Guess you like

Origin blog.csdn.net/kk_gods/article/details/109746646