Bug #54225 | Optimizer prefers table scan and filesort instead of index scan | ||
---|---|---|---|
Submitted: | 4 Jun 2010 8:12 | Modified: | 11 Apr 2020 2:09 |
Reporter: | Andrew A | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S5 (Performance) |
Version: | 5.1.37, 5.1.49, 5.5.5-m3 | OS: | Linux (5.1.37-1ubuntu5.1) |
Assigned to: | CPU Architecture: | Any | |
Tags: | optimizer index filesort |
[4 Jun 2010 8:12]
Andrew A
[6 Jun 2010 18:42]
Valeriy Kravchuk
It can be related to bug #38397, bug #46011 or bug #42094.
[10 Jun 2010 6:53]
Valeriy Kravchuk
Verified just as described. Even simpler query does NOT use index for ORDER BY: mysql> explain SELECT t.topic_last_post_time FROM phpbb_posts p, phpbb_topics t WHERE t.topic_replies =0 AND t.topic_moved_id =0 AND p.topic_id = t.topic_id AND p.forum_id NOT IN ( 72, 73 ) ORDER BY t.topic_last_post_time DESC LIMIT 1001; +----+-------------+-------+------+---------------------------------+----------+---------+-----------------+------+-----------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------------------------+----------+---------+-----------------+------+-----------------------------+ | 1 | SIMPLE | t | ALL | PRIMARY | NULL | NULL | NULL | 2 | Using where; Using filesort | | 1 | SIMPLE | p | ref | forum_id,topic_id,tid_post_time | topic_id | 3 | test.t.topic_id | 6144 | Using where | +----+-------------+-------+------+---------------------------------+----------+---------+-----------------+------+-----------------------------+ 2 rows in set (0.00 sec) neither with InnoDB nor with MyISAM storage engine. Index is used when forced though: mysql> explain SELECT t.topic_last_post_time FROM phpbb_posts p, phpbb_topics t force index(`last_post_time`) WHERE t.topic_replies =0 AND t.topic_moved_id =0 AND p.topic_id = t.topic_id AND p.forum_id NOT IN ( 72, 73 ) ORDER BY t.topic_last_post_time DESC LIMIT 1001; +----+-------------+-------+-------+---------------------------------+----------------+---------+-----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------------------------+----------------+---------+-----------------+------+-------------+ | 1 | SIMPLE | t | index | NULL | last_post_time | 4 | NULL | 1 | Using where | | 1 | SIMPLE | p | ref | forum_id,topic_id,tid_post_time | topic_id | 3 | test.t.topic_id | 6144 | Using where | +----+-------------+-------+-------+---------------------------------+----------------+---------+-----------------+------+-------------+ 2 rows in set (0.00 sec)
[13 Jun 2010 12:31]
Andrew A
Hi, Thank's for taking the time to check this out. RE: 'Index is used when forced though' I noticed that USE INDEX and FORCE INDEX in my original query does NOT take effect (another possible bug?). Thanks
[25 Mar 2011 20:02]
Rick James
"rows" in the first EXPLAIN is 2, which severely disagrees with "topics table at ~150,000" I suggest that if "2" is correct, the optimizer did the right thing by simply doing a table scan. So, is this an ANALYZE problem? Or is the table really smaller than stated?
[11 Apr 2020 2:09]
Jon Stephens
Should be fixed in MySQL 8.0.21 by WL#13929/BUG#97001. See same for docs info. Closed.