[MDEV-27262] Unexpected index intersection with full index scan for an index Created: 2021-12-15 Updated: 2021-12-28 Resolved: 2021-12-25 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Fix Version/s: | 10.2.42, 10.3.33, 10.4.23, 10.5.14, 10.6.6, 10.7.2 |
| Type: | Task | Priority: | Major |
| Reporter: | Igor Babaev | Assignee: | Igor Babaev |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Issue Links: |
|
||||||||
| Description |
|
For the table
populated with the statements:
the following execution plan is chosen for the query
if the optimizer switch index_merge_sort_intersection is set to 'on'
As the range condition for the index 'er' (er!=4 OR er=4) is always true this index is of no use for index intersection. |
| Comments |
| Comment by Igor Babaev [ 2021-12-15 ] | ||||||||||||||||||||||||||||||||
|
This bug manifests itself more clearly in 10.4 where the optimization employing range rowid filters has been introduced. | ||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2021-12-16 ] | ||||||||||||||||||||||||||||||||
|
The problem appears for queries whose WHERE conditions contain OR formulas where the Range Optimizer builds OR ranges fully covering an index. Such ranges should be ignored, but the function key_or() from the Range Optimizer code not always does it.
For this query the Range Optimizer builds the following plan
It does not make sense to use here an index merge scan as the range for the index i3 is (-inf,+inf). It should be noted that the index intersect plan is chosen for the reported query only due to a defect in the InnoDB code the makes the returns wrong estimates of the cardinality of the big ranges. The engine always thinks that the number of records in a range cannot exceed 50% of the number of records in the whole index.
| ||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2021-12-20 ] | ||||||||||||||||||||||||||||||||
|
The following change should be applied when merging the patch into 10.4:
| ||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-12-20 ] | ||||||||||||||||||||||||||||||||
|
A question: http://lists.askmonty.org/pipermail/commits/2021-December/014823.html | ||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2021-12-21 ] | ||||||||||||||||||||||||||||||||
|
Here's another query where the overlapped ranges that are ORed and cover the whole index er:
When the optimizer switch 'index_merge_sort_intersection' is set to 'on' we see the same problem
However this query does not cause any problem in 10.4 with default settings for optimizer switches 'rowid_filter' and 'index_merge_sort_intersection' ('on' and 'off' correspondingly. This is because for this query when only the index er is enabled key_or() returns the range (-inf,+inf) for the index er rather then just NULL (see comments in | ||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2021-12-21 ] | ||||||||||||||||||||||||||||||||
|
A similar problem can be observed for overlapping ranges that are Ored and cover the whole index when the optimizer decides to use a table retrieval with index_merge_sort_union scan:
| ||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-12-23 ] | ||||||||||||||||||||||||||||||||
|
The second patch is ok to push. | ||||||||||||||||||||||||||||||||
| Comment by Igor Babaev [ 2021-12-25 ] | ||||||||||||||||||||||||||||||||
|
Attention: See my comment how to merge into 10.4. |