Skip to content

Commit

Permalink
statistics: remove unused LastAnalyzePos field (#58859)
Browse files Browse the repository at this point in the history
ref #55043
  • Loading branch information
Rustin170506 authored Jan 13, 2025
1 parent 6d2a05e commit c44672e
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 56 deletions.
10 changes: 4 additions & 6 deletions pkg/statistics/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ import (

// Column represents a column histogram.
type Column struct {
LastAnalyzePos types.Datum
CMSketch *CMSketch
TopN *TopN
FMSketch *FMSketch
Info *model.ColumnInfo
CMSketch *CMSketch
TopN *TopN
FMSketch *FMSketch
Info *model.ColumnInfo
Histogram

// StatsLoadedStatus indicates the status of column statistics
Expand All @@ -54,7 +53,6 @@ func (c *Column) Copy() *Column {
StatsVer: c.StatsVer,
IsHandle: c.IsHandle,
}
c.LastAnalyzePos.Copy(&nc.LastAnalyzePos)
if c.CMSketch != nil {
nc.CMSketch = c.CMSketch.Copy()
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/statistics/handle/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
table.StatsVer = int(statsVer)
}
id, ndv, nullCount, version, totColSize := row.GetInt64(2), row.GetInt64(3), row.GetInt64(5), row.GetUint64(4), row.GetInt64(7)
lastAnalyzePos := row.GetDatum(10, types.NewFieldType(mysql.TypeBlob))
tbl, ok := h.TableInfoByID(is, table.PhysicalID)
if !ok {
// this table has been dropped. but stats meta still exists and wait for being deleted.
Expand Down Expand Up @@ -235,7 +234,6 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
// The LastAnalyzeVersion is added by ALTER table so its value might be 0.
table.LastAnalyzeVersion = max(table.LastAnalyzeVersion, version)
}
lastAnalyzePos.Copy(&index.LastAnalyzePos)
table.SetIdx(idxInfo.ID, index)
table.ColAndIdxExistenceMap.InsertIndex(idxInfo.ID, statsVer != statistics.Version0)
} else {
Expand All @@ -258,7 +256,6 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
IsHandle: tbl.Meta().PKIsHandle && mysql.HasPriKeyFlag(colInfo.GetFlag()),
StatsVer: statsVer,
}
lastAnalyzePos.Copy(&col.LastAnalyzePos)
table.SetCol(hist.ID, col)
table.ColAndIdxExistenceMap.InsertCol(colInfo.ID, statsVer != statistics.Version0 || ndv > 0 || nullCount > 0)
if statsVer != statistics.Version0 {
Expand All @@ -283,7 +280,7 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
// genInitStatsHistogramsSQL generates the SQL to load all stats_histograms records.
// We need to read all the records since we need to do initialization of table.ColAndIdxExistenceMap.
func genInitStatsHistogramsSQL(isPaging bool) string {
selectPrefix := "select /*+ ORDER_INDEX(mysql.stats_histograms,tbl) */ HIGH_PRIORITY table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, correlation, last_analyze_pos from mysql.stats_histograms"
selectPrefix := "select /*+ ORDER_INDEX(mysql.stats_histograms,tbl) */ HIGH_PRIORITY table_id, is_index, hist_id, distinct_count, version, null_count, cm_sketch, tot_col_size, stats_ver, correlation from mysql.stats_histograms"
orderSuffix := " order by table_id"
if !isPaging {
return selectPrefix + orderSuffix
Expand Down
3 changes: 0 additions & 3 deletions pkg/statistics/handle/handletest/statstest/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ func TestInitStats(t *testing.T) {
h.Clear()
require.NoError(t, h.InitStats(context.Background(), is))
table0 := h.GetTableStats(tbl.Meta())
require.Equal(t, uint8(0x3), table0.GetIdx(1).LastAnalyzePos.GetBytes()[0])
h.Clear()
require.NoError(t, h.Update(context.Background(), is))
// Index and pk are loaded.
Expand Down Expand Up @@ -414,8 +413,6 @@ func initStatsVer2(t *testing.T) {
require.True(t, !table0.GetCol(4).IsStatsInitialized())
require.True(t, table0.GetCol(5).IsStatsInitialized())
require.Equal(t, 2, table0.IdxNum())
require.Equal(t, uint8(0x3), table0.GetIdx(1).LastAnalyzePos.GetBytes()[0])
require.Equal(t, uint8(0x3), table0.GetIdx(2).LastAnalyzePos.GetBytes()[0])
h.Clear()
require.NoError(t, h.InitStats(context.Background(), is))
table1 := h.GetTableStats(tbl.Meta())
Expand Down
26 changes: 9 additions & 17 deletions pkg/statistics/handle/storage/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func statsMetaCountAndModifyCount(
}

// HistMetaFromStorageWithHighPriority reads the meta info of the histogram from the storage.
func HistMetaFromStorageWithHighPriority(sctx sessionctx.Context, item *model.TableItemID, possibleColInfo *model.ColumnInfo) (*statistics.Histogram, *types.Datum, int64, error) {
func HistMetaFromStorageWithHighPriority(sctx sessionctx.Context, item *model.TableItemID, possibleColInfo *model.ColumnInfo) (*statistics.Histogram, int64, error) {
isIndex := 0
var tp *types.FieldType
if item.IsIndex {
Expand All @@ -93,21 +93,20 @@ func HistMetaFromStorageWithHighPriority(sctx sessionctx.Context, item *model.Ta
tp = &possibleColInfo.FieldType
}
rows, _, err := util.ExecRows(sctx,
"select high_priority distinct_count, version, null_count, tot_col_size, stats_ver, correlation, last_analyze_pos from mysql.stats_histograms where table_id = %? and hist_id = %? and is_index = %?",
"select high_priority distinct_count, version, null_count, tot_col_size, stats_ver, correlation from mysql.stats_histograms where table_id = %? and hist_id = %? and is_index = %?",
item.TableID,
item.ID,
isIndex,
)
if err != nil {
return nil, nil, 0, err
return nil, 0, err
}
if len(rows) == 0 {
return nil, nil, 0, nil
return nil, 0, nil
}
hist := statistics.NewHistogram(item.ID, rows[0].GetInt64(0), rows[0].GetInt64(2), rows[0].GetUint64(1), tp, chunk.InitialCapacity, rows[0].GetInt64(3))
hist.Correlation = rows[0].GetFloat64(5)
lastPos := rows[0].GetDatum(6, types.NewFieldType(mysql.TypeBlob))
return hist, &lastPos, rows[0].GetInt64(4), nil
return hist, rows[0].GetInt64(4), nil
}

// HistogramFromStorageWithPriority wraps the HistogramFromStorage with the given kv.Priority.
Expand Down Expand Up @@ -305,7 +304,6 @@ func indexStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statis
nullCount := row.GetInt64(5)
statsVer := row.GetInt64(7)
idx := table.GetIdx(histID)
lastAnalyzePos := row.GetDatum(9, types.NewFieldType(mysql.TypeBlob))

for _, idxInfo := range tableInfo.Indices {
if histID != idxInfo.ID {
Expand Down Expand Up @@ -341,7 +339,6 @@ func indexStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statis
if idx.IsAnalyzed() {
idx.StatsLoadedStatus = statistics.NewStatsAllEvictedStatus()
}
lastAnalyzePos.Copy(&idx.LastAnalyzePos)
break
}
if idx == nil || idx.LastUpdateVersion < histVer || loadAll {
Expand Down Expand Up @@ -374,7 +371,6 @@ func indexStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statis
if statsVer != statistics.Version0 {
idx.StatsLoadedStatus = statistics.NewStatsFullLoadStatus()
}
lastAnalyzePos.Copy(&idx.LastAnalyzePos)
}
break
}
Expand All @@ -397,7 +393,6 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
totColSize := row.GetInt64(6)
statsVer := row.GetInt64(7)
correlation := row.GetFloat64(8)
lastAnalyzePos := row.GetDatum(9, types.NewFieldType(mysql.TypeBlob))
col := table.GetCol(histID)

for _, colInfo := range tableInfo.Columns {
Expand Down Expand Up @@ -447,7 +442,6 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
if col.StatsAvailable() {
col.StatsLoadedStatus = statistics.NewStatsAllEvictedStatus()
}
lastAnalyzePos.Copy(&col.LastAnalyzePos)
col.Histogram.Correlation = correlation
break
}
Expand Down Expand Up @@ -482,7 +476,6 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
if col.StatsAvailable() {
col.StatsLoadedStatus = statistics.NewStatsFullLoadStatus()
}
lastAnalyzePos.Copy(&col.LastAnalyzePos)
break
}
if col.TotColSize != totColSize {
Expand Down Expand Up @@ -532,7 +525,7 @@ func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *
table.ModifyCount = modidyCount
table.RealtimeCount = realtimeCount

rows, _, err := util.ExecRows(sctx, "select table_id, is_index, hist_id, distinct_count, version, null_count, tot_col_size, stats_ver, correlation, last_analyze_pos from mysql.stats_histograms where table_id = %?", tableID)
rows, _, err := util.ExecRows(sctx, "select table_id, is_index, hist_id, distinct_count, version, null_count, tot_col_size, stats_ver, correlation from mysql.stats_histograms where table_id = %?", tableID)
if err != nil {
return nil, err
}
Expand All @@ -559,7 +552,7 @@ func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *

// LoadHistogram will load histogram from storage.
func LoadHistogram(sctx sessionctx.Context, tableID int64, isIndex int, histID int64, tableInfo *model.TableInfo) (*statistics.Histogram, error) {
row, _, err := util.ExecRows(sctx, "select distinct_count, version, null_count, tot_col_size, stats_ver, flag, correlation, last_analyze_pos from mysql.stats_histograms where table_id = %? and is_index = %? and hist_id = %?", tableID, isIndex, histID)
row, _, err := util.ExecRows(sctx, "select distinct_count, version, null_count, tot_col_size, stats_ver, flag, correlation from mysql.stats_histograms where table_id = %? and is_index = %? and hist_id = %?", tableID, isIndex, histID)
if err != nil || len(row) == 0 {
return nil, err
}
Expand Down Expand Up @@ -664,7 +657,7 @@ func loadNeededColumnHistograms(sctx sessionctx.Context, statsHandle statstypes.
return nil
}

hg, _, statsVer, err := HistMetaFromStorageWithHighPriority(sctx, &col, colInfo)
hg, statsVer, err := HistMetaFromStorageWithHighPriority(sctx, &col, colInfo)
if hg == nil || err != nil {
asyncload.AsyncLoadHistogramNeededItems.Delete(col)
return err
Expand Down Expand Up @@ -743,7 +736,7 @@ func loadNeededIndexHistograms(sctx sessionctx.Context, is infoschema.InfoSchema
asyncload.AsyncLoadHistogramNeededItems.Delete(idx)
return nil
}
hgMeta, lastAnalyzePos, statsVer, err := HistMetaFromStorageWithHighPriority(sctx, &idx, nil)
hgMeta, statsVer, err := HistMetaFromStorageWithHighPriority(sctx, &idx, nil)
if hgMeta == nil || err != nil {
asyncload.AsyncLoadHistogramNeededItems.Delete(idx)
return err
Expand Down Expand Up @@ -772,7 +765,6 @@ func loadNeededIndexHistograms(sctx sessionctx.Context, is infoschema.InfoSchema
Info: idxInfo, StatsVer: statsVer,
PhysicalID: idx.TableID,
StatsLoadedStatus: statistics.NewStatsFullLoadStatus()}
lastAnalyzePos.Copy(&idxHist.LastAnalyzePos)

tbl, ok = statsHandle.Get(idx.TableID)
if !ok {
Expand Down
21 changes: 3 additions & 18 deletions pkg/statistics/handle/storage/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func saveTopNToStorage(sctx sessionctx.Context, tableID int64, isIndex int, hist
return nil
}

func saveBucketsToStorage(sctx sessionctx.Context, tableID int64, isIndex int, hg *statistics.Histogram) (lastAnalyzePos []byte, err error) {
func saveBucketsToStorage(sctx sessionctx.Context, tableID int64, isIndex int, hg *statistics.Histogram) (err error) {
if hg == nil {
return
}
Expand All @@ -97,9 +97,6 @@ func saveBucketsToStorage(sctx sessionctx.Context, tableID int64, isIndex int, h
if err != nil {
return
}
if j == len(hg.Buckets)-1 {
lastAnalyzePos = upperBound.GetBytes()
}
var lowerBound types.Datum
lowerBound, err = hg.GetLower(j).ConvertTo(sc.TypeCtx(), types.NewFieldType(mysql.TypeBlob))
if err != nil {
Expand Down Expand Up @@ -281,16 +278,10 @@ func SaveTableStatsToStorage(sctx sessionctx.Context,
if _, err = util.Exec(sctx, "delete from mysql.stats_buckets where table_id = %? and is_index = %? and hist_id = %?", tableID, result.IsIndex, hg.ID); err != nil {
return 0, err
}
var lastAnalyzePos []byte
lastAnalyzePos, err = saveBucketsToStorage(sctx, tableID, result.IsIndex, hg)
err = saveBucketsToStorage(sctx, tableID, result.IsIndex, hg)
if err != nil {
return 0, err
}
if len(lastAnalyzePos) > 0 {
if _, err = util.Exec(sctx, "update mysql.stats_histograms set last_analyze_pos = %? where table_id = %? and is_index = %? and hist_id = %?", lastAnalyzePos, tableID, result.IsIndex, hg.ID); err != nil {
return 0, err
}
}
if result.IsIndex == 0 {
if _, err = util.Exec(sctx, "insert into mysql.column_stats_usage (table_id, column_id, last_analyzed_at) values(%?, %?, current_timestamp()) on duplicate key update last_analyzed_at = values(last_analyzed_at)", tableID, hg.ID); err != nil {
return 0, err
Expand Down Expand Up @@ -376,16 +367,10 @@ func SaveStatsToStorage(
if _, err = util.Exec(sctx, "delete from mysql.stats_buckets where table_id = %? and is_index = %? and hist_id = %?", tableID, isIndex, hg.ID); err != nil {
return 0, err
}
var lastAnalyzePos []byte
lastAnalyzePos, err = saveBucketsToStorage(sctx, tableID, isIndex, hg)
err = saveBucketsToStorage(sctx, tableID, isIndex, hg)
if err != nil {
return 0, err
}
if len(lastAnalyzePos) > 0 {
if _, err = util.Exec(sctx, "update mysql.stats_histograms set last_analyze_pos = %? where table_id = %? and is_index = %? and hist_id = %?", lastAnalyzePos, tableID, isIndex, hg.ID); err != nil {
return 0, err
}
}
if updateAnalyzeTime && isIndex == 0 {
if _, err = util.Exec(sctx, "insert into mysql.column_stats_usage (table_id, column_id, last_analyzed_at) values(%?, %?, current_timestamp()) on duplicate key update last_analyzed_at = current_timestamp()", tableID, hg.ID); err != nil {
return 0, err
Expand Down
3 changes: 1 addition & 2 deletions pkg/statistics/handle/syncload/stats_syncload.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (*statsSyncLoad) readStatsForOneItem(sctx sessionctx.Context, item model.Ta
var hg *statistics.Histogram
var err error
isIndexFlag := int64(0)
hg, lastAnalyzePos, statsVer, err := storage.HistMetaFromStorageWithHighPriority(sctx, &item, w.colInfo)
hg, statsVer, err := storage.HistMetaFromStorageWithHighPriority(sctx, &item, w.colInfo)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -460,7 +460,6 @@ func (*statsSyncLoad) readStatsForOneItem(sctx sessionctx.Context, item model.Ta
idxHist.StatsLoadedStatus = statistics.NewStatsAllEvictedStatus()
}
}
lastAnalyzePos.Copy(&idxHist.LastAnalyzePos)
w.idx = idxHist
} else {
colHist := &statistics.Column{
Expand Down
10 changes: 4 additions & 6 deletions pkg/statistics/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ import (

// Index represents an index histogram.
type Index struct {
LastAnalyzePos types.Datum
CMSketch *CMSketch
TopN *TopN
FMSketch *FMSketch
Info *model.IndexInfo
CMSketch *CMSketch
TopN *TopN
FMSketch *FMSketch
Info *model.IndexInfo
Histogram
StatsLoadedStatus
StatsVer int64 // StatsVer is the version of the current stats, used to maintain compatibility
Expand All @@ -50,7 +49,6 @@ func (idx *Index) Copy() *Index {
PhysicalID: idx.PhysicalID,
StatsVer: idx.StatsVer,
}
idx.LastAnalyzePos.Copy(&nc.LastAnalyzePos)
if idx.CMSketch != nil {
nc.CMSketch = idx.CMSketch.Copy()
}
Expand Down

0 comments on commit c44672e

Please sign in to comment.