Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: fix unnecessary index fmsketch loading #44330

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
15 changes: 10 additions & 5 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ func (h *Handle) fmSketchFromStorage(reader *statsReader, tblID int64, isIndex,
return statistics.DecodeFMSketch(rows[0].GetBytes(0))
}

func (h *Handle) indexStatsFromStorage(reader *statsReader, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo) error {
func (h *Handle) indexStatsFromStorage(reader *statsReader, row chunk.Row, table *statistics.Table, tableInfo *model.TableInfo, loadAll bool) error {
histID := row.GetInt64(2)
distinct := row.GetInt64(3)
histVer := row.GetUint64(4)
Expand Down Expand Up @@ -1278,9 +1278,14 @@ func (h *Handle) indexStatsFromStorage(reader *statsReader, row chunk.Row, table
if err != nil {
return errors.Trace(err)
}
fmSketch, err := h.fmSketchFromStorage(reader, table.PhysicalID, 1, histID)
if err != nil {
return errors.Trace(err)
var fmSketch *statistics.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 = h.fmSketchFromStorage(reader, table.PhysicalID, 1, histID)
if err != nil {
return errors.Trace(err)
}
}
idx = &statistics.Index{
Histogram: *hg,
Expand Down Expand Up @@ -1477,7 +1482,7 @@ func (h *Handle) TableStatsFromStorage(tableInfo *model.TableInfo, physicalID in
}
for _, row := range rows {
if row.GetInt64(1) > 0 {
err = h.indexStatsFromStorage(reader, row, table, tableInfo)
err = h.indexStatsFromStorage(reader, row, table, tableInfo, loadAll)
} else {
err = h.columnStatsFromStorage(reader, row, table, tableInfo, loadAll)
}
Expand Down
24 changes: 24 additions & 0 deletions statistics/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,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)
}