pingcap/tidb

Prepare query with ORDER BY clause does not work with sql binding

Open

#63,805 创建于 2025年9月29日

在 GitHub 查看
 (3 评论) (0 反应) (1 负责人)Go (6,186 fork)batch import
affects-8.5good first issuehelp wantedreport/customerseverity/moderatesig/plannertype/bug

仓库指标

Star
 (40,090 star)
PR 合并指标
 (平均合并 14天 4小时) (30 天内合并 346 个 PR)

描述

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

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

mysql> create binding for select min(id),a from t where a=1 group by a order by min(id) using select min(id),a from t use index(idx) where a=1 group by a order by min(id);
Query OK, 0 rows affected (0.00 sec)

mysql> select min(id),a from t where a=1 group by a order by min(id);
Empty set (0.02 sec)

mysql> select @@last_plan_from_binding;
+--------------------------+
| @@last_plan_from_binding |
+--------------------------+
|                        1 |
+--------------------------+
1 row in set (0.00 sec)

mysql> prepare stmt from 'select min(id),a from t where a=? group by a order by min(id)';
Query OK, 0 rows affected (0.00 sec)

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

mysql> execute stmt using @a;
Empty set (0.00 sec)

mysql> select @@last_plan_from_binding;
+--------------------------+
| @@last_plan_from_binding |
+--------------------------+
|                        0 |
+--------------------------+
1 row in set (0.00 sec)

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

The last_plan_from_binding returns 1 for prepare query.

3. What did you see instead (Required)

The binding doesn't work.

4. What is your TiDB version? (Required)

v8.5.3

贡献者指南