Skip to content

planner: read-only variables with uppercase name cannot be converted to constant and lead to sub-optimal plans #66339

Closed

Description

@qw4990

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

mysql> create table t (a int, key(a));
Query OK, 0 rows affected (0.020 sec)

mysql> set @a=1;
Query OK, 0 rows affected (0.000 sec)

mysql> explain select a from t where a=@a;
+------------------------+---------+-----------+---------------------+---------------------------------------------+
| id                     | estRows | task      | access object       | operator info                               |
+------------------------+---------+-----------+---------------------+---------------------------------------------+
| IndexReader_6          | 10.00   | root      |                     | index:IndexRangeScan_5                      |
| └─IndexRangeScan_5     | 10.00   | cop[tikv] | table:t, index:a(a) | range:[1,1], keep order:false, stats:pseudo |
+------------------------+---------+-----------+---------------------+---------------------------------------------+
2 rows in set (0.001 sec)

mysql> 
mysql> set @A=1;
Query OK, 0 rows affected (0.000 sec)

mysql> explain select a from t where a=@A;
+-------------------------+----------+-----------+---------------------+--------------------------------+
| id                      | estRows  | task      | access object       | operator info                  |
+-------------------------+----------+-----------+---------------------+--------------------------------+
| Selection_5             | 8000.00  | root      |                     | eq(test.t.a, getvar("a"))      |
| └─IndexReader_9         | 10000.00 | root      |                     | index:IndexFullScan_8          |
|   └─IndexFullScan_8     | 10000.00 | cop[tikv] | table:t, index:a(a) | keep order:false, stats:pseudo |
+-------------------------+----------+-----------+---------------------+--------------------------------+

2. What did you expect to see? (Required)

The second plan should also use IndexRangeScan.

3. What did you see instead (Required)

It's using IndexFullScan.

4. What is your TiDB version? (Required)

Master & v8.5.4

Activity

  1. added 2 commits that reference this issue on Feb 24, 2026
    da5c6fd
    1206baa
  2. self-assigned this
    on Apr 30, 2026
  3. added a commit that references this issue on Apr 30, 2026
    6569d59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions