[MDEV-20740] Odd computations in calculate_cond_selectivity_for_table Created: 2019-10-03 Updated: 2023-04-27 |
|
| Status: | Open |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Affects Version/s: | 10.2, 10.3, 10.4 |
| Fix Version/s: | 10.4 |
| Type: | Bug | Priority: | Major |
| Reporter: | Sergei Petrunia | Assignee: | Igor Babaev |
| Resolution: | Unresolved | Votes: | 1 |
| Labels: | eits | ||
| Issue Links: |
|
||||||||||||||||
| Description |
|
calculate_cond_selectivity_for_table has the code to compute table->cond_selectivity from selectivities of potential range accesses. Selectivities of range accesses are multiplied, but there is code to handle the situation where range accesses use restrictions on the same column. In that case, the code has additional multiplier to account for this fact (and avoid counting the same selectivity twice). This bug is about that code not working for a fairly basic example where I think it should work. The testcase (not necessarily minimal):
The query:
Checking the optimizer trace, I can see that this will create potential range accesses:
Both range accesses use column col2, both have selectivity around 0.5 But when I step through calculate_cond_selectivity_for_table() function, I can see that
I don't see any adjustments to the selectivity that are due to the fact that both potential range acccesses use the same column. I think this is not what was intended. |
| Comments |
| Comment by Sergei Petrunia [ 2019-10-03 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
EXPLAIN output, just in case. filtered shows the product of quick selects' selectivities.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2019-10-03 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
and when we reach this:
we have used_key_parts=1, i=1 I think the issue is somewhere around here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2019-10-03 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Wait, why is this:
The trace excerpt shows that i0 uses 3 key parts (matches gdb output), while i1 uses 2 key parts (doesn't match it). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2019-10-04 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
opt_range code doesn't max_key_part correctly for certain kinds of range scans. Setting the value to be correct causes this change:
Debugging the range vs index merge difference (3rd). Relevant part of the trace:
filtered_records is set like so:
The computation in records_in_index_intersect_extension depends on how many key parts merged key scans use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2019-10-04 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Patch for the max_key_parts issue: http://lists.askmonty.org/pipermail/commits/2019-October/014019.html . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2019-10-04 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Ok, trying the example again after the patch for max_key_part. Execution reaches this point twice:
both times the quick select selectivity is around 0.5, so table->cond_selectivity becomes around 0.25 Then we reach this code with i=1
the stats are realistic:
Then we execute this line
and get (gdb) p table->cond_selectivity |