Skip to content

Commit

Permalink
statistics: fix unnecessary index fmsketch loading (#42074)
Browse files Browse the repository at this point in the history
close #42052
  • Loading branch information
xuyifangreeneyes authored Mar 10, 2023
1 parent a273039 commit cdab358
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
24 changes: 24 additions & 0 deletions statistics/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
15 changes: 10 additions & 5 deletions statistics/interact_with_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit cdab358

Please sign in to comment.