[MDEV-25830] optimizer_use_condition_selectivity=4 sometimes produces worse plan than optimizer_use_condition_selectivity=1 Created: 2021-05-31 Updated: 2023-05-01 Resolved: 2022-01-19 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Affects Version/s: | 10.4.18, 10.5.9 |
| Fix Version/s: | 10.4.23 |
| Type: | Bug | Priority: | Critical |
| Reporter: | Edward Stoever | Assignee: | Sergei Petrunia |
| Resolution: | Fixed | Votes: | 3 |
| Labels: | None | ||
| Issue Links: |
|
||||||||||||||||||||
| Description |
|
When using set optimizer_use_condition_selectivity=1 we get a better query plan than for set optimizer_use_condition_selectivity=[2|3|4|5]
A workaround has been discovered:
extended_keys=off produces a much better query plan, returning results in 0.001 seconds even when set optimizer_use_condition_selectivity=4. |
| Comments |
| Comment by Thejaka Kanewala [ 2021-06-10 ] | |||||||||||||||||||||||||||||||||||||
|
Hello, As an attempt to fix this issue a PR is created – https://github.com/MariaDB/server/pull/1856. Thank you | |||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-07-12 ] | |||||||||||||||||||||||||||||||||||||
|
(To improve understanding of the patch, I'll study the testcase) The query:
query plan before the patch:
query plan after the patch:
| |||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-07-12 ] | |||||||||||||||||||||||||||||||||||||
|
Optimizer trace differences, comparing the code before the patch with the code after the patch:
...
...
| |||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-07-12 ] | |||||||||||||||||||||||||||||||||||||
|
.. that is, before this patch, the optimizer was using selectivity from sys_class_name2:
and ignoring selectivity from the PRIMARY key, because it has assumed (incorrectly! ) that quick select on sys_class_name_2 has "taken into account" the selectivity on column sys_id. (which is not the case. quick on sys_class_name_2 has rows=49, while the condition "sys_id=8e7..." has rows=1) | |||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-07-12 ] | |||||||||||||||||||||||||||||||||||||
|
The worse part starts happening during the join optimizer.
Note the "cost": 2e308 part. | |||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-07-12 ] | |||||||||||||||||||||||||||||||||||||
|
An alternative patch. I think it's more "conservative":
| |||||||||||||||||||||||||||||||||||||
| Comment by Thejaka Kanewala [ 2021-07-12 ] | |||||||||||||||||||||||||||||||||||||
|
Hello Sergei, Thanks a lot for all the comments and review feedback. Thanks | |||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-07-12 ] | |||||||||||||||||||||||||||||||||||||
|
Hi thejaka, Yes, I would appreciate the input. I'm also interested if the patch posted two comments above solve your issues as well (I mean, it does solve the reduced testcase from the pull request, but does it solve the full case or any other cases you're hitting ?) | |||||||||||||||||||||||||||||||||||||
| Comment by Thejaka Kanewala [ 2021-07-14 ] | |||||||||||||||||||||||||||||||||||||
|
Hello Sergei, The alternate patch attached works for the specific query above and few other queries I selected. To see whether it improves overall performance of all the degrading queries under selectivity=4, I need to run some further tests. However, I am not sure whether the proposed alternate patch addresses the root cause of the issue reported in this ticket. When a query has range conditions on the primary key and other keys, the proposed alternate patch will still not consider condition selectivity from primary key range condition. The proposed alternate patch put an upper bound on the selectivity (trivial selectivity). However, there is still a possibility that the optimizer choosing a less efficient plan, because the table condition selectivity is excluding the primary key range condition selectivity. However, I am yet to find a counter example to demonstrate above. What is your opinion about the above comment? Also, are you planning to merge proposed alternate patch as a fix to this issue ? Thank you | |||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-09-17 ] | |||||||||||||||||||||||||||||||||||||
|
... the "alternative patch" posted above | |||||||||||||||||||||||||||||||||||||
| Comment by Larry Adams [ 2021-10-05 ] | |||||||||||||||||||||||||||||||||||||
|
Sergei, This looks like it's got legs for the other bug I found. Know you are busy. Larry | |||||||||||||||||||||||||||||||||||||
| Comment by Larry Adams [ 2021-10-07 ] | |||||||||||||||||||||||||||||||||||||
|
Just to note guys, I enabled the setting: set optimizer_switch="extended_keys=off"; And that has helped, but I periodically still get queries that hang, that should run pretty quickly. Not sure I can do any testing though, and not sure this comment helps. With the extended_keys off, I do get correct query plans though, at least the times that I have checked them. Larry | |||||||||||||||||||||||||||||||||||||
| Comment by Larry Adams [ 2021-10-07 ] | |||||||||||||||||||||||||||||||||||||
|
One of the queries hung, and then an optimize came right behind it and created a meta-data lock, but since the query before the optimize did not finish on it's own, the whole server started to melt down as transactional queries stacked up behind the meta-data/optimize lock. Today I implemented a long running query control mechanism to prevent blocking of the optimize queries, but it would be good to figure out why these queries are blocking randomly. It's very frustrating. Unfortunately, in my 20+ years experience with MySQL, I've never had to dig into the InnoDB engine status. So, it's still Greek to me. So, I likely won't be of much help in the near term. I'll log a separate ticket once I come up for air. | |||||||||||||||||||||||||||||||||||||
| Comment by Roel Van de Paar [ 2021-11-04 ] | |||||||||||||||||||||||||||||||||||||
|
When fixing this bug please also keep the findings in | |||||||||||||||||||||||||||||||||||||
| Comment by Michael Widenius [ 2021-12-15 ] | |||||||||||||||||||||||||||||||||||||
|
There is several known issues with optimizer_use_condition_selectivity=4. See My hope is that the code will be in 10.9. Until then, don't use optimizer_use_condition_selectivity=4. | |||||||||||||||||||||||||||||||||||||
| Comment by Michael Widenius [ 2021-12-20 ] | |||||||||||||||||||||||||||||||||||||
|
I have checked the test case from the pull request with my new optimizer code and I get the same result file. The part of the code that solves this particular issue is probably that I sort the ranges to first try the range with the least rows. I will talk with Petrunia if we can take that particular sort code and add that to 10.4.8 and see if helps a bit, until I am ready with Sorry, I missed that optimizer_use_condition_selectivity=4 is now default. I agree that we should try to fix the biggest issues in selectivity in 10.4, but the real fixes has to wait for 10.9 (as there are a LOT of them and they need proper testing before we can even consider back porting any of them) | |||||||||||||||||||||||||||||||||||||
| Comment by Michael Widenius [ 2022-01-19 ] | |||||||||||||||||||||||||||||||||||||
|
Patch given on slack | |||||||||||||||||||||||||||||||||||||
| Comment by Michael Widenius [ 2022-01-19 ] | |||||||||||||||||||||||||||||||||||||
|
The issue was that calc_cond_selectivity_for_table preferred ranges with This ensures that selectivity from ranges with few rows will be preferred |