Skip to content

Init Stats May Be Very Slow When Using Information Schema V1 with Partitioned Tables #64509

Closed

Description

@0xPoe

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

Image
select /*+ ORDER_INDEX(mysql.stats_histograms,tbl) */ HIGH_PRIORITY table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, correlation, flag, last_analyze_pos from mysql.stats_histograms where table_id >= 2192000 and table_id < 2192500 order by table_id;
| id                             | estRows | estCost   | actRows | task      | access object                                                  | execution info                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | operator info                                                                                                                                                                                                                                                                                                                                                                                                                 | memory   | disk  |
| Projection_8                   | 250.00  | 492478.33 | 1670    | root      |                                                                | time:7.62ms, loops:3, Concurrency:OFF                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | mysql.stats_histograms.table_id, mysql.stats_histograms.is_index, mysql.stats_histograms.hist_id, mysql.stats_histograms.distinct_count, mysql.stats_histograms.version, mysql.stats_histograms.null_count, mysql.stats_histograms.cm_sketch, mysql.stats_histograms.tot_col_size, mysql.stats_histograms.stats_ver, mysql.stats_histograms.correlation, mysql.stats_histograms.flag, mysql.stats_histograms.last_analyze_pos | 186.8 KB | N/A   |
| └─Projection_12                | 250.00  | 492178.93 | 1670    | root      |                                                                | time:7.58ms, loops:3, Concurrency:OFF                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | mysql.stats_histograms.table_id, mysql.stats_histograms.is_index, mysql.stats_histograms.hist_id, mysql.stats_histograms.distinct_count, mysql.stats_histograms.null_count, mysql.stats_histograms.tot_col_size, mysql.stats_histograms.version, mysql.stats_histograms.cm_sketch, mysql.stats_histograms.stats_ver, mysql.stats_histograms.flag, mysql.stats_histograms.correlation, mysql.stats_histograms.last_analyze_pos | 196.3 KB | N/A   |
|   └─IndexLookUp_11             | 250.00  | 491879.53 | 1670    | root      |                                                                | time:7.56ms, loops:3, index_task: {total_time: 2.08ms, fetch_handle: 1.91ms, build: 153.8µs, wait: 20.4µs}, table_task: {total_time: 8.17ms, num: 2, concurrency: 5}, next: {wait_index: 2.09ms, wait_table_lookup_build: 252.4µs, wait_table_lookup_resp: 4.89ms}                                                                                                                                                                                                                                                                                                                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                                                                                                               | 374.0 KB | N/A   |
|     ├─IndexRangeScan_9(Build)  | 250.00  | 61050.00  | 1670    | cop[tikv] | table:stats_histograms, index:tbl(table_id, is_index, hist_id) | time:1.86ms, loops:4, cop_task: {num: 1, max: 1.8ms, proc_keys: 1670, tot_proc: 638.1µs, tot_wait: 17.3µs, copr_cache_hit_ratio: 0.00, build_task_duration: 10.1µs, max_distsql_concurrency: 1}, rpc_info:{Cop:{num_rpc:1, total_time:1.79ms}}, tikv_task:{time:0s, loops:6}, scan_detail: {total_process_keys: 1670, total_process_keys_size: 103540, total_keys: 1671, get_snapshot_time: 5.26µs, rocksdb: {key_skipped_count: 1670, block: {cache_hit_count: 11}}}, time_detail: {total_process_time: 638.1µs, total_wait_time: 17.3µs, tikv_wall_time: 698.6µs}                                                                                                                                             | range:[1742000,1742500), keep order:true, stats:pseudo                                                                                                                                                                                                                                                                                                                                                                        | N/A      | N/A   |
|     └─TableRowIDScan_10(Probe) | 250.00  | 104559.57 | 1670    | cop[tikv] | table:stats_histograms                                         | time:7.54ms, loops:4, cop_task: {num: 7, max: 4.51ms, min: 740.1µs, avg: 1.91ms, p95: 4.51ms, max_proc_keys: 624, p95_proc_keys: 624, tot_proc: 8.94ms, tot_wait: 152.5µs, copr_cache_hit_ratio: 0.00, build_task_duration: 44.9µs, max_distsql_concurrency: 4}, rpc_info:{Cop:{num_rpc:7, total_time:13.3ms}}, tikv_task:{proc max:0s, min:0s, avg: 0s, p80:0s, p95:0s, iters:22, tasks:7}, scan_detail: {total_process_keys: 1670, total_process_keys_size: 148630, total_keys: 1912, get_snapshot_time: 56.8µs, rocksdb: {key_skipped_count: 1525, block: {cache_hit_count: 2554}}}, time_detail: {total_process_time: 8.94ms, total_suspend_time: 34.3µs, total_wait_time: 152.5µs, tikv_wall_time: 9.73ms} | keep order:false, stats:pseudo                                                                                                                                                                                                                                                                                                                                                                                                | N/A      | N/A   |

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

No slow queries.

3. What did you see instead (Required)

Very slow queries.

4. What is your TiDB version? (Required)

master, v8.5.x

Activity

  1. 0xPoe commented on Nov 16, 2025

    @0xPoe
    MemberAuthor

    Introduced by: #57574

  2. 0xPoe commented on Nov 16, 2025

    @0xPoe
    MemberAuthor

    The issue comes from:

    func (is *infoSchema) FindTableByPartitionID(partitionID int64) (table.Table, *model.DBInfo, *model.PartitionDefinition) {

    func (is *infoSchema) FindTableByPartitionID(partitionID int64) (table.Table, *model.DBInfo, *model.PartitionDefinition) {
            for _, v := range is.schemaMap {
                    for _, tbl := range v.tables {
                            pi := tbl.Meta().GetPartitionInfo()
                            if pi == nil {
                                    continue
                            }
                            for _, p := range pi.Definitions {
                                    if p.ID == partitionID {
                                            return tbl, v.dbInfo, &p
                                    }
                            }
                    }
            }
            return nil, nil, nil
    }
    
    // Meta implements table.Table Meta interface.
    func (t *TableCommon) Meta() *model.TableInfo {
            return t.meta
    Image

    The CPU is heavily consumed after the call to FindTableByPartitionID. After reviewing the entire implementation, I found that it is an O(n) function. This means each invocation may trigger a full scan of all 200,000 tables to retrieve partition table information.

    In this issue, the number of full-table traversals grows linearly with the number of tables. The more partitioned tables, total tables, or columns there are, the more significant the problem becomes. For clusters with a large number of tables, the fallback of init stats becomes more noticeable. Since init stats affects the readiness time of each node during rolling restarts, the primary impact is that the overall rolling restart duration becomes longer — and the larger the number of tables, the longer it gets.

  3. 0xPoe commented on Nov 16, 2025

    @0xPoe
    MemberAuthor
  4. kennedy8312 commented on Nov 18, 2025

    @kennedy8312

    /type regression

  5. added a commit that references this issue on Nov 18, 2025
    9b24940
  6. added 2 commits that reference this issue on Nov 18, 2025
    16c343d
    69b1dbe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions