The MySQL optimizer parameter derived_merge leads to the performance of multi-table association SQL and its low

    Recently, during MySQL maintenance, I encountered a problem. The execution speed of the SQL statement in the MySQL 5.6 test environment is less than 1 second, but the execution speed is less than 1 second in the MySQL 5.6 test environment.

It takes nearly 5 minutes to execute mysql 5.7 in a production environment. The same database in mysql 5.7 has the same amount of data, and it is executed after updating the statistics of the table.

The speed is still 2 minutes. The processing of this problem has nothing to do with the sql statement itself, only related to the optimizer parameters of the mysql database itself, the following is

During the problem analysis and troubleshooting process, the SQL statement in the problem analysis does not need to be displayed. It can be clear that the SQL is a multi-table join connection and the business is not allowed to be changed.

    1. First, check the execution plan of mysql's sql statement in the test environment and the production environment

--Test environment, the execution plan only needs to show the part that can explain the problem

--In the production environment, the execution plan only needs to show the part that can explain the problem

    ​2. From the comparison of SQL statements in the test and production environments, it is obvious that the execution plan of SQL is inconsistent, and it is found in the follow-up investigation

a. The statistical information of the tables and indexes involved in SQL in the test environment are all the latest that day, while the statistical information of the related tables and indexes in the production environment is relatively old

b. The major version of mysql in the test environment is 5.6, and the major version of mysql in the production environment is 5.7

    ​3, problem solving

a. Because the SQL execution plan is inconsistent, and the statistical information of the production environment is relatively old, re-collect the statistical information of the production environment table, the execution speed of SQL after collection is not

Improve, compared to the test is still very slow.

b. Pay attention to the test environment execution plan derived and <auto_key>, this feature is related to the mysql parameter derived_merge, and check the setting of this parameter

--test environment

MySQL [(none)]> show global variables like '%switch%';

+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Variable_name    | Value                                                                                                                                                                                                                                                                                                                                            |

+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| optimizer_switch | index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,....... |

+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

MySQL [(none)]> 

--In the production environment, in order to facilitate the explanation of the problem, the redundant parameter display is omitted

mysql> show  variables like '%switch%';

+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Variable_name    | Value                                                                                                                                                                                                                                                                                                                                                                                                             |

+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| optimizer_switch | ......,derived_merge=on|

+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

mysql>

c. According to the official instructions, the parameter derived_merge was introduced in the mysql5.7 version, and its function is to join (select) table joins and merges. This question SQL has

A large number of joins (selects) result in the merging of SQL execution results, and the SQL execution speed is extremely slow.

d. Temporarily cancel this parameter through the session level, observe the SQL execution plan, and find that the SQL execution plan is normal, and the SQL execution speed is less than 1 second similar to the test environment

--Cancel the optimizer derived_merge parameter

mysql> set optimizer_switch="derived_merge=off";

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id:    8980

Current database: mysql

Query OK, 0 rows affected (0.02 sec)

mysql> 

-Observe the problem SQL execution plan, at this time the production environment execution plan is the same as the test environment

--The problem sql execution speed has changed from nearly 5 minutes to about 1 second now

 

Guess you like

Origin blog.csdn.net/www_xue_xi/article/details/90710102