Bug #74030 | Filesort chosen where index should've been, using LIMIT | ||
---|---|---|---|
Submitted: | 23 Sep 2014 7:54 | Modified: | 11 Apr 2020 2:15 |
Reporter: | Edgars Irmejs (OCA) | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S5 (Performance) |
Version: | 8.0dev, 5.7.16, 5.6.20, 5.5.39, | OS: | Any |
Assigned to: | Assigned Account | CPU Architecture: | Any |
Tags: | Contribution |
[23 Sep 2014 7:54]
Edgars Irmejs
[23 Sep 2014 7:55]
Edgars Irmejs
InnoDB case where the problem occurs in production use
Attachment: trace_bug_74030.log (application/octet-stream, text), 16.96 KiB.
[26 Sep 2014 11:32]
MySQL Verification Team
Hello Edgars, Thank you for reporting the issue, and supplying a patch along with it. Please note that in order to submit contributions you must first sign the Oracle Contribution Agreement (OCA). For additional information please check http://www.oracle.com/technetwork/community/oca-486395.html. If you have any questions, please contact the MySQL community team - http://www.mysql.com/about/contact/?topic=community. Thanks, Umesh
[26 Sep 2014 11:33]
MySQL Verification Team
// With 5.6.22 [root@cluster-repo mysql-advanced-5.6.22]# ./test.sh Loading the table with data ...................................................................... For this query plan, the index idx_ac should been chosen! Table Op Msg_type Msg_text test.t analyze status Table is already up to date -------------- explain select c from test.t where a = 2 order by c desc limit 1 -------------- +----+-------------+-------+------+----------------+---------+---------+-------+------+------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+----------------+---------+---------+-------+------+------------------------------------------+ | 1 | SIMPLE | t | ref | idx_ac,idx_abc | idx_abc | 5 | const | 5 | Using where; Using index; Using filesort | +----+-------------+-------+------+----------------+---------+---------+-------+------+------------------------------------------+ 1 row in set (0.00 sec) Bye [root@cluster-repo mysql-advanced-5.6.22]# ./test2.sh Loading the table with data .......... For this query plan, all keypars of idx_abc should be used in a 'range' access. See the 'FORCE INDEX' plan for the correct choice. alter table test.t engine=innodb Table Op Msg_type Msg_text test.t analyze status OK -------------- explain select c from test.t where a = 1 and b > 500 order by b limit 1 -------------- +----+-------------+-------+------+---------------+--------+---------+-------+------+------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+--------+---------+-------+------+------------------------------------+ | 1 | SIMPLE | t | ref | idx_ab | idx_ab | 5 | const | 500 | Using index condition; Using where | +----+-------------+-------+------+---------------+--------+---------+-------+------+------------------------------------+ 1 row in set (0.00 sec) Bye -------------- explain select c from test.t force index (idx_ab) where a = 1 and b > 500 order by b limit 1 -------------- +----+-------------+-------+-------+---------------+--------+---------+------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+--------+---------+------+------+-----------------------+ | 1 | SIMPLE | t | range | idx_ab | idx_ab | 10 | NULL | 500 | Using index condition | +----+-------------+-------+-------+---------------+--------+---------+------+------+-----------------------+ 1 row in set (0.00 sec)
[4 Oct 2014 10:51]
Edgars Irmejs
OCA approved. I assume you tested the non-patched version, as the wrong index is chosen in test.sh. No need to run test2.sh - it was only made to show that the MySQL Optimizer Cost Model Project is very necessary, and I put high hopes on it. The bug shown by test2.sh is not being fixed by the patch. If anything else is needed from me, let me know.
[9 Dec 2016 15:10]
Jeremie Rioux
I'm affected. Any update? Doesn't seem to be fixed even in latest 5.7 version.
[12 Dec 2016 18:11]
Edgars Irmejs
They actually did the Optimizer Cost Model project in 5.7.5 but the implementation did not fix this issue. It's unresolved in 8.0 branch too. I see the commit "WL#7870: Create JOIN object after query preparation" has dealt with the test2.sh problem but the main one, that of test1.sh, still remains. Thanks for raising awareness that this issue matters!
[26 Jun 2017 11:57]
Manyi Lu
Hi, thanks for you contribution. We are currently looking into improving cost model for index vs filesort decision. As part of this work, we will process your contribution. We will certainly make sure your test cases are covered. We welcome more contributions!