Skip to content

Commit

Permalink
statistics: remove unused HavePhysicalID field from HistColl struct (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustin170506 authored Jan 3, 2025
1 parent 0662241 commit 35f329d
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pkg/planner/cardinality/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func mockStatsHistogram(id int64, values []types.Datum, repeat int64, tp *types.
}

func mockStatsTable(tbl *model.TableInfo, rowCount int64) *statistics.Table {
histColl := *statistics.NewHistColl(tbl.ID, true, rowCount, 0, 0, 0)
histColl := *statistics.NewHistColl(tbl.ID, rowCount, 0, 0, 0)
statsTbl := &statistics.Table{
HistColl: histColl,
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/statistics/handle/autoanalyze/autoanalyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,21 @@ func TestNeedAnalyzeTable(t *testing.T) {
},
// table was already analyzed but auto analyze is disabled
{
tbl: &statistics.Table{HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, 1, 1, columns, nil), LastAnalyzeVersion: 1},
tbl: &statistics.Table{HistColl: *statistics.NewHistCollWithColsAndIdxs(0, 1, 1, columns, nil), LastAnalyzeVersion: 1},
ratio: 0,
result: false,
reason: "",
},
// table was already analyzed but modify count is small
{
tbl: &statistics.Table{HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, 1, 0, columns, nil), LastAnalyzeVersion: 1},
tbl: &statistics.Table{HistColl: *statistics.NewHistCollWithColsAndIdxs(0, 1, 0, columns, nil), LastAnalyzeVersion: 1},
ratio: 0.3,
result: false,
reason: "",
},
// table was already analyzed
{
tbl: &statistics.Table{HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, 1, 1, columns, nil), LastAnalyzeVersion: 1},
tbl: &statistics.Table{HistColl: *statistics.NewHistCollWithColsAndIdxs(0, 1, 1, columns, nil), LastAnalyzeVersion: 1},
ratio: 0.3,
result: true,
reason: "too many modifications",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCalculateChangePercentage(t *testing.T) {
{
name: "Unanalyzed table",
tblStats: &statistics.Table{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, statistics.AutoAnalyzeMinCnt+1, 0, nil, nil),
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, statistics.AutoAnalyzeMinCnt+1, 0, nil, nil),
ColAndIdxExistenceMap: statistics.NewColAndIndexExistenceMap(0, 0),
},
autoAnalyzeRatio: 0.5,
Expand All @@ -46,7 +46,7 @@ func TestCalculateChangePercentage(t *testing.T) {
{
name: "Analyzed table with change percentage above threshold",
tblStats: &statistics.Table{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, 100, 60, nil, nil),
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, 100, 60, nil, nil),
ColAndIdxExistenceMap: statistics.NewColAndIndexExistenceMap(1, 1),
LastAnalyzeVersion: 1,
},
Expand All @@ -56,7 +56,7 @@ func TestCalculateChangePercentage(t *testing.T) {
{
name: "Analyzed table with change percentage below threshold",
tblStats: &statistics.Table{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, 100, 40, nil, nil),
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, 100, 40, nil, nil),
ColAndIdxExistenceMap: statistics.NewColAndIndexExistenceMap(1, 1),
LastAnalyzeVersion: 1,
},
Expand All @@ -66,7 +66,7 @@ func TestCalculateChangePercentage(t *testing.T) {
{
name: "Auto analyze ratio set to 0",
tblStats: &statistics.Table{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, 100, 60, nil, nil),
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, 100, 60, nil, nil),
ColAndIdxExistenceMap: statistics.NewColAndIndexExistenceMap(1, 1),
LastAnalyzeVersion: 1,
},
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestCheckIndexesNeedAnalyze(t *testing.T) {
},
},
tblStats: &statistics.Table{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, 0, 0, map[int64]*statistics.Column{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, 0, 0, map[int64]*statistics.Column{
1: {
StatsVer: 2,
},
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestCalculateIndicatorsForPartitions(t *testing.T) {
},
partitionStats: map[priorityqueue.PartitionIDAndName]*statistics.Table{
priorityqueue.NewPartitionIDAndName("p0", 1): {
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, statistics.AutoAnalyzeMinCnt+1, (statistics.AutoAnalyzeMinCnt+1)*2, map[int64]*statistics.Column{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, statistics.AutoAnalyzeMinCnt+1, (statistics.AutoAnalyzeMinCnt+1)*2, map[int64]*statistics.Column{
1: {
StatsVer: 2,
Histogram: statistics.Histogram{
Expand All @@ -269,7 +269,7 @@ func TestCalculateIndicatorsForPartitions(t *testing.T) {
LastAnalyzeVersion: lastUpdateTs,
},
priorityqueue.NewPartitionIDAndName("p1", 2): {
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, statistics.AutoAnalyzeMinCnt+1, 0, map[int64]*statistics.Column{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, statistics.AutoAnalyzeMinCnt+1, 0, map[int64]*statistics.Column{
1: {
StatsVer: 2,
Histogram: statistics.Histogram{
Expand Down Expand Up @@ -312,7 +312,7 @@ func TestCalculateIndicatorsForPartitions(t *testing.T) {
},
partitionStats: map[priorityqueue.PartitionIDAndName]*statistics.Table{
priorityqueue.NewPartitionIDAndName("p0", 1): {
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, statistics.AutoAnalyzeMinCnt+1, 0, map[int64]*statistics.Column{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, statistics.AutoAnalyzeMinCnt+1, 0, map[int64]*statistics.Column{
1: {
StatsVer: 2,
Histogram: statistics.Histogram{
Expand All @@ -331,7 +331,7 @@ func TestCalculateIndicatorsForPartitions(t *testing.T) {
LastAnalyzeVersion: lastUpdateTs,
},
priorityqueue.NewPartitionIDAndName("p1", 2): {
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, statistics.AutoAnalyzeMinCnt+1, 0, map[int64]*statistics.Column{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, statistics.AutoAnalyzeMinCnt+1, 0, map[int64]*statistics.Column{
1: {
StatsVer: 2,
Histogram: statistics.Histogram{
Expand Down Expand Up @@ -418,11 +418,11 @@ func TestCheckNewlyAddedIndexesNeedAnalyzeForPartitionedTable(t *testing.T) {
}
partitionStats := map[priorityqueue.PartitionIDAndName]*statistics.Table{
priorityqueue.NewPartitionIDAndName("p0", 1): {
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, statistics.AutoAnalyzeMinCnt+1, 0, nil, map[int64]*statistics.Index{}),
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, statistics.AutoAnalyzeMinCnt+1, 0, nil, map[int64]*statistics.Index{}),
ColAndIdxExistenceMap: statistics.NewColAndIndexExistenceMap(0, 0),
},
priorityqueue.NewPartitionIDAndName("p1", 2): {
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, false, statistics.AutoAnalyzeMinCnt+1, 0, nil, map[int64]*statistics.Index{
HistColl: *statistics.NewHistCollWithColsAndIdxs(0, statistics.AutoAnalyzeMinCnt+1, 0, nil, map[int64]*statistics.Index{
2: {
StatsVer: 2,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/statistics/handle/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (*Handle) initStatsMeta4Chunk(cache statstypes.StatsCache, iter *chunk.Iter
for row := iter.Begin(); row != iter.End(); row = iter.Next() {
physicalID = row.GetInt64(1)
maxPhysicalID = max(physicalID, maxPhysicalID)
newHistColl := *statistics.NewHistColl(physicalID, true, row.GetInt64(3), row.GetInt64(2), 4, 4)
newHistColl := *statistics.NewHistColl(physicalID, row.GetInt64(3), row.GetInt64(2), 4, 4)
snapshot := row.GetUint64(4)
tbl := &statistics.Table{
HistColl: newHistColl,
Expand Down
2 changes: 1 addition & 1 deletion pkg/statistics/handle/cache/internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// NewMockStatisticsTable creates a mock statistics table with given columns and indices.
// each column and index consumes 4 bytes memory
func NewMockStatisticsTable(columns int, indices int, withCMS, withTopN, withHist bool) *statistics.Table {
t := &statistics.Table{HistColl: *statistics.NewHistColl(0, false, 0, 0, 0, 0)}
t := &statistics.Table{HistColl: *statistics.NewHistColl(0, 0, 0, 0, 0)}
for i := 1; i <= columns; i++ {
var (
cms *statistics.CMSketch
Expand Down
2 changes: 1 addition & 1 deletion pkg/statistics/handle/storage/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func GenJSONTableFromStats(

// TableStatsFromJSON loads statistic from JSONTable and return the Table of statistic.
func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *statsutil.JSONTable) (*statistics.Table, error) {
newHistColl := *statistics.NewHistColl(physicalID, true, jsonTbl.Count, jsonTbl.ModifyCount, len(jsonTbl.Columns), len(jsonTbl.Indices))
newHistColl := *statistics.NewHistColl(physicalID, jsonTbl.Count, jsonTbl.ModifyCount, len(jsonTbl.Columns), len(jsonTbl.Indices))
tbl := &statistics.Table{
HistColl: newHistColl,
ColAndIdxExistenceMap: statistics.NewColAndIndexExistenceMap(len(tableInfo.Columns), len(tableInfo.Indices)),
Expand Down
2 changes: 1 addition & 1 deletion pkg/statistics/handle/storage/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func TableStatsFromStorage(sctx sessionctx.Context, snapshot uint64, tableInfo *
// If table stats is pseudo, we also need to copy it, since we will use the column stats when
// the average error rate of it is small.
if table == nil || snapshot > 0 {
histColl := *statistics.NewHistColl(tableID, true, 0, 0, 4, 4)
histColl := *statistics.NewHistColl(tableID, 0, 0, 4, 4)
table = &statistics.Table{
HistColl: histColl,
ColAndIdxExistenceMap: statistics.NewColAndIndexExistenceMap(len(tableInfo.Columns), len(tableInfo.Indices)),
Expand Down
6 changes: 3 additions & 3 deletions pkg/statistics/statistics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func SubTestColumnRange() func(*testing.T) {
StatsLoadedStatus: NewStatsFullLoadStatus(),
}
tbl := &Table{
HistColl: *NewHistCollWithColsAndIdxs(0, false, int64(col.TotalRowCount()), 0, make(map[int64]*Column), make(map[int64]*Index)),
HistColl: *NewHistCollWithColsAndIdxs(0, int64(col.TotalRowCount()), 0, make(map[int64]*Column), make(map[int64]*Index)),
}
ran := []*ranger.Range{{
LowVal: []types.Datum{{}},
Expand Down Expand Up @@ -295,7 +295,7 @@ func SubTestIntColumnRanges() func(*testing.T) {
require.Equal(t, int64(100000), rowCount)
col := &Column{Histogram: *hg, Info: &model.ColumnInfo{}, StatsLoadedStatus: NewStatsFullLoadStatus()}
tbl := &Table{
HistColl: *NewHistCollWithColsAndIdxs(0, false, int64(col.TotalRowCount()), 0, make(map[int64]*Column), make(map[int64]*Index)),
HistColl: *NewHistCollWithColsAndIdxs(0, int64(col.TotalRowCount()), 0, make(map[int64]*Column), make(map[int64]*Index)),
}
ran := []*ranger.Range{{
LowVal: []types.Datum{types.NewIntDatum(math.MinInt64)},
Expand Down Expand Up @@ -388,7 +388,7 @@ func SubTestIndexRanges() func(*testing.T) {
idxInfo := &model.IndexInfo{Columns: []*model.IndexColumn{{Offset: 0}}}
idx := &Index{Histogram: *hg, CMSketch: cms, Info: idxInfo}
tbl := &Table{
HistColl: *NewHistCollWithColsAndIdxs(0, false, int64(idx.TotalRowCount()), 0, nil, make(map[int64]*Index)),
HistColl: *NewHistCollWithColsAndIdxs(0, int64(idx.TotalRowCount()), 0, nil, make(map[int64]*Index)),
}
ran := []*ranger.Range{{
LowVal: []types.Datum{types.MinNotNullDatum()},
Expand Down
54 changes: 22 additions & 32 deletions pkg/statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ type HistColl struct {

// The version of the statistics, refer to Version0, Version1, Version2 and so on.
StatsVer int
// HavePhysicalID is true means this HistColl is from single table and have its ID's information.
// The physical id is used when try to load column stats from storage.
HavePhysicalID bool
Pseudo bool
Pseudo bool

/*
Fields below are only used in a query, like for estimation, and they will be useless when stored in
Expand All @@ -258,12 +255,11 @@ type HistColl struct {
}

// NewHistColl creates a new HistColl.
func NewHistColl(id int64, havePhysicalID bool, realtimeCnt, modifyCnt int64, colNum, idxNum int) *HistColl {
func NewHistColl(id int64, realtimeCnt, modifyCnt int64, colNum, idxNum int) *HistColl {
return &HistColl{
columns: make(map[int64]*Column, colNum),
indices: make(map[int64]*Index, idxNum),
PhysicalID: id,
HavePhysicalID: havePhysicalID,
RealtimeCount: realtimeCnt,
ModifyCount: modifyCnt,
Idx2ColUniqueIDs: make(map[int64][]int64),
Expand All @@ -274,12 +270,11 @@ func NewHistColl(id int64, havePhysicalID bool, realtimeCnt, modifyCnt int64, co
}

// NewHistCollWithColsAndIdxs creates a new HistColl with given columns and indices.
func NewHistCollWithColsAndIdxs(id int64, havePhysicalID bool, realtimeCnt, modifyCnt int64, cols map[int64]*Column, idxs map[int64]*Index) *HistColl {
func NewHistCollWithColsAndIdxs(id int64, realtimeCnt, modifyCnt int64, cols map[int64]*Column, idxs map[int64]*Index) *HistColl {
return &HistColl{
columns: cols,
indices: idxs,
PhysicalID: id,
HavePhysicalID: havePhysicalID,
RealtimeCount: realtimeCnt,
ModifyCount: modifyCnt,
Idx2ColUniqueIDs: make(map[int64][]int64),
Expand Down Expand Up @@ -591,14 +586,13 @@ func (t *Table) MemoryUsage() *TableMemoryUsage {
// Copy copies the current table.
func (t *Table) Copy() *Table {
newHistColl := HistColl{
PhysicalID: t.PhysicalID,
HavePhysicalID: t.HavePhysicalID,
RealtimeCount: t.RealtimeCount,
columns: make(map[int64]*Column, len(t.columns)),
indices: make(map[int64]*Index, len(t.indices)),
Pseudo: t.Pseudo,
ModifyCount: t.ModifyCount,
StatsVer: t.StatsVer,
PhysicalID: t.PhysicalID,
RealtimeCount: t.RealtimeCount,
columns: make(map[int64]*Column, len(t.columns)),
indices: make(map[int64]*Index, len(t.indices)),
Pseudo: t.Pseudo,
ModifyCount: t.ModifyCount,
StatsVer: t.StatsVer,
}
for id, col := range t.columns {
newHistColl.columns[id] = col.Copy()
Expand Down Expand Up @@ -633,14 +627,13 @@ func (t *Table) Copy() *Table {
// The internal containers, like t.Columns and t.Indices, and the stats, like TopN and Histogram are not copied.
func (t *Table) ShallowCopy() *Table {
newHistColl := HistColl{
PhysicalID: t.PhysicalID,
HavePhysicalID: t.HavePhysicalID,
RealtimeCount: t.RealtimeCount,
columns: t.columns,
indices: t.indices,
Pseudo: t.Pseudo,
ModifyCount: t.ModifyCount,
StatsVer: t.StatsVer,
PhysicalID: t.PhysicalID,
RealtimeCount: t.RealtimeCount,
columns: t.columns,
indices: t.indices,
Pseudo: t.Pseudo,
ModifyCount: t.ModifyCount,
StatsVer: t.StatsVer,
}
nt := &Table{
HistColl: newHistColl,
Expand Down Expand Up @@ -928,12 +921,11 @@ func (coll *HistColl) ID2UniqueID(columns []*expression.Column) *HistColl {
}
}
newColl := &HistColl{
PhysicalID: coll.PhysicalID,
HavePhysicalID: coll.HavePhysicalID,
Pseudo: coll.Pseudo,
RealtimeCount: coll.RealtimeCount,
ModifyCount: coll.ModifyCount,
columns: cols,
PhysicalID: coll.PhysicalID,
Pseudo: coll.Pseudo,
RealtimeCount: coll.RealtimeCount,
ModifyCount: coll.ModifyCount,
columns: cols,
}
return newColl
}
Expand Down Expand Up @@ -994,7 +986,6 @@ func (coll *HistColl) GenerateHistCollFromColumnInfo(tblInfo *model.TableInfo, c
}
newColl := &HistColl{
PhysicalID: coll.PhysicalID,
HavePhysicalID: coll.HavePhysicalID,
Pseudo: coll.Pseudo,
RealtimeCount: coll.RealtimeCount,
ModifyCount: coll.ModifyCount,
Expand All @@ -1016,7 +1007,6 @@ func PseudoTable(tblInfo *model.TableInfo, allowTriggerLoading bool, allowFillHi
pseudoHistColl := HistColl{
RealtimeCount: PseudoRowCount,
PhysicalID: tblInfo.ID,
HavePhysicalID: true,
columns: make(map[int64]*Column, 2),
indices: make(map[int64]*Index, 2),
Pseudo: true,
Expand Down

0 comments on commit 35f329d

Please sign in to comment.