Detailed explain&Profiling of mysql_query optimization

1、explain

To have a better understanding of the execution plan, you need to have a simple understanding of the basic structure of MySQL and the basic principles of query. The functional architecture of MySQL itself is divided into three parts, namely the application layer, logical layer, and physical layer. Not only MySQL, most other database products are divided according to this architecture.

The application layer is mainly responsible for interacting with the client, establishing a link, remembering the link status, returning data, and responding to requests. This layer deals with the client.
The logic layer is mainly responsible for query processing, transaction management and other database function processing

Take query as an example.
First, after receiving the query SQL, the database will immediately allocate a thread to process it. The first step is to optimize the SQL query by the query processor. After optimization, an execution plan will be generated and then handed over to the plan executor for execution.
The plan executor needs to access the lower-level transaction manager and storage manager to operate data. Their respective division of labor is different. Finally, the query structure information is obtained by calling the file of the physical layer, and the final result is responded to the application layer.

The physical layer, the files stored on the actual physical disk, mainly includes sub-text data files and log files.

Through the above description, generating an execution plan is an essential step to execute a SQL. The performance of a SQL can be seen intuitively by viewing the execution plan. The execution plan provides various query types and levels, which is convenient for us to perform View and serve as the basis for performance analysis.

insert image description here

1.1, identity column

id indicates the serial number of the query statement, which is automatically allocated and incremented in order. The larger the value, the higher the execution priority.
insert image description here
When the IDs are the same, the priority is from top to bottom.
insert image description here

1.2, select_type column

select_type indicates the query type, common ones include SIMPLE simple query, PRIMARY main query, SUBQUERY subquery, UNION joint query, UNION
RESULT joint temporary table results, etc.
insert image description here

1.3, table columns

table indicates the table name, table alias, and temporary table name queried by the SQL statement.
Please add a picture description

1.4, partitions column

partitions indicates the partitions matched by the SQL query, and NULL is displayed if there is no partition.
insert image description here

1.5, type column

type indicates the table connection type or data access type, that is, how to establish connection between tables, or how to access data. Specifically, there are the following values, and the order of performance from good to bad is: system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL system When there is only one row of records in the table, that is, the system table,
it
is A special column of const type.
insert image description here
const
indicates that the primary key or unique index is used for equivalent query, and at most one record is returned. Good performance, recommended to use.
insert image description here
eq_ref
indicates that the table connection uses the primary key or unique index, and the following SQL uses the primary key id of the user table.
insert image description here
ref
means to use non-unique index for equivalent query.
insert image description here
ref_or_null
indicates that a non-unique index is used for equivalent query and contains rows with null values.
insert image description here
index_merge
indicates the optimization logic used for index merging, that is, multiple indexes used.
insert image description here
range
indicates that the index range query is used.
insert image description here
index
means to use the index for full table scan.
insert image description here
ALL
means a full table scan, with the worst performance.
insert image description here

1.6, possible_keys column

Indicates the index columns that may be used, but may not be used in actual queries.
insert image description here

1.7, key column

Indicates that the actual query uses the index column.
insert image description here

1.8、key_len列

Indicates the number of bytes occupied by the index.
insert image description here
The number of bytes occupied by each type is as follows:
insert image description here

1.9, ref column

Indicates the parameters compared with the index in the where statement or table connection. The common ones are const (constant), func (function), and field name. Displays NULL if no index is used.
insert image description here
insert image description here
insert image description here

1.10. rows

Indicates the number of rows scanned by executing the SQL statement.
insert image description here

1.11. filtered columns

Indicates the percentage of table rows filtered by the condition.
insert image description here
Used to estimate the number of rows scanned when connecting with other tables, row x filtered = 252004 x 10% = 250,000 rows

1.12, Extra column

Indicates some additional extended information, which is not suitable for display in other columns, but is very important.
Using where
indicates that the where conditional search is used, but the index is not used.
insert image description here
Using index
means that the covering index is used, that is, the required data can be found on the index, and there is no need to query the table twice, and the performance is better.
insert image description here
Using filesort
indicates that external sorting is used, that is, the sorting field does not use an index.
insert image description here
Using temporary
means that a temporary table is used. In the following example, a temporary table is used to store query results.
insert image description here
Using join buffer
indicates that when performing table association, no index is used, and the connection buffer is used to store temporary results.
In the following example, user_id is not indexed in both tables.
insert image description here
Using index condition
indicates that the optimization feature of index pushdown is used.
insert image description here

2. Use Profiling for query optimization

MySQL's Query Profiler is a very convenient Query diagnostic analysis tool. Through this tool, you can obtain the consumption of various resources during the entire execution process of a Query, such as CPU, IO, IPC, SWAP, etc., as well as the PAGE FAULTS that occurred. CONTEXT SWITCHE, etc., and at the same time, the position in the source file of each function called by MySQL during the execution of the Query can also be obtained.
Let's take a look at the specific usage of Query Profiler.
insert image description here
insert image description here
insert image description here
Other techniques for query optimization
Index design relies on predicate expressions (conditional expressions or truth expressions----the statement after where)
to avoid
appropriate redundancy of Join data

Guess you like

Origin blog.csdn.net/chuige2013/article/details/128791706