[MDEV-25858] Query results are incorrect when indexes are added. Created: 2021-06-04 Updated: 2022-01-26 Resolved: 2021-07-15 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Data Manipulation - Subquery |
| Affects Version/s: | 10.5.10, 10.2, 10.3, 10.4, 10.5 |
| Fix Version/s: | 10.2.40, 10.3.31, 10.4.21, 10.5.12, 10.6.4 |
| Type: | Bug | Priority: | Major |
| Reporter: | Wataru Maeda | Assignee: | Sergei Petrunia |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Environment: |
CentOS 7.9, Docker |
||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
I am using MariaDB for several products. When I inserted data in a table with indexes pre-added, I sometimes did not get the expected search results. I tried this with Mariadb 10.5.10 on Docker container. First, start the MariaDB container on Linux (CentOS 7.9).
Next, I ran the following on my MacBook.
This is not the result I was expecting. Next, I removed the index created at the end of `setup.sql` and ran `select.sql` again, and got the expected results.
After that, I re-added the same index, and the correct search results remained. I also tried with MariaDB 10.2.38 and the results were the same. I think It might be related to the fact that I'm trying to reference the index in the correlation subquery. I tried the same with MySQL 5.7.34 and got the correct results without reindexing.
thanx. |
| Comments |
| Comment by Alice Sherepa [ 2021-06-04 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks for the report!
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-06-09 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The problem is here in the subquery's plan:
How does one ensure that order by dd.d1 desc, dd.d2 desc, dd.id desc limit 1 is enforced? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-06-09 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Yes, when debugging, I see this to happen: The subquery is executed,
instead of
and the wrong query output agrees with this. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-06-10 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Indexes in table t2:
Subquery optimization goes like this: Range optimizer produces:
Based on the above, make_join_select() sets tab->use_quick=2 (do "Range checked for each record") Then, test_if_skip_sort_order()
Then, query execution starts
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-06-10 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
If I make test_if_skip_sort_order() to change tab->use_quick from 2 to 1 for this query, it doesn't help. This is because make_join_readinfo() has already set tab->read_first_record to be join_init_quick_read_record. Changing tab->use_quick is too late here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-06-10 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
... what about already existing lines in test_if_skip_sort_order() that assign tab->use_quick=1 ? Do we have tests where they would actually change tab->use_quick from 2 to 1? The answer is no: I've added printout and ran the testsuite. Nothing was printed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-06-10 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Note that test_if_skip_sort_order also does this:
Relevant changesets: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-06-11 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
bb-10.2-mdev25858 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-06-11 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
http://lists.askmonty.org/pipermail/commits/2021-June/014645.html | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2021-06-29 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Sanja, please review. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Oleksandr Byelkin [ 2021-07-14 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
OK to push |