Skip to content

Commit 7578037

Browse files
authored
statistics: do not load unnecessary index statistics (#54060) (#54087)
close #54022
1 parent 89a45a6 commit 7578037

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

statistics/handle/handle.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,8 +1089,8 @@ func (h *Handle) loadNeededColumnHistograms(reader *statistics.StatsReader, col
10891089
return errors.Trace(err)
10901090
}
10911091
if len(rows) == 0 {
1092-
logutil.BgLogger().Error("fail to get stats version for this histogram", zap.Int64("table_id", col.TableID), zap.Int64("hist_id", col.ID))
1093-
return errors.Trace(fmt.Errorf("fail to get stats version for this histogram, table_id:%v, hist_id:%v", col.TableID, col.ID))
1092+
logutil.BgLogger().Error("fail to get stats version for this histogram, normally this wouldn't happen, please check if this column or index has a histogram record in `mysql.stats_histogram`", zap.Int64("table_id", col.TableID), zap.Int64("column_id", col.ID))
1093+
return errors.Trace(fmt.Errorf("fail to get stats version for this histogram, normally this wouldn't happen, please check if this column or index has a histogram record in `mysql.stats_histogram`, table_id:%v, column_id:%v", col.TableID, col.ID))
10941094
}
10951095
statsVer := rows[0].GetInt64(0)
10961096
colHist := &statistics.Column{
@@ -1128,7 +1128,16 @@ func (h *Handle) loadNeededIndexHistograms(reader *statistics.StatsReader, idx m
11281128
return nil
11291129
}
11301130
index, ok := tbl.Indices[idx.ID]
1131-
if !ok {
1131+
// Double check if the index is really needed to load.
1132+
// If we don't do this it might cause a memory leak.
1133+
// See: https://github.com/pingcap/tidb/issues/54022
1134+
if !ok || !index.IsLoadNeeded() {
1135+
if !index.IsLoadNeeded() {
1136+
logutil.BgLogger().Warn(
1137+
"Although the index stats is not required to load, an attempt is still made to load it, skip it",
1138+
zap.Int64("table_id", idx.TableID), zap.Int64("hist_id", idx.ID),
1139+
)
1140+
}
11321141
statistics.HistogramNeededItems.Delete(idx)
11331142
return nil
11341143
}
@@ -1152,8 +1161,8 @@ func (h *Handle) loadNeededIndexHistograms(reader *statistics.StatsReader, idx m
11521161
return errors.Trace(err)
11531162
}
11541163
if len(rows) == 0 {
1155-
logutil.BgLogger().Error("fail to get stats version for this histogram", zap.Int64("table_id", idx.TableID), zap.Int64("hist_id", idx.ID))
1156-
return errors.Trace(fmt.Errorf("fail to get stats version for this histogram, table_id:%v, hist_id:%v", idx.TableID, idx.ID))
1164+
logutil.BgLogger().Error("fail to get stats version for this histogram, normally this wouldn't happen, please check if this column or index has a histogram record in `mysql.stats_histogram`", zap.Int64("table_id", idx.TableID), zap.Int64("index_id", idx.ID))
1165+
return errors.Trace(fmt.Errorf("fail to get stats version for this histogram, normally this wouldn't happen, please check if this column or index has a histogram record in `mysql.stats_histogram`, table_id:%v, index_id:%v", idx.TableID, idx.ID))
11571166
}
11581167
idxHist := &statistics.Index{Histogram: *hg, CMSketch: cms, TopN: topN, FMSketch: fms,
11591168
Info: index.Info, ErrorRate: index.ErrorRate, StatsVer: rows[0].GetInt64(0),

statistics/handle/handle_hist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ func (h *Handle) readStatsForOneItem(item model.TableItemID, w *statsWrapper, re
405405
return nil, errors.Trace(err)
406406
}
407407
if len(rows) == 0 {
408-
logutil.BgLogger().Error("fail to get stats version for this histogram", zap.Int64("table_id", item.TableID),
408+
logutil.BgLogger().Error("fail to get stats version for this histogram, normally this wouldn't happen, please check if this column or index has a histogram record in `mysql.stats_histogram`", zap.Int64("table_id", item.TableID),
409409
zap.Int64("hist_id", item.ID), zap.Bool("is_index", item.IsIndex))
410-
return nil, errors.Trace(fmt.Errorf("fail to get stats version for this histogram, table_id:%v, hist_id:%v, is_index:%v", item.TableID, item.ID, item.IsIndex))
410+
return nil, errors.Trace(fmt.Errorf("fail to get stats version for this histogram, normally this wouldn't happen, please check if this column or index has a histogram record in `mysql.stats_histogram`, table_id:%v, hist_id:%v, is_index:%v", item.TableID, item.ID, item.IsIndex))
411411
}
412412
statsVer := rows[0].GetInt64(0)
413413
if item.IsIndex {

0 commit comments

Comments
 (0)