pingcap / pingcap/tidb

planner, statistics: use selected partition stats for dynamic pruning

Open
#66,919 0 comments 0 reactions 0 assignees View on GitHub
component/statistics report/customer sig/planner type/enhancement
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Enhancement

TiDB currently prefers dynamic partition pruning for partitioned tables. This avoids building static-pruning plans, but it also means the optimizer usually estimates row counts from the partition table stats path instead of the stats of the actually selected partitions.

In some queries, this leads to less accurate cardinality estimation. The partition table global stats are aggregated from all partitions, so they are not a good replacement when the query only touches a subset of partitions.

We should improve this path by:

1. Keeping dynamic partition pruning as the execution mode.
2. Reusing the partition pruning result when the optimizer can determine the selected partitions during planning.
3. Merging stats from the selected partitions at runtime for cardinality estimation, instead of falling back to the partition table global stats.
4. Skipping plan cache when static-pruning information is involved, because cached plans can otherwise reuse an incorrect partition-specific estimation result.

This should improve plan quality for partitioned tables without reintroducing the old static-pruning execution behavior.

Contributor guide

Open the contributing guide

Research direction

No files or tests are named in the issue. Start by tracing TiDB's planner paths for dynamic partition pruning, selected-partition statistics, and plan caching. Done means dynamic pruning remains the execution mode, selected partition stats improve cardinality estimation, and plans involving static-pruning information are not cached.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases, performance
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.