Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions pkg/statistics/handle/cache/stats_table_row_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions tests/integrationtest/r/globalindex/information_schema.result
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions tests/integrationtest/t/globalindex/information_schema.test
Original file line number Diff line number Diff line change
Expand Up @@ -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;