Skip to content

Commit c44672e

Browse files
authored
statistics: remove unused LastAnalyzePos field (#58859)
ref #55043
1 parent 6d2a05e commit c44672e

File tree

7 files changed

+22
-56
lines changed

7 files changed

+22
-56
lines changed

pkg/statistics/column.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ import (
2626

2727
// Column represents a column histogram.
2828
type Column struct {
29-
LastAnalyzePos types.Datum
30-
CMSketch *CMSketch
31-
TopN *TopN
32-
FMSketch *FMSketch
33-
Info *model.ColumnInfo
29+
CMSketch *CMSketch
30+
TopN *TopN
31+
FMSketch *FMSketch
32+
Info *model.ColumnInfo
3433
Histogram
3534

3635
// StatsLoadedStatus indicates the status of column statistics
@@ -54,7 +53,6 @@ func (c *Column) Copy() *Column {
5453
StatsVer: c.StatsVer,
5554
IsHandle: c.IsHandle,
5655
}
57-
c.LastAnalyzePos.Copy(&nc.LastAnalyzePos)
5856
if c.CMSketch != nil {
5957
nc.CMSketch = c.CMSketch.Copy()
6058
}

pkg/statistics/handle/bootstrap.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
190190
table.StatsVer = int(statsVer)
191191
}
192192
id, ndv, nullCount, version, totColSize := row.GetInt64(2), row.GetInt64(3), row.GetInt64(5), row.GetUint64(4), row.GetInt64(7)
193-
lastAnalyzePos := row.GetDatum(10, types.NewFieldType(mysql.TypeBlob))
194193
tbl, ok := h.TableInfoByID(is, table.PhysicalID)
195194
if !ok {
196195
// this table has been dropped. but stats meta still exists and wait for being deleted.
@@ -235,7 +234,6 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
235234
// The LastAnalyzeVersion is added by ALTER table so its value might be 0.
236235
table.LastAnalyzeVersion = max(table.LastAnalyzeVersion, version)
237236
}
238-
lastAnalyzePos.Copy(&index.LastAnalyzePos)
239237
table.SetIdx(idxInfo.ID, index)
240238
table.ColAndIdxExistenceMap.InsertIndex(idxInfo.ID, statsVer != statistics.Version0)
241239
} else {
@@ -258,7 +256,6 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
258256
IsHandle: tbl.Meta().PKIsHandle && mysql.HasPriKeyFlag(colInfo.GetFlag()),
259257
StatsVer: statsVer,
260258
}
261-
lastAnalyzePos.Copy(&col.LastAnalyzePos)
262259
table.SetCol(hist.ID, col)
263260
table.ColAndIdxExistenceMap.InsertCol(colInfo.ID, statsVer != statistics.Version0 || ndv > 0 || nullCount > 0)
264261
if statsVer != statistics.Version0 {
@@ -283,7 +280,7 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
283280
// genInitStatsHistogramsSQL generates the SQL to load all stats_histograms records.
284281
// We need to read all the records since we need to do initialization of table.ColAndIdxExistenceMap.
285282
func genInitStatsHistogramsSQL(isPaging bool) string {
286-
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"
283+
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"
287284
orderSuffix := " order by table_id"
288285
if !isPaging {
289286
return selectPrefix + orderSuffix

pkg/statistics/handle/handletest/statstest/stats_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ func TestInitStats(t *testing.T) {
301301
h.Clear()
302302
require.NoError(t, h.InitStats(context.Background(), is))
303303
table0 := h.GetTableStats(tbl.Meta())
304-
require.Equal(t, uint8(0x3), table0.GetIdx(1).LastAnalyzePos.GetBytes()[0])
305304
h.Clear()
306305
require.NoError(t, h.Update(context.Background(), is))
307306
// Index and pk are loaded.
@@ -414,8 +413,6 @@ func initStatsVer2(t *testing.T) {
414413
require.True(t, !table0.GetCol(4).IsStatsInitialized())
415414
require.True(t, table0.GetCol(5).IsStatsInitialized())
416415
require.Equal(t, 2, table0.IdxNum())
417-
require.Equal(t, uint8(0x3), table0.GetIdx(1).LastAnalyzePos.GetBytes()[0])
418-
require.Equal(t, uint8(0x3), table0.GetIdx(2).LastAnalyzePos.GetBytes()[0])
419416
h.Clear()
420417
require.NoError(t, h.InitStats(context.Background(), is))
421418
table1 := h.GetTableStats(tbl.Meta())

pkg/statistics/handle/storage/read.go

+9-17
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func statsMetaCountAndModifyCount(
8383
}
8484

8585
// HistMetaFromStorageWithHighPriority reads the meta info of the histogram from the storage.
86-
func HistMetaFromStorageWithHighPriority(sctx sessionctx.Context, item *model.TableItemID, possibleColInfo *model.ColumnInfo) (*statistics.Histogram, *types.Datum, int64, error) {
86+
func HistMetaFromStorageWithHighPriority(sctx sessionctx.Context, item *model.TableItemID, possibleColInfo *model.ColumnInfo) (*statistics.Histogram, int64, error) {
8787
isIndex := 0
8888
var tp *types.FieldType
8989
if item.IsIndex {
@@ -93,21 +93,20 @@ func HistMetaFromStorageWithHighPriority(sctx sessionctx.Context, item *model.Ta
9393
tp = &possibleColInfo.FieldType
9494
}
9595
rows, _, err := util.ExecRows(sctx,
96-
"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 = %?",
96+
"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 = %?",
9797
item.TableID,
9898
item.ID,
9999
isIndex,
100100
)
101101
if err != nil {
102-
return nil, nil, 0, err
102+
return nil, 0, err
103103
}
104104
if len(rows) == 0 {
105-
return nil, nil, 0, nil
105+
return nil, 0, nil
106106
}
107107
hist := statistics.NewHistogram(item.ID, rows[0].GetInt64(0), rows[0].GetInt64(2), rows[0].GetUint64(1), tp, chunk.InitialCapacity, rows[0].GetInt64(3))
108108
hist.Correlation = rows[0].GetFloat64(5)
109-
lastPos := rows[0].GetDatum(6, types.NewFieldType(mysql.TypeBlob))
110-
return hist, &lastPos, rows[0].GetInt64(4), nil
109+
return hist, rows[0].GetInt64(4), nil
111110
}
112111

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

310308
for _, idxInfo := range tableInfo.Indices {
311309
if histID != idxInfo.ID {
@@ -341,7 +339,6 @@ func indexStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statis
341339
if idx.IsAnalyzed() {
342340
idx.StatsLoadedStatus = statistics.NewStatsAllEvictedStatus()
343341
}
344-
lastAnalyzePos.Copy(&idx.LastAnalyzePos)
345342
break
346343
}
347344
if idx == nil || idx.LastUpdateVersion < histVer || loadAll {
@@ -374,7 +371,6 @@ func indexStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *statis
374371
if statsVer != statistics.Version0 {
375372
idx.StatsLoadedStatus = statistics.NewStatsFullLoadStatus()
376373
}
377-
lastAnalyzePos.Copy(&idx.LastAnalyzePos)
378374
}
379375
break
380376
}
@@ -397,7 +393,6 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
397393
totColSize := row.GetInt64(6)
398394
statsVer := row.GetInt64(7)
399395
correlation := row.GetFloat64(8)
400-
lastAnalyzePos := row.GetDatum(9, types.NewFieldType(mysql.TypeBlob))
401396
col := table.GetCol(histID)
402397

403398
for _, colInfo := range tableInfo.Columns {
@@ -447,7 +442,6 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
447442
if col.StatsAvailable() {
448443
col.StatsLoadedStatus = statistics.NewStatsAllEvictedStatus()
449444
}
450-
lastAnalyzePos.Copy(&col.LastAnalyzePos)
451445
col.Histogram.Correlation = correlation
452446
break
453447
}
@@ -482,7 +476,6 @@ func columnStatsFromStorage(sctx sessionctx.Context, row chunk.Row, table *stati
482476
if col.StatsAvailable() {
483477
col.StatsLoadedStatus = statistics.NewStatsFullLoadStatus()
484478
}
485-
lastAnalyzePos.Copy(&col.LastAnalyzePos)
486479
break
487480
}
488481
if col.TotColSize != totColSize {
@@ -532,7 +525,7 @@ func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *
532525
table.ModifyCount = modidyCount
533526
table.RealtimeCount = realtimeCount
534527

535-
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)
528+
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)
536529
if err != nil {
537530
return nil, err
538531
}
@@ -559,7 +552,7 @@ func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *
559552

560553
// LoadHistogram will load histogram from storage.
561554
func LoadHistogram(sctx sessionctx.Context, tableID int64, isIndex int, histID int64, tableInfo *model.TableInfo) (*statistics.Histogram, error) {
562-
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)
555+
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)
563556
if err != nil || len(row) == 0 {
564557
return nil, err
565558
}
@@ -664,7 +657,7 @@ func loadNeededColumnHistograms(sctx sessionctx.Context, statsHandle statstypes.
664657
return nil
665658
}
666659

667-
hg, _, statsVer, err := HistMetaFromStorageWithHighPriority(sctx, &col, colInfo)
660+
hg, statsVer, err := HistMetaFromStorageWithHighPriority(sctx, &col, colInfo)
668661
if hg == nil || err != nil {
669662
asyncload.AsyncLoadHistogramNeededItems.Delete(col)
670663
return err
@@ -743,7 +736,7 @@ func loadNeededIndexHistograms(sctx sessionctx.Context, is infoschema.InfoSchema
743736
asyncload.AsyncLoadHistogramNeededItems.Delete(idx)
744737
return nil
745738
}
746-
hgMeta, lastAnalyzePos, statsVer, err := HistMetaFromStorageWithHighPriority(sctx, &idx, nil)
739+
hgMeta, statsVer, err := HistMetaFromStorageWithHighPriority(sctx, &idx, nil)
747740
if hgMeta == nil || err != nil {
748741
asyncload.AsyncLoadHistogramNeededItems.Delete(idx)
749742
return err
@@ -772,7 +765,6 @@ func loadNeededIndexHistograms(sctx sessionctx.Context, is infoschema.InfoSchema
772765
Info: idxInfo, StatsVer: statsVer,
773766
PhysicalID: idx.TableID,
774767
StatsLoadedStatus: statistics.NewStatsFullLoadStatus()}
775-
lastAnalyzePos.Copy(&idxHist.LastAnalyzePos)
776768

777769
tbl, ok = statsHandle.Get(idx.TableID)
778770
if !ok {

pkg/statistics/handle/storage/save.go

+3-18
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func saveTopNToStorage(sctx sessionctx.Context, tableID int64, isIndex int, hist
7474
return nil
7575
}
7676

77-
func saveBucketsToStorage(sctx sessionctx.Context, tableID int64, isIndex int, hg *statistics.Histogram) (lastAnalyzePos []byte, err error) {
77+
func saveBucketsToStorage(sctx sessionctx.Context, tableID int64, isIndex int, hg *statistics.Histogram) (err error) {
7878
if hg == nil {
7979
return
8080
}
@@ -97,9 +97,6 @@ func saveBucketsToStorage(sctx sessionctx.Context, tableID int64, isIndex int, h
9797
if err != nil {
9898
return
9999
}
100-
if j == len(hg.Buckets)-1 {
101-
lastAnalyzePos = upperBound.GetBytes()
102-
}
103100
var lowerBound types.Datum
104101
lowerBound, err = hg.GetLower(j).ConvertTo(sc.TypeCtx(), types.NewFieldType(mysql.TypeBlob))
105102
if err != nil {
@@ -281,16 +278,10 @@ func SaveTableStatsToStorage(sctx sessionctx.Context,
281278
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 {
282279
return 0, err
283280
}
284-
var lastAnalyzePos []byte
285-
lastAnalyzePos, err = saveBucketsToStorage(sctx, tableID, result.IsIndex, hg)
281+
err = saveBucketsToStorage(sctx, tableID, result.IsIndex, hg)
286282
if err != nil {
287283
return 0, err
288284
}
289-
if len(lastAnalyzePos) > 0 {
290-
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 {
291-
return 0, err
292-
}
293-
}
294285
if result.IsIndex == 0 {
295286
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 {
296287
return 0, err
@@ -376,16 +367,10 @@ func SaveStatsToStorage(
376367
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 {
377368
return 0, err
378369
}
379-
var lastAnalyzePos []byte
380-
lastAnalyzePos, err = saveBucketsToStorage(sctx, tableID, isIndex, hg)
370+
err = saveBucketsToStorage(sctx, tableID, isIndex, hg)
381371
if err != nil {
382372
return 0, err
383373
}
384-
if len(lastAnalyzePos) > 0 {
385-
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 {
386-
return 0, err
387-
}
388-
}
389374
if updateAnalyzeTime && isIndex == 0 {
390375
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 {
391376
return 0, err

pkg/statistics/handle/syncload/stats_syncload.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ func (*statsSyncLoad) readStatsForOneItem(sctx sessionctx.Context, item model.Ta
404404
var hg *statistics.Histogram
405405
var err error
406406
isIndexFlag := int64(0)
407-
hg, lastAnalyzePos, statsVer, err := storage.HistMetaFromStorageWithHighPriority(sctx, &item, w.colInfo)
407+
hg, statsVer, err := storage.HistMetaFromStorageWithHighPriority(sctx, &item, w.colInfo)
408408
if err != nil {
409409
return nil, err
410410
}
@@ -460,7 +460,6 @@ func (*statsSyncLoad) readStatsForOneItem(sctx sessionctx.Context, item model.Ta
460460
idxHist.StatsLoadedStatus = statistics.NewStatsAllEvictedStatus()
461461
}
462462
}
463-
lastAnalyzePos.Copy(&idxHist.LastAnalyzePos)
464463
w.idx = idxHist
465464
} else {
466465
colHist := &statistics.Column{

pkg/statistics/index.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ import (
2727

2828
// Index represents an index histogram.
2929
type Index struct {
30-
LastAnalyzePos types.Datum
31-
CMSketch *CMSketch
32-
TopN *TopN
33-
FMSketch *FMSketch
34-
Info *model.IndexInfo
30+
CMSketch *CMSketch
31+
TopN *TopN
32+
FMSketch *FMSketch
33+
Info *model.IndexInfo
3534
Histogram
3635
StatsLoadedStatus
3736
StatsVer int64 // StatsVer is the version of the current stats, used to maintain compatibility
@@ -50,7 +49,6 @@ func (idx *Index) Copy() *Index {
5049
PhysicalID: idx.PhysicalID,
5150
StatsVer: idx.StatsVer,
5251
}
53-
idx.LastAnalyzePos.Copy(&nc.LastAnalyzePos)
5452
if idx.CMSketch != nil {
5553
nc.CMSketch = idx.CMSketch.Copy()
5654
}

0 commit comments

Comments
 (0)