pingcap/tidb

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

Open

#63805 opened on Sep 29, 2025

View on GitHub
 (3 comments) (0 reactions) (1 assignee)Go (40,090 stars) (6,186 forks)batch import
affects-8.5good first issuehelp wantedreport/customerseverity/moderatesig/plannertype/bug

Description

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

Contributor guide