diff --git a/pkg/statistics/handle/cache/stats_table_row_cache.go b/pkg/statistics/handle/cache/stats_table_row_cache.go index d9753fda64d7c..5d7b3bc8b7de5 100644 --- a/pkg/statistics/handle/cache/stats_table_row_cache.go +++ b/pkg/statistics/handle/cache/stats_table_row_cache.go @@ -147,10 +147,12 @@ func (c *StatsTableRowCache) Update(sctx sessionctx.Context) error { // Returns row count, average row length, total data length, and all indexed column length. func (c *StatsTableRowCache) EstimateDataLength(table *model.TableInfo) ( rowCount uint64, avgRowLength uint64, dataLength uint64, indexLength uint64) { - if table.GetPartitionInfo() == nil { - rowCount = c.GetTableRows(table.ID) - dataLength, indexLength = c.GetDataAndIndexLength(table, table.ID, rowCount) - } else { + rowCount = c.GetTableRows(table.ID) + dataLength, indexLength = c.GetDataAndIndexLength(table, table.ID, rowCount) + if table.GetPartitionInfo() != nil { + // For partition table, data only stores in partition level. + // Keep `indexLength` for global index. + rowCount, dataLength = 0, 0 for _, pi := range table.GetPartitionInfo().Definitions { piRowCnt := c.GetTableRows(pi.ID) rowCount += piRowCnt @@ -257,6 +259,16 @@ func (c *StatsTableRowCache) GetDataAndIndexLength(info *model.TableInfo, physic if idx.State != model.StatePublic { continue } + if info.GetPartitionInfo() != nil { + // Global indexes calcuated in table level. + if idx.Global && info.ID != physicalID { + continue + } + // Normal indexes calculated in partition level. + if !idx.Global && info.ID == physicalID { + continue + } + } for _, col := range idx.Columns { if col.Length == types.UnspecifiedLength { indexLength += columnLength[col.Name.L] diff --git a/tests/integrationtest/r/globalindex/information_schema.result b/tests/integrationtest/r/globalindex/information_schema.result index 85a3df235a31b..6f5cf29197df7 100644 --- a/tests/integrationtest/r/globalindex/information_schema.result +++ b/tests/integrationtest/r/globalindex/information_schema.result @@ -7,8 +7,20 @@ UNIQUE KEY `idx1` (`b`), KEY `idx` (`b`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin PARTITION BY HASH (`a`) PARTITIONS 5; +insert into t values (1, 1); +analyze table t; select key_name, is_global from information_schema.tidb_indexes where table_name='t' and table_schema='globalindex__information_schema' order by key_name; key_name is_global idx 0 idx1 1 +select partition_name, data_length, index_length from information_schema.PARTITIONS where table_name='t' and table_schema='globalindex__information_schema' order by partition_name; +partition_name data_length index_length +p0 0 0 +p1 16 8 +p2 0 0 +p3 0 0 +p4 0 0 +select table_name, data_length, index_length from information_schema.tables where table_name='t' and table_schema='globalindex__information_schema'; +table_name data_length index_length +t 16 16 set tidb_enable_global_index=default; diff --git a/tests/integrationtest/t/globalindex/information_schema.test b/tests/integrationtest/t/globalindex/information_schema.test index 50be70b1528eb..1f63a1e27458f 100644 --- a/tests/integrationtest/t/globalindex/information_schema.test +++ b/tests/integrationtest/t/globalindex/information_schema.test @@ -9,7 +9,13 @@ CREATE TABLE `t` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin PARTITION BY HASH (`a`) PARTITIONS 5; +insert into t values (1, 1); +analyze table t; + select key_name, is_global from information_schema.tidb_indexes where table_name='t' and table_schema='globalindex__information_schema' order by key_name; +select partition_name, data_length, index_length from information_schema.PARTITIONS where table_name='t' and table_schema='globalindex__information_schema' order by partition_name; +select table_name, data_length, index_length from information_schema.tables where table_name='t' and table_schema='globalindex__information_schema'; + set tidb_enable_global_index=default;