Skip to content

Optimizer chose tablerangescan with order rather than indexrangescan #61540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
terry1purcell opened this issue Jun 6, 2025 · 0 comments · May be fixed by #61506
Open

Optimizer chose tablerangescan with order rather than indexrangescan #61540

terry1purcell opened this issue Jun 6, 2025 · 0 comments · May be fixed by #61506
Labels
affects-9.0 This bug affects the 9.0.x versions. severity/moderate sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@terry1purcell
Copy link
Contributor

terry1purcell commented Jun 6, 2025

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

CREATE TABLE `t1` (
  `a` int,
  `b` int,
  `c` int,
  Primary KEY `iba` (`a`, `b`),
  KEY `iac` (`a`, `c`)
);

set @@cte_max_recursion_depth=10000000;
INSERT INTO t1 (a, b, c)
SELECT mod(a, 80) AS a, a as b, mod(a, 100) AS c
FROM (
    WITH RECURSIVE x AS (
        SELECT 1 AS a
        UNION ALL
        SELECT a + 1 AS a
        FROM x
        WHERE a < 1000000
    )
    SELECT a
    FROM x
) AS subquery;

update t1 set a = 6 where a < 6;

analyze table t1;

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

tidb> explain analyze select * from t1 use index (iac) where a = 6 and c = 66 order by b limit 10000;
+---------------------------+---------+---------+-----------+---------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------+---------+---------+
| id                        | estRows | actRows | task      | access object             | execution info                                                                                                                                                                                                                                                                                                | operator info                       | memory  | disk    |
+---------------------------+---------+---------+-----------+---------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------+---------+---------+
| TopN_10                   | 2496.69 | 2500    | root      |                           | time:23.6ms, open:389.7µs, close:30.8µs, loops:4, RU:9.33                                                                                                                                                                                                                                                     | test.t1.b, offset:0, count:10000    | 89.0 KB | 0 Bytes |
| └─IndexReader_19          | 2496.69 | 2500    | root      |                           | time:20.6ms, open:293µs, close:28.7µs, loops:5, cop_task: {num: 5, max: 9.71ms, min: 705.3µs, avg: 3.98ms, p95: 9.71ms, tot_proc: 18ms, copr_cache_hit_ratio: 0.00, build_task_duration: 186.1µs, max_distsql_concurrency: 1}, fetch_resp_duration: 19.9ms, rpc_info:{Cop:{num_rpc:5, total_time:19.8ms}}     | index:IndexRangeScan_18             | 39.2 KB | N/A     |
|   └─IndexRangeScan_18     | 2496.69 | 2500    | cop[tikv] | table:t1, index:iac(a, c) | tikv_task:{proc max:9.55ms, min:678.4µs, avg: 3.92ms, p80:9.55ms, p95:9.55ms, iters:0, tasks:5}, time_detail: {total_process_time: 18ms}                                                                                                                                                                      | range:[6 66,6 66], keep order:false | N/A     | N/A     |
+---------------------------+---------+---------+-----------+---------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------+---------+---------+

3. What did you see instead (Required)

tidb> explain analyze select * from t1 where a = 6 and c = 66 order by b limit 10000;

+-------------------------------+----------+---------+-----------+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------+---------+------+
| id                            | estRows  | actRows | task      | access object | execution info                                                                                                                                                                                                                                                                                         | operator info                | memory  | disk |
+-------------------------------+----------+---------+-----------+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------+---------+------+
| Limit_12                      | 2496.69  | 2500    | root      |               | time:78.3ms, open:50.4µs, close:7.33µs, loops:4, RU:28.66                                                                                                                                                                                                                                              | offset:0, count:10000        | N/A     | N/A  |
| └─TableReader_23              | 2496.69  | 2500    | root      |               | time:78.2ms, open:45.3µs, close:6.83µs, loops:4, cop_task: {num: 5, max: 27.7ms, min: 9.2ms, avg: 15.6ms, p95: 27.7ms, tot_proc: 76ms, copr_cache_hit_ratio: 0.00, build_task_duration: 28µs, max_distsql_concurrency: 1}, fetch_resp_duration: 78.1ms, rpc_info:{Cop:{num_rpc:5, total_time:78ms}}    | data:Limit_22                | 39.2 KB | N/A  |
|   └─Limit_22                  | 2496.69  | 2500    | cop[tikv] |               | tikv_task:{proc max:27.7ms, min:9.18ms, avg: 15.6ms, p80:27.7ms, p95:27.7ms, iters:0, tasks:5}, time_detail: {total_process_time: 76ms}                                                                                                                                                                | offset:0, count:10000        | N/A     | N/A  |
|     └─Selection_21            | 2496.69  | 2500    | cop[tikv] |               | tikv_task:{proc max:27.7ms, min:9.18ms, avg: 15.6ms, p80:27.7ms, p95:27.7ms, iters:0, tasks:5}                                                                                                                                                                                                         | eq(test.t1.c, 66)            | N/A     | N/A  |
|       └─TableRangeScan_20     | 87288.63 | 2500    | cop[tikv] | table:t1      | tikv_task:{proc max:27.7ms, min:9.18ms, avg: 15.6ms, p80:27.7ms, p95:27.7ms, iters:0, tasks:5}                                                                                                                                                                                                         | range:[6,6], keep order:true | N/A     | N/A  |
+-------------------------------+----------+---------+-----------+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------+---------+------+

4. What is your TiDB version? (Required)

Current (master)

@terry1purcell terry1purcell added type/bug The issue is confirmed as a bug. affects-9.0 This bug affects the 9.0.x versions. labels Jun 6, 2025
@hawkingrei hawkingrei added the sig/planner SIG: Planner label Jun 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-9.0 This bug affects the 9.0.x versions. severity/moderate sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants