Performance differences before and after a user migrates a database
Problem
One user problem is that the data is migrated from ECS to RDS, and the query performance decreases by dozens of times with the same statements. In fact, the memory configuration of the RDS instance is equivalent to that of the original ECS instance.
This article briefly describes the causes and suggestions for this case. The statement for slow performance feedback is (modifying the real table and column name) select count (1) from HR hr join H h on h. hid = hr. hid join A e one. aid = h. eid join A t on t. aid = e. pid join A c on c. aid = t. pid join A p on p. aid = c. pidleft join U u on u. uid = hr. uId left join E emp on emp. eid = hr. oid where (hr. s in (1, 2, 3, 4) and hr.cn = 0 );
Background
Two major processes are involved in MySQL statement execution: Optimizer and executor. The most important task of the optimizer is to select the index and the connection sequence when connecting multiple tables. In this case, the choice of join sequence affects the execution performance. To determine the join execution sequence, you need to estimate the cost of all join operations. By default, MySQL estimates all possible combinations. MySQL Tips: MySQL limits the maximum number of join tables for a query to 61. for a join operation involving 61 tables, 61 is required theoretically! (Factorial) evaluation. Of course, this is the worst case. In fact, the subtraction algorithm will make this number look a little better, but it is still terrible. In the multi-table join scenario, MySQL provides the optimizer_search_depth parameter to control the recursive depth to prevent the optimizer_search_depth from occupying too much time. This parameter controls the algorithm and can be simply described as follows: for all the arrays, only the pre-optimizer_search_depth table of the current join order is used to estimate the cost. For example, for 20 tables, if optimizer_search_depth is 4, the number of evaluations is 20*19*18*17, although it is also very large (so we do not recommend the join of so many tables), than 20! Much better. So the selection of optimizer_search_depth becomes a problem.
MySQL Tips: In MySQL, the default value of optimizer_search_depth is 62. that is to say, the default value is full sorting.
In this way, the optimal execution plan can be obtained, but in some scenarios, the execution plan time is much larger than the execution time.
Quantitative analysis
On ECS, it is a MySQL maintained by the user. optimizer_search_depth is not set, so it is the default value of 62.
On RDS, our configuration is 4.
We can tell from the analysis that the reason is that 4 of RDS configuration does not obtain the optimal execution plan.
The explain result when optimizer_search_depth = 4 (the table name and field name related to the business are hidden) is the scenario where optimizer_search_depth = 62. of course, there are eight join tables in this case, therefore, 62 and 8 are equivalent here. As you can see, since optimizer_search_depth = 4, the optimizer thinks that it has chosen the optimal join order (22039*1*1*1), better than (41360*1*1*1 ), in fact, the latter is the global optimal. Interestingly, in this case, if you look at more than one layer, you can get the most solution, because the fifth table in the first join order evaluates rows as 82720. This means that in this case, setting 5 is the same as setting 62 to get the same execution plan. of course, setting 5 to the optimizer is less costly. This is actually the intention to provide optimizer_search_depth: reduce the optimizer execution time, and there is also a probability that local optimization is the global optimal solution.
Practices
While configurable parameters provide flexibility, a headache is also raised: it is appropriate to set the number of parameters.
In fact, when you execute a multi-table join operation, the overall RT expectation for this statement will not be high. Therefore, you can first define an expectation. for example, the time for the optimizer to determine the join sequence cannot exceed 500 ms.
The user type is related to the cpu, so this can only be a recommended value.
User practice
In fact, it is more important for users:
1) when the multi-table join execution results are significantly different After instance migration, you should consider adjusting this value. This parameter allows the thread to be set separately. Therefore, for the application layer, each connection should obtain a better value. 2) in turn, when the default optimizer_search_depth is set to 62, how do we evaluate whether our setting is too large? MySQL Tips: MySQL profiling can be used to view the time consumed by each execution step. The following is a query created by the author for a join query of 60 tables. you can use profiling to view the execution process consumed. Set profiling = 1; set optimizer_search_depth = 4; explain select ....... show profile for query 2; execute set optimizer_search_depth = 40; explain select ....... show profile for query 4; the red part in the figure shows the execution time difference between the two optimizers.
Summary
1) estimate an acceptable time based on the machine configuration, used by the optimizer to select the join sequence. 2) use profiling to determine whether a large optimizer_search_depth value is set. 3) optimize the business. try not to use multi-table join with more than 10 tables. 4) PS: Do not trust silver bullets. The MySQL document says that if it is set to 0, the system can automatically selectThe reasonable value of optimizer_search_depth. in actual code, the policy is: if the number of join tables is N <= 7, optimizer_search_depth = N + 1; otherwise, select N.