Comprehensive ==== View the execution plan through explain

View the specific explanation of explain and suggestions for creating indexes

Insert picture description here
id: the reading order of the table
select_type: indicates the type of query.
table: the table of the output result set
type: the connection type of the table
possible_keys: the index that may be used during the query
key: the index actually used
key_len: the length of the index field
ref: the comparison between the column and the index
rows: the scanned row Number (estimated number of rows)
Extra: Description and explanation of execution

CREATE TABLE testidx (
id bigint(20) NOT NULL AUTO_INCREMENT,
a bigint(11) DEFAULT NULL,
b varchar(100) NOT NULL,
c varchar(100) NOT NULL,
d varchar(100) NOT NULL,
PRIMARY KEY (id),
KEY idx_a (a),
KEY idx_b (b),
KEY idx_bc (b,c),
KEY idx_abc (a,b,c)
)

  1. Type field
    Solution

Read: The
access method to the table indicates the way MySQL finds the required row in the table, also known as the "access type".
Commonly used types are: ALL, index, range, ref, eq_ref, const, system, NULL (from left to right, performance from poor to good)
ALL: Full Table Scan, MySQL will traverse the entire table to find the matching row
index: Full Index Scan, the difference between index and ALL is that the index type only traverses the index tree
range: only retrieves rows in a given range, and uses an index to select rows
ref: indicates the connection matching condition of the above table, that is, which columns or constants are used for searching The value
eq_ref on the index column : Similar to ref, the difference is that the index used is a unique index. For each index key value, there is only one record in the table that matches. In simple terms, the primary key or unique key is used in the multi-table connection. Associated conditions
const, system: When MySQL optimizes a certain part of the query and converts it to a constant, these types of access are used. If the primary key is placed in the where list, MySQL can convert the query into a constant. System is a special case of the const type. When the query table has only one row, use system
NULL: MySQL decomposes the statement during the optimization process and executes it. Sometimes you don’t even need to access the table or index. For example, selecting the minimum value from an index column can be done through a separate index search.

ref
Insert picture description here
range
Insert picture description here

index
Insert picture description here
2. Interpretation of possible_keys and key fields:

possible_keys
indicates which indexes MySQL can use, but not necessarily be queried using (the query can use the index, if the index does not show any null)
Key
Key column displays the key (index) MySQL actually decided to use, must be included in possible_keys in
order to Force MySQL to use or ignore the index in the possible_keys column and use FORCE INDEX, USE INDEX, or IGNORE INDEX in queries.

Insert picture description here
3. Interpretation of the key_len field:
indicates the number of bytes used in the index, and the index length used in the query can be calculated through this column. For tables stored in the utf8 character set, each character occupies 3 bytes,

Through key_len, you can know which fields are used in the composite index. The calculation of key_len:

When the field definition can be empty, an extra 1 byte is needed to record whether it is empty, and when the field definition is not null, this extra 1 byte is not needed.

When the field is defined as a variable-length data type (such as varchar), an extra 2 bytes are required to record its length; when the field is defined as a fixed-length data type (such as int, char, datetime, etc.), this extra 2 bytes are not needed.

Insert picture description here
Remarks: varchar(100) data occupies 300 bytes, plus 2 bytes to indicate the field length

  1. Extra field interpretation:
    Using where: In the case of searching and using an index, you need to go back to the table to query the required data.
    Using index & using where: Search using an index, but the required data can be found in the index column, so there is no need Back to the table query data
    using index condition: search uses the index, need to return to the table query data. Conditional push is a feature that appeared after mysqlV5.6 version, direct filtering of rows in the secondary index can reduce the engine layer and sever layer Interaction.

Insert picture description here

Insert picture description here

  1. Extra field interpretation:
    Using temporary: indicates that MySQL needs to use a temporary table to store the result set, common in sorting and grouping queries, common group by; order by
    Using filesort: when the query contains order by, grpup by and other sorting operations, and cannot be used The sorting operation performed by the index is called "file sorting".
    "The truth is, filesort is badly named. Anytime a sort can't be performed from an index, it's a filesort. It has nothing to do with files. Filesort should be called “sort.” It is quicksort at heart."

Insert picture description here

Insert picture description here

Principles of index creation

1. Create indexes on those columns for large tables
, fields that appear in the Where clause, fields
that are connected to other tables, and fields
that need to be sorted (order by or group by)

2.Bad case:
create an index on an unneeded field, create an index
repeatedly, create an index
on a column with low selectivity, and place it at the top of the index.
Too many indexes (for example, more than 7)

Guess you like

Origin blog.csdn.net/shunnianlv/article/details/109094758