Bug #117408 Tight index scan not used when group by does not begin with first part of key, but where condition is list of constants
Submitted: 7 Feb 4:26 Modified: 7 Feb 8:36
Reporter: Jacob Brown Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S5 (Performance)
Version:8.0.41 OS:Any
Assigned to: CPU Architecture:Any

[7 Feb 4:26] Jacob Brown
Description:
In https://dev.mysql.com/doc/refman/8.4/en/group-by-optimization.html, it says that a tight index scan can be used when "The GROUP BY does not begin with the first part of the key, but there is a condition that provides a constant for that part".  But when a list of constants satisfies this condition, then a temporary table is used, which is not performant.

How to repeat:
CREATE TABLE `test_table` (
  `a` bigint NOT NULL,
  `b` bigint NOT NULL,
  `c` bigint NOT NULL,
  PRIMARY KEY (`a`),
  KEY `bc_idx` (`b`,`c`)
)

explain select c, count(*) from test_table where b IN (1, 2, 3) group by c;
(note that this uses a temporary table, which is not performant)

explain select c, count(*) from test_table where b = 1 group by c;
(this one can use just the index)

explain select c from test_table where b in (1, 2, 3);
(this is the closest available workaround (still have to sum rows after the query), which avoids using a temporary table)

Suggested fix:
"The GROUP BY does not begin with the first part of the key, but there is a condition that provides a constant for that part" (https://dev.mysql.com/doc/refman/8.4/en/group-by-optimization.html)
should also apply to a list of constants.
[7 Feb 8:36] MySQL Verification Team
Hi,

This is not a bug, but I will verify this as FEATURE REQUEST.

Thanks