Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: do not depend on table information when calculating the table size #56036

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pkg/statistics/handle/autoanalyze/refresher/refresher.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func CreateTableAnalysisJob(
statistics.CheckAnalyzeVerOnTable(tblStats, &tableStatsVer)

changePercentage := CalculateChangePercentage(tblStats, autoAnalyzeRatio)
tableSize := calculateTableSize(tblInfo, tblStats)
tableSize := calculateTableSize(tblStats)
lastAnalysisDuration := GetTableLastAnalyzeDuration(tblStats, currentTs)
indexes := CheckIndexesNeedAnalyze(tblInfo, tblStats)

Expand Down Expand Up @@ -391,7 +391,7 @@ func CreateStaticPartitionAnalysisJob(
statistics.CheckAnalyzeVerOnTable(partitionStats, &tableStatsVer)

changePercentage := CalculateChangePercentage(partitionStats, autoAnalyzeRatio)
tableSize := calculateTableSize(globalTblInfo, partitionStats)
tableSize := calculateTableSize(partitionStats)
lastAnalysisDuration := GetTableLastAnalyzeDuration(partitionStats, currentTs)
indexes := CheckIndexesNeedAnalyze(globalTblInfo, partitionStats)

Expand Down Expand Up @@ -448,12 +448,11 @@ func CalculateChangePercentage(
}

func calculateTableSize(
tblInfo *model.TableInfo,
tblStats *statistics.Table,
) float64 {
tblCnt := float64(tblStats.RealtimeCount)
// TODO: Ignore unanalyzable columns.
colCnt := float64(len(tblInfo.Columns))
colCnt := float64(tblStats.ColNum())
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
intest.Assert(colCnt != 0, "Column count should not be 0")

return tblCnt * colCnt
}
Expand Down Expand Up @@ -526,7 +525,7 @@ func createTableAnalysisJobForPartitions(
statistics.CheckAnalyzeVerOnTable(tblStats, &tableStatsVer)

averageChangePercentage, avgSize, minLastAnalyzeDuration, partitionNames := CalculateIndicatorsForPartitions(
tblInfo,
tblStats,
partitionStats,
autoAnalyzeRatio,
currentTs,
Expand Down Expand Up @@ -563,7 +562,7 @@ func createTableAnalysisJobForPartitions(
// Size is the product of the number of rows and the number of columns.
// Last analyze duration is the duration since the last analyze.
func CalculateIndicatorsForPartitions(
tblInfo *model.TableInfo,
globalStats *statistics.Table,
partitionStats map[PartitionIDAndName]*statistics.Table,
autoAnalyzeRatio float64,
currentTs uint64,
Expand All @@ -577,7 +576,8 @@ func CalculateIndicatorsForPartitions(
totalSize := 0.0
count := 0.0
partitionNames = make([]string, 0, len(partitionStats))
cols := float64(len(tblInfo.Columns))
cols := float64(globalStats.ColNum())
intest.Assert(cols != 0, "Column count should not be 0")
totalLastAnalyzeDuration := time.Duration(0)

for pIDAndName, tblStats := range partitionStats {
Expand Down
61 changes: 11 additions & 50 deletions pkg/statistics/handle/autoanalyze/refresher/refresher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,12 @@ func TestCalculateIndicatorsForPartitions(t *testing.T) {
analyzedMap.InsertCol(1, nil, true)
analyzedMap.InsertCol(2, nil, true)
analyzedMap.InsertIndex(1, nil, true)
histColl := statistics.NewHistColl(1, false, 1000, 10000, 2, 0)
histColl.SetCol(1, &statistics.Column{})
histColl.SetCol(2, &statistics.Column{})
tests := []struct {
name string
tblInfo *model.TableInfo
globalStats *statistics.Table
partitionStats map[refresher.PartitionIDAndName]*statistics.Table
defs []model.PartitionDefinition
autoAnalyzeRatio float64
Expand All @@ -626,22 +629,8 @@ func TestCalculateIndicatorsForPartitions(t *testing.T) {
}{
{
name: "Test Table not analyzed",
tblInfo: &model.TableInfo{
Indices: []*model.IndexInfo{
{
ID: 1,
Name: pmodel.NewCIStr("index1"),
State: model.StatePublic,
},
},
Columns: []*model.ColumnInfo{
{
ID: 1,
},
{
ID: 2,
},
},
globalStats: &statistics.Table{
HistColl: *histColl,
},
partitionStats: map[refresher.PartitionIDAndName]*statistics.Table{
{
Expand Down Expand Up @@ -684,22 +673,8 @@ func TestCalculateIndicatorsForPartitions(t *testing.T) {
},
{
name: "Test Table analyzed and only one partition meets the threshold",
tblInfo: &model.TableInfo{
Indices: []*model.IndexInfo{
{
ID: 1,
Name: pmodel.NewCIStr("index1"),
State: model.StatePublic,
},
},
Columns: []*model.ColumnInfo{
{
ID: 1,
},
{
ID: 2,
},
},
globalStats: &statistics.Table{
HistColl: *histColl,
},
partitionStats: map[refresher.PartitionIDAndName]*statistics.Table{
{
Expand Down Expand Up @@ -766,22 +741,8 @@ func TestCalculateIndicatorsForPartitions(t *testing.T) {
},
{
name: "No partition meets the threshold",
tblInfo: &model.TableInfo{
Indices: []*model.IndexInfo{
{
ID: 1,
Name: pmodel.NewCIStr("index1"),
State: model.StatePublic,
},
},
Columns: []*model.ColumnInfo{
{
ID: 1,
},
{
ID: 2,
},
},
globalStats: &statistics.Table{
HistColl: *histColl,
},
partitionStats: map[refresher.PartitionIDAndName]*statistics.Table{
{
Expand Down Expand Up @@ -855,7 +816,7 @@ func TestCalculateIndicatorsForPartitions(t *testing.T) {
gotAvgLastAnalyzeDuration,
gotPartitions :=
refresher.CalculateIndicatorsForPartitions(
tt.tblInfo,
tt.globalStats,
tt.partitionStats,
tt.autoAnalyzeRatio,
tt.currentTs,
Expand Down