diff --git a/pkg/planner/cardinality/selectivity_test.go b/pkg/planner/cardinality/selectivity_test.go index b09f1010a618d..e645f3d863e79 100644 --- a/pkg/planner/cardinality/selectivity_test.go +++ b/pkg/planner/cardinality/selectivity_test.go @@ -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, } diff --git a/pkg/statistics/handle/autoanalyze/autoanalyze_test.go b/pkg/statistics/handle/autoanalyze/autoanalyze_test.go index fd18834fdfffb..0bedc4c92b18b 100644 --- a/pkg/statistics/handle/autoanalyze/autoanalyze_test.go +++ b/pkg/statistics/handle/autoanalyze/autoanalyze_test.go @@ -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", diff --git a/pkg/statistics/handle/autoanalyze/priorityqueue/analysis_job_factory_test.go b/pkg/statistics/handle/autoanalyze/priorityqueue/analysis_job_factory_test.go index a0646129c61ba..99d442a26d9e5 100644 --- a/pkg/statistics/handle/autoanalyze/priorityqueue/analysis_job_factory_test.go +++ b/pkg/statistics/handle/autoanalyze/priorityqueue/analysis_job_factory_test.go @@ -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, @@ -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, }, @@ -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, }, @@ -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, }, @@ -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, }, @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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, }, diff --git a/pkg/statistics/handle/bootstrap.go b/pkg/statistics/handle/bootstrap.go index a97d6b3e3c3f9..8d5b6ea8b396b 100644 --- a/pkg/statistics/handle/bootstrap.go +++ b/pkg/statistics/handle/bootstrap.go @@ -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, diff --git a/pkg/statistics/handle/cache/internal/testutil/testutil.go b/pkg/statistics/handle/cache/internal/testutil/testutil.go index 6578a660cecc2..672d33b8f611b 100644 --- a/pkg/statistics/handle/cache/internal/testutil/testutil.go +++ b/pkg/statistics/handle/cache/internal/testutil/testutil.go @@ -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 diff --git a/pkg/statistics/handle/storage/json.go b/pkg/statistics/handle/storage/json.go index 144fda1416450..faec9f25e0396 100644 --- a/pkg/statistics/handle/storage/json.go +++ b/pkg/statistics/handle/storage/json.go @@ -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)), diff --git a/pkg/statistics/handle/storage/read.go b/pkg/statistics/handle/storage/read.go index e41b832c3ffee..439fb77db5ee2 100644 --- a/pkg/statistics/handle/storage/read.go +++ b/pkg/statistics/handle/storage/read.go @@ -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)), diff --git a/pkg/statistics/statistics_test.go b/pkg/statistics/statistics_test.go index 566c72eff2aee..cfaf69cc7f68b 100644 --- a/pkg/statistics/statistics_test.go +++ b/pkg/statistics/statistics_test.go @@ -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{{}}, @@ -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)}, @@ -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()}, diff --git a/pkg/statistics/table.go b/pkg/statistics/table.go index 91734dfed013c..75c50e1921ce6 100644 --- a/pkg/statistics/table.go +++ b/pkg/statistics/table.go @@ -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 @@ -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), @@ -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), @@ -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() @@ -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, @@ -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 } @@ -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, @@ -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,