From 735e1717ca8edded2507222931eedb63546d4d56 Mon Sep 17 00:00:00 2001 From: xuyifan <675434007@qq.com> Date: Thu, 9 Mar 2023 20:02:13 +0800 Subject: [PATCH 1/2] fix unnecessary index fmsketch loading --- statistics/integration_test.go | 24 ++++++++++++++++++++++++ statistics/interact_with_storage.go | 15 ++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/statistics/integration_test.go b/statistics/integration_test.go index 1c8672b790ffd..4f2c87fdd09f5 100644 --- a/statistics/integration_test.go +++ b/statistics/integration_test.go @@ -693,3 +693,27 @@ func TestSingleColumnIndexNDV(t *testing.T) { require.Equal(t, expectedResults[i][2], row[7]) // null_count } } + +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 c0acce31b77b7..ba7f6f78cb78c 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, @@ -454,7 +459,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) } From cf420a2e9e4c4b753f6bd5e6fa909ec091e90750 Mon Sep 17 00:00:00 2001 From: xuyifan <675434007@qq.com> Date: Fri, 10 Mar 2023 15:02:56 +0800 Subject: [PATCH 2/2] fmt --- statistics/integration_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/statistics/integration_test.go b/statistics/integration_test.go index e5bf21776f47e..a7563a6b06542 100644 --- a/statistics/integration_test.go +++ b/statistics/integration_test.go @@ -743,4 +743,3 @@ func TestUpdateNotLoadIndexFMSketch(t *testing.T) { require.Nil(t, h.GetPartitionStats(tblInfo, p0.ID).Indices[idxInfo.ID].FMSketch) require.Nil(t, h.GetPartitionStats(tblInfo, p1.ID).Indices[idxInfo.ID].FMSketch) } -