Inconsistent query plans when Functional Indexes are used

Description

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";
  1. 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
  2. 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 | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+

    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.

Environment

None

AFFECTED CS IDs

CS0047375

Activity

Show:

Julia Vural September 30, 2024 at 10:40 AM

This is a design flaw coming from upstream. Currently, the fix cannot be made on the downstream side as it would cause code to deviate from upstream tremendously and make it impossible to maintain.

Julia Vural September 26, 2024 at 1:07 PM

This is a design flaw coming from upstream.

Julia Vural July 1, 2024 at 10:44 AM

The upstream bug was marked as a duplicate of another upstream bug that was opened in 2021. It looks like upstream has no intention to fix it. There is a possibility that this might not be an easy fix due to the current design.

Won't Do

Details

Assignee

Reporter

Needs QA

Yes

Time tracking

1d logged

Affects versions

Priority

Smart Checklist

Created June 25, 2024 at 9:22 AM
Updated October 1, 2024 at 9:40 AM
Resolved September 30, 2024 at 10:40 AM

Flag notifications