Bug #115427 Inconsistent query plans when Functional Indexes are used
Submitted: 25 Jun 9:14 Modified: 25 Jun 9:30
Reporter: Iwo P Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8 OS:Any
Assigned to: CPU Architecture:Any

[25 Jun 9:14] Iwo P
Description:
MySQL does not utilise functional indexes for operators like `LIKE'.  

How to repeat:
All of the below are executed under:
```
 SET SESSION debug="+d,show_hidden_columns";
```

01) Table schema:
CREATE TABLE `a` (
  `id` int NOT NULL AUTO_INCREMENT,
  `a` varchar(100) DEFAULT NULL,
  `!hidden!test01!0!0` varchar(100) GENERATED ALWAYS AS (lower(`a`)) VIRTUAL,
  PRIMARY KEY (`id`),
  KEY `test01` ((lower(`a`)))
) ENGINE=InnoDB AUTO_INCREMENT=122 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

02) Implicit usage does not use the `test01` index:
mysql> explain select * from a where lower(a) like 'a';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | a     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   96 |   100.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+

03) Explicit usage of the virtually generated columns makes use of that index:
mysql> explain select * from a where `!hidden!test01!0!0` like 'a';
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key    | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | a     | NULL       | range | test01        | tets01 | 403     | NULL |    1 |   100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+

Suggested fix:
The optimizer should use the functional indexes more consistently.
[25 Jun 9:30] MySQL Verification Team
Hello Iwo P,

Thank you for the report and feedback.
IMHO this is duplicate of Bug #104713, please see Bug #104713

regards,
Umesh