Analysis of performance differences before and after a user migrates a database: A User's ticket: data is migrated from ECS to RDS. with the same statements, the query performance is reduced by dozens of times. 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 (the real table name and column name are modified)
Select count (1) from HR hr join H h on h. hid = hr. hid
Join A e on e. 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. pid
Left join U u on u. uid = hr. uId
Left join E emp on emp. eid = hr. oid
Where (hr. s in (1, 2, 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 one query to 61.
In theory, 61 join operations involving 61 tables are required! (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.
Is the explain result when optimizer_search_depth = 4 (the table name and field name related to the business are hidden)
It is a scenario where optimizer_search_depth = 62. of course, there are eight join tables in this case, so 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.
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;
Result
Continue execution
Set optimizer_search_depth = 40;
Explain select .......
Show profile for query 4;
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. If the value of MySQL is set to 0, the optimizer_search_depth value can be automatically selected. in actual code, the policy is: if the number of join tables is N <= 7, optimizer_search_depth = N + 1, otherwise, select N.