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: avoid allocating the memory when to auto analyze with pseudo table #50099

Merged
merged 5 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions pkg/statistics/handle/autoanalyze/autoanalyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func RandomPickOneTableAndTryAutoAnalyze(
pi := tblInfo.GetPartitionInfo()
// No partitions, analyze the whole table.
if pi == nil {
statsTbl := statsHandle.GetTableStats(tblInfo)
statsTbl := statsHandle.GetTableStatsForAutoAnalyze(tblInfo)
sql := "analyze table %n.%n"
analyzed := tryAutoAnalyzeTable(sctx, statsHandle, sysProcTracker, tblInfo, statsTbl, autoAnalyzeRatio, sql, db, tblInfo.Name.O)
if analyzed {
Expand Down Expand Up @@ -494,7 +494,7 @@ func tryAutoAnalyzeTable(
// 1. If the stats are not loaded, we don't need to analyze it.
// 2. If the table is too small, we don't want to waste time to analyze it.
// Leave the opportunity to other bigger tables.
if statsTbl.Pseudo || statsTbl.RealtimeCount < AutoAnalyzeMinCnt {
if statsTbl == nil || statsTbl.RealtimeCount < AutoAnalyzeMinCnt || statsTbl.Pseudo {
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
return false
}

Expand Down
22 changes: 17 additions & 5 deletions pkg/statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,18 @@ func (h *Handle) GetTableStats(tblInfo *model.TableInfo) *statistics.Table {
return h.GetPartitionStats(tblInfo, tblInfo.ID)
}

// GetTableStatsForAutoAnalyze is to get table stats but it will
func (h *Handle) GetTableStatsForAutoAnalyze(tblInfo *model.TableInfo) *statistics.Table {
return h.getPartitionStats(tblInfo, tblInfo.ID, false)
}

// GetPartitionStats retrieves the partition stats from cache.
// TODO: remove GetTableStats later on.
func (h *Handle) GetPartitionStats(tblInfo *model.TableInfo, pid int64) *statistics.Table {
return h.getPartitionStats(tblInfo, pid, true)
}

func (h *Handle) getPartitionStats(tblInfo *model.TableInfo, pid int64, returnPseudo bool) *statistics.Table {
var tbl *statistics.Table
if h == nil {
tbl = statistics.PseudoTable(tblInfo, false)
Expand All @@ -160,12 +169,15 @@ func (h *Handle) GetPartitionStats(tblInfo *model.TableInfo, pid int64) *statist
}
tbl, ok := h.Get(pid)
if !ok {
tbl = statistics.PseudoTable(tblInfo, false)
tbl.PhysicalID = pid
if tblInfo.GetPartitionInfo() == nil || h.Len() < 64 {
h.UpdateStatsCache([]*statistics.Table{tbl}, nil)
if returnPseudo {
tbl = statistics.PseudoTable(tblInfo, false)
tbl.PhysicalID = pid
if tblInfo.GetPartitionInfo() == nil || h.Len() < 64 {
h.UpdateStatsCache([]*statistics.Table{tbl}, nil)
}
return tbl
}
return tbl
return nil
}
return tbl
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/statistics/handle/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ type StatsHandle interface {
// GetTableStats retrieves the statistics table from cache, and the cache will be updated by a goroutine.
GetTableStats(tblInfo *model.TableInfo) *statistics.Table

// GetTableStatsForAutoAnalyze retrieves the statistics table from cache, but it will not return pseudo.
GetTableStatsForAutoAnalyze(tblInfo *model.TableInfo) *statistics.Table

// GetPartitionStats retrieves the partition stats from cache.
GetPartitionStats(tblInfo *model.TableInfo, pid int64) *statistics.Table

Expand Down