Skip to content
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

planner: unexpected FullScan when using prepared plan cache #38533

Closed
qw4990 opened this issue Oct 18, 2022 · 2 comments · Fixed by #38537
Closed

planner: unexpected FullScan when using prepared plan cache #38533

qw4990 opened this issue Oct 18, 2022 · 2 comments · Fixed by #38537
Assignees
Labels
affects-5.4 This bug affects 5.4.x versions. affects-6.1 affects-6.5 epic/plan-cache sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@qw4990
Copy link
Contributor

qw4990 commented Oct 18, 2022

Enhancement

prepare st from "select /*+ use_index(t, a) */ a from t where a=? and a=?";
-- prepare st from "select /*+ use_index(t, a) */ a from t where a>=? and a<=?";
set @a=1;
execute st using @a, @a;

The Plan:

+---------------------------+----------+---------+-----------+---------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+--------+------+
| Selection_8               | 10.00    | 0       | root      |                     | time:820.7µs, loops:1                                                                                                                                        | eq(test.t.a, 1), eq(test.t.a, 1) | N/A    | N/A  |
| └─IndexReader_7           | 10.00    | 0       | root      |                     | time:816.9µs, loops:1, cop_task: {num: 1, max: 677.4µs, proc_keys: 0, rpc_num: 1, rpc_time: 593.4µs, copr_cache_hit_ratio: 0.00, distsql_concurrency: 15}    | index:Selection_6                | N/A    | N/A  |
|   └─Selection_6           | 10.00    | 0       | cop[tikv] |                     | tikv_task:{time:533.6µs, loops:0}                                                                                                                            | eq(test.t.a, 1), eq(test.t.a, 1) | N/A    | N/A  |
|     └─IndexFullScan_5     | 10000.00 | 0       | cop[tikv] | table:t, index:a(a) | tikv_task:{time:533.6µs, loops:0}                                                                                                                            | keep order:false, stats:pseudo   | N/A    | N/A  |
+---------------------------+----------+---------+-----------+---------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+--------+------+

The reason is to avoid invalid plans, the optimizer skips to build ranges for prepared statements in this case. (see https://github.com/pingcap/tidb/blob/master/util/ranger/detacher.go#L581)

@qw4990 qw4990 added type/enhancement The issue or PR belongs to an enhancement. sig/planner SIG: Planner epic/plan-cache labels Oct 18, 2022
@qw4990
Copy link
Contributor Author

qw4990 commented Oct 19, 2022

Compared with skipping the optimization and then caching a bad plan, maybe we should process this optimization to generate an optimal plan and not cache it.

@time-and-fate
Copy link
Member

A relevant but different issue: #34561

ti-chi-bot pushed a commit that referenced this issue Oct 20, 2022
@qw4990 qw4990 added affects-5.4 This bug affects 5.4.x versions. affects-6.1 type/bug The issue is confirmed as a bug. and removed type/enhancement The issue or PR belongs to an enhancement. labels Mar 28, 2023
ti-chi-bot added a commit that referenced this issue Mar 28, 2023
ti-chi-bot pushed a commit that referenced this issue Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.4 This bug affects 5.4.x versions. affects-6.1 affects-6.5 epic/plan-cache sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
3 participants