Inefficient query plan generated by normal SELECT compare to the prepared SELECT with false condition
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
Hi,
In the following test case, there are two equivalent queries, one is a normal SELECT, the other is a prepared SELECT. However, I found that the normal SELECT is 500x slower than the prepared SELECT. I found that these two queries generate different query plans. The prepared SELECT direct return the empty result, but the normal SELECT scans the table. Given that prepared statements typically result in suboptimal query plans, the query plan generation process for normal SELECT statements may still have room for further optimization.
```
CREATE TABLE t0(c0 CHAR , c2 DOUBLE UNSIGNED );
INSERT INTO t0 (c0, c2)
WITH RECURSIVE nr AS (
SELECT 1 AS n
UNION ALL
SELECT n + 1 FROM nr WHERE n < 1000
)
SELECT
CHAR(FLOOR(65 + RAND() * 26)),
RAND() * 10000
FROM nr;
EXPLAIN ANALYZE SELECT t0.c2 FROM t0 WHERE CAST((CASE 'T_' WHEN NULL THEN t0.c0 ELSE 0.7011748148148058 END ) AS CHAR);
id estRows actRows task access object execution info operator info memory disk
TableReader_7 10000.00 1000 root time:1.35ms, open:50.2µs, close:11.4µs, loops:2, RU:2.03, cop_task: {num: 4, max: 428.2µs, min: 165.4µs, avg: 257.9µs, p95: 428.2µs, copr_cache_hit_ratio: 0.00, build_task_duration: 11.5µs, max_distsql_concurrency: 1}, fetch_resp_duration: 1.14ms, rpc_info:{Cop:{num_rpc:4, total_time:924.8µs}} data:TableFullScan_6 7.05 KB N/A
└─TableFullScan_6 10000.00 1000 cop[tikv] table:t0 tikv_task:{proc max:294.8µs, min:142.9µs, avg: 200.8µs, p80:294.8µs, p95:294.8µs, iters:0, tasks:4} keep order:false, stats:pseudo N/A N/A
SET @c = 0.7011748148148058;
PREPARE explain_prepare_query FROM "EXPLAIN ANALYZE SELECT t0.c2 FROM t0 WHERE CAST((CASE 'T_' WHEN NULL THEN t0.c0 ELSE ? END ) AS CHAR);";
EXECUTE explain_prepare_query USING @c;
id estRows actRows task access object execution info operator info memory disk
TableDual_7 0.00 0 root time:2.85µs, open:410ns, close:130ns, loops:1, RU:0.00 rows:0 N/A N/A
```
### 2. What did you expect to see? (Required)
The normal SELECT should be faster or the same with the prepared SELECT.
### 3. What did you see instead (Required)
The normal SELECT is slower than the prepared SELECT.
### 4. What is your TiDB version? (Required)
Release Version: v9.0.0-beta.2.pre-1118-gfc6f7b2e42\nEdition: Community\nGit Commit Hash: fc6f7b2e42be08957a9b80e5761bfa4793066353\nGit Branch: master\nUTC Build Time: 2026-01-26 12:27:51\nGoVersion: go1.25.6\nRace Enabled: false\nCheck Table Before Drop: false\nStore: unistore\nKernel Type: Classic
Contributor guide
Assessment
This issue has not been assessed yet.