Bug #118486 Index is ignored in case of multiple values in "select" and the length of any one value is more than column length
Submitted: 19 Jun 10:07 Modified: 14 Jul 12:17
Reporter: Claude Veigas Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S5 (Performance)
Version:8.4.3, 8.4.5, 9.3.0 OS:Any (AWS RDS)
Assigned to: CPU Architecture:Any
Tags: MySQL, mysql 8.4.3

[19 Jun 10:07] Claude Veigas
Description:
I have a table with a varchar(10) column, and it is indexed.

When I perform a "select * from table where column_name in ('value1','value2')", I come across the below issue based on values being passed.

Conditions:
1. If length(value1) <=10 and length(value2) <= 10 characters, index is picked up.
2. If only one value is passed and its length is more than 10, index is picked up.
3. If length(value1) <= 10 and length(value2) > 10, index is not picked up.

We recently upgraded from 8.0.40 to 8.4.3 and encountered this problem in the new version.

How to repeat:
Condition 1:
mysql> explain select id from bugtest where bug in ('value1','value2');
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | bugtest | NULL       | range | bug_ndx       | bug_ndx | 43      | NULL |    2 |   100.00 | Using index condition |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

Condition 2:
mysql> explain select id from bugtest where bug in ('value1_greater_than_10');
+----+-------------+---------+------------+------+---------------+---------+---------+-------+------+----------+-----------------------+
| id | select_type | table   | partitions | type | possible_keys | key     | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+---------+------------+------+---------------+---------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | bugtest | NULL       | ref  | bug_ndx       | bug_ndx | 43      | const |    1 |   100.00 | Using index condition |
+----+-------------+---------+------------+------+---------------+---------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

Condition 3:
mysql> explain select id from bugtest where bug in ('value1_greater_than_10','value2');
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | bugtest | NULL       | ALL  | bug_ndx       | NULL | NULL    | NULL |    1 |   100.00 | Using where |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

Suggested fix:
In this case, the optimizer should work the same way as 8.0.40.
[19 Jun 10:18] Claude Veigas
I repeated the same commands on 8.0.40 and the indexes were picked in all three conditions.
[19 Jun 10:19] Claude Veigas
I repeated the same commands on Mysql 8.0.40 and the indexes were picked in all three conditions.
[19 Jun 10:30] Claude Veigas
Updated Synopsis
[19 Jun 10:48] Claude Veigas
Adding create statement for the above table:
mysql> create table bugtest (id int, bug varchar(10));
Query OK, 0 rows affected (0.07 sec)

mysql> create index bug_ndx on bugtest(bug);
Query OK, 0 rows affected (0.17 sec)
[19 Jun 14:13] MySQL Verification Team
Hello Claude Vegas,

Thank you for the report and test case.

regards,
Umesh
[14 Jul 12:17] Knut Anders Hatlen
This seems to be the same problem as bug#118009, so I'm closing this report as a duplicate.