Skip to content

Commit

Permalink
statistics: avoid allocating the memory when to auto analyze with pse…
Browse files Browse the repository at this point in the history
…udo table (#50099)

close #50100
  • Loading branch information
hawkingrei authored Jan 5, 2024
1 parent 9190b05 commit 7e83932
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
6 changes: 3 additions & 3 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 @@ -491,10 +491,10 @@ func tryAutoAnalyzeTable(
sql string,
params ...interface{},
) bool {
// 1. If the stats are not loaded, we don't need to analyze it.
// 1. If the statistics are either not loaded or are classified as pseudo, there is no need for analyze
// 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 {
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

0 comments on commit 7e83932

Please sign in to comment.