diff --git a/statistics/integration_test.go b/statistics/integration_test.go index 569a23dbbb..a7563a6b06 100644 --- a/statistics/integration_test.go +++ b/statistics/integration_test.go @@ -719,3 +719,27 @@ func TestColumnStatsLazyLoad(t *testing.T) { require.True(t, h.GetTableStats(tblInfo).Columns[c1.ID].IsAllEvicted()) require.True(t, h.GetTableStats(tblInfo).Columns[c2.ID].IsAllEvicted()) } + +func TestUpdateNotLoadIndexFMSketch(t *testing.T) { + store, dom := testkit.CreateMockStoreAndDomain(t) + tk := testkit.NewTestKit(t, store) + h := dom.StatsHandle() + tk.MustExec("use test") + tk.MustExec("create table t(a int, b int, index idx(a)) partition by range (a) (partition p0 values less than (10),partition p1 values less than maxvalue)") + tk.MustExec("insert into t values (1,2), (3,4), (5,6), (7,8)") + require.NoError(t, h.HandleDDLEvent(<-h.DDLEventCh())) + tk.MustExec("analyze table t") + is := dom.InfoSchema() + tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) + require.NoError(t, err) + tblInfo := tbl.Meta() + idxInfo := tblInfo.Indices[0] + p0 := tblInfo.Partition.Definitions[0] + p1 := tblInfo.Partition.Definitions[1] + require.Nil(t, h.GetPartitionStats(tblInfo, p0.ID).Indices[idxInfo.ID].FMSketch) + require.Nil(t, h.GetPartitionStats(tblInfo, p1.ID).Indices[idxInfo.ID].FMSketch) + h.Clear() + require.NoError(t, h.Update(is)) + require.Nil(t, h.GetPartitionStats(tblInfo, p0.ID).Indices[idxInfo.ID].FMSketch) + require.Nil(t, h.GetPartitionStats(tblInfo, p1.ID).Indices[idxInfo.ID].FMSketch) +} diff --git a/statistics/interact_with_storage.go b/statistics/interact_with_storage.go index 1ad1b82080..8231b90dec 100644 --- a/statistics/interact_with_storage.go +++ b/statistics/interact_with_storage.go @@ -253,7 +253,7 @@ func ExtendedStatsFromStorage(reader *StatsReader, table *Table, physicalID int6 return table, nil } -func indexStatsFromStorage(reader *StatsReader, row chunk.Row, table *Table, tableInfo *model.TableInfo) error { +func indexStatsFromStorage(reader *StatsReader, row chunk.Row, table *Table, tableInfo *model.TableInfo, loadAll bool) error { histID := row.GetInt64(2) distinct := row.GetInt64(3) histVer := row.GetUint64(4) @@ -279,9 +279,14 @@ func indexStatsFromStorage(reader *StatsReader, row chunk.Row, table *Table, tab if err != nil { return errors.Trace(err) } - fmSketch, err := FMSketchFromStorage(reader, table.PhysicalID, 1, histID) - if err != nil { - return errors.Trace(err) + var fmSketch *FMSketch + if loadAll { + // FMSketch is only used when merging partition stats into global stats. When merging partition stats into global stats, + // we load all the statistics, i.e., loadAll is true. + fmSketch, err = FMSketchFromStorage(reader, table.PhysicalID, 1, histID) + if err != nil { + return errors.Trace(err) + } } idx = &Index{ Histogram: *hg, @@ -467,7 +472,7 @@ func TableStatsFromStorage(reader *StatsReader, tableInfo *model.TableInfo, phys } for _, row := range rows { if row.GetInt64(1) > 0 { - err = indexStatsFromStorage(reader, row, table, tableInfo) + err = indexStatsFromStorage(reader, row, table, tableInfo, loadAll) } else { err = columnStatsFromStorage(reader, row, table, tableInfo, loadAll, lease) }