pingcap / pingcap/tidb

planner: prevent the optimizer from using different versions of stats of a table during optimization

Open
#50,281 0 comments 0 reactions 0 assignees View on GitHub
component/statistics epic/cardinality-estimation sig/planner type/enhancement
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Enhancement
```
create table t (a int, b int, key(a), key(b));
insert into t values (1, 1);
analyze table t; -- stats-ver1
set global tidb_stats_load_sync_wait=1;

-- restart TiDB
explain select * from t where a=1; -- load stats-col-a

explain select * from t where a=1 and b=1; -- load stats-col-b timeout
+-------------------------+---------+-----------+---------------+-----------------------------------------------+
| id | estRows | task | access object | operator info |
+-------------------------+---------+-----------+---------------+-----------------------------------------------+
| TableReader_7 | 2.00 | root | | data:Selection_6 |
| └─Selection_6 | 2.00 | cop[tikv] | | eq(test.t.a, 1), eq(test.t.b, 1) |
| └─TableFullScan_5 | 2.00 | cop[tikv] | table:t | keep order:false, stats:partial[b:allEvicted] |
+-------------------------+---------+-----------+---------------+-----------------------------------------------+
```

In the second query, the optimizer uses 2 versions of statistics of table `t` where `stats-col-a` is loaded and then stats-ver1 is used while `stats-col-b` is not loaded and then pseudo is used, which seems risky.
Should we force the optimizer to always use one statistics version of a table during optimization? (all stats-ver1 or all stats-pseudo, no in-between state, not use them together).

Contributor guide

Open the contributing guide

Research direction

Reproduce the SQL sequence in the issue, including the restart and tidb_stats_load_sync_wait setting, then trace the optimizer's table-statistics loading and fallback behavior. Determine how optimization should select one statistics version for all columns; done means the second EXPLAIN no longer combines loaded stats with pseudo statistics.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.