Bug #45373 | INDEX FOR GROUP BY is not used if there is a LIMIT clause in the query | ||
---|---|---|---|
Submitted: | 8 Jun 2009 13:14 | Modified: | 19 Jun 2009 4:19 |
Reporter: | Alex Bolenok | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S3 (Non-critical) |
Version: | 5.1.35-community, 5.1.36-bzr | OS: | Any |
Assigned to: | Assigned Account | CPU Architecture: | Any |
Tags: | cost model, GROUP BY, INDEX, limit |
[8 Jun 2009 13:14]
Alex Bolenok
[8 Jun 2009 17:37]
Valeriy Kravchuk
Thank you for the problem report. Verified just as described with recent 5.1.36 from bzr. Index is not used even with LIMIT 1, and this leads to slower execution. Your workaround (where index is used for GROUP BY) allows to get results faster.
[8 Jun 2009 20:03]
Alex Bolenok
On my installation of '6.0.11-alpha-community' the queries behave just the same.
[9 Jun 2009 17:35]
MySQL Verification Team
Test case has to be changed thoroughly. * There must not be any FORCE INDEX used * There must not be any nested or derived query * There must be ORDER BY NULL, so that index for sorting is not chosen Hence, there must be a pure case. One in which index is used for GROUP BY, without limit, and the other when it is NOT used for GROUP BY, when LIMIT is present. I hope that this is clear enough ......
[9 Jun 2009 18:03]
Valeriy Kravchuk
The same results with ORDER BY NULL and no FORCE INDEX: mysql> EXPLAIN SELECT INET_NTOA(ip) AS textip, MAX(ts) AS maxts FROM t_latest GROUP BY ip ORDER BY NULL; +----+-------------+----------+-------+---------------+-----------------+---------+------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+-------+---------------+-----------------+---------+------+------+--------------------------+ | 1 | SIMPLE | t_latest | range | NULL | ix_latest_ip_ts | 4 | NULL | 23 | Using index for group-by | +----+-------------+----------+-------+---------------+-----------------+---------+------+------+--------------------------+ 1 row in set (0.00 sec) mysql> EXPLAIN SELECT INET_NTOA(ip) AS textip, MAX(ts) AS maxts FROM t_latest GROUP BY ip ORDER BY NULL LIMIT 1; +----+-------------+----------+-------+---------------+-----------------+---------+------+-------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+-------+---------------+-----------------+---------+------+-------+-------------+ | 1 | SIMPLE | t_latest | index | NULL | ix_latest_ip_ts | 12 | NULL | 10465 | Using index | +----+-------------+----------+-------+---------------+-----------------+---------+------+-------+-------------+ 1 row in set (0.00 sec)
[4 Mar 2010 9:37]
Manyi Lu
See also BUG#49111.
[16 Mar 2010 9:42]
Sveta Smirnova
Bug #52081 was marked as duplicate of this one.