From efe3793f49c08eed0e0928d9146e69bd185a1975 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Fri, 5 Jan 2024 00:27:19 +0800 Subject: [PATCH 1/5] statistics: avoid allocate the memory when to auto analyze with pseudo table Signed-off-by: Weizhen Wang --- .../handle/autoanalyze/autoanalyze.go | 4 ++-- pkg/statistics/handle/handle.go | 22 ++++++++++++++----- pkg/statistics/handle/types/interfaces.go | 3 +++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pkg/statistics/handle/autoanalyze/autoanalyze.go b/pkg/statistics/handle/autoanalyze/autoanalyze.go index 9e1fd89ed956e..87e97830eb31d 100644 --- a/pkg/statistics/handle/autoanalyze/autoanalyze.go +++ b/pkg/statistics/handle/autoanalyze/autoanalyze.go @@ -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 { @@ -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.Pseudo || statsTbl.RealtimeCount < AutoAnalyzeMinCnt { return false } diff --git a/pkg/statistics/handle/handle.go b/pkg/statistics/handle/handle.go index 82872c1ebb72e..315e46a6d9c0c 100644 --- a/pkg/statistics/handle/handle.go +++ b/pkg/statistics/handle/handle.go @@ -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 { + 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) @@ -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 } diff --git a/pkg/statistics/handle/types/interfaces.go b/pkg/statistics/handle/types/interfaces.go index d8eb1c7663ac7..b7b7a65fd3130 100644 --- a/pkg/statistics/handle/types/interfaces.go +++ b/pkg/statistics/handle/types/interfaces.go @@ -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 From 2916e887c302387d2f623f0d91ee5fb2699a73ce Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Fri, 5 Jan 2024 00:27:24 +0800 Subject: [PATCH 2/5] statistics: avoid allocate the memory when to auto analyze with pseudo table Signed-off-by: Weizhen Wang --- pkg/statistics/handle/autoanalyze/autoanalyze.go | 2 +- pkg/statistics/handle/handle.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/statistics/handle/autoanalyze/autoanalyze.go b/pkg/statistics/handle/autoanalyze/autoanalyze.go index 87e97830eb31d..bda7347bff4b1 100644 --- a/pkg/statistics/handle/autoanalyze/autoanalyze.go +++ b/pkg/statistics/handle/autoanalyze/autoanalyze.go @@ -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 == nil || statsTbl.Pseudo || statsTbl.RealtimeCount < AutoAnalyzeMinCnt { + if statsTbl == nil || statsTbl.RealtimeCount < AutoAnalyzeMinCnt || statsTbl.Pseudo { return false } diff --git a/pkg/statistics/handle/handle.go b/pkg/statistics/handle/handle.go index 315e46a6d9c0c..dd523bb8a7a8a 100644 --- a/pkg/statistics/handle/handle.go +++ b/pkg/statistics/handle/handle.go @@ -157,7 +157,7 @@ func (h *Handle) GetTableStatsForAutoAnalyze(tblInfo *model.TableInfo) *statisti // GetPartitionStats retrieves the partition stats from cache. // TODO: remove GetTableStats later on. func (h *Handle) GetPartitionStats(tblInfo *model.TableInfo, pid int64) *statistics.Table { - h.getPartitionStats(tblInfo, pid, true) + return h.getPartitionStats(tblInfo, pid, true) } func (h *Handle) getPartitionStats(tblInfo *model.TableInfo, pid int64, returnPseudo bool) *statistics.Table { From 5964b5f63c9c939625949332ceff6da77ebf8e6a Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Fri, 5 Jan 2024 11:32:34 +0800 Subject: [PATCH 3/5] *: remove useless code Signed-off-by: Weizhen Wang --- pkg/statistics/handle/autoanalyze/autoanalyze.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/statistics/handle/autoanalyze/autoanalyze.go b/pkg/statistics/handle/autoanalyze/autoanalyze.go index bda7347bff4b1..4376d67efb722 100644 --- a/pkg/statistics/handle/autoanalyze/autoanalyze.go +++ b/pkg/statistics/handle/autoanalyze/autoanalyze.go @@ -494,7 +494,8 @@ 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 == nil || statsTbl.RealtimeCount < AutoAnalyzeMinCnt || statsTbl.Pseudo { + // 3. Pseudo will not appear here. because nil is pseudo. + if statsTbl == nil || statsTbl.RealtimeCount < AutoAnalyzeMinCnt { return false } From 4a267248253a6f8fac896dafb488ef839b1286ee Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Fri, 5 Jan 2024 11:38:07 +0800 Subject: [PATCH 4/5] *: remove useless code Signed-off-by: Weizhen Wang --- pkg/statistics/handle/autoanalyze/autoanalyze.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/statistics/handle/autoanalyze/autoanalyze.go b/pkg/statistics/handle/autoanalyze/autoanalyze.go index 4376d67efb722..bda7347bff4b1 100644 --- a/pkg/statistics/handle/autoanalyze/autoanalyze.go +++ b/pkg/statistics/handle/autoanalyze/autoanalyze.go @@ -494,8 +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. - // 3. Pseudo will not appear here. because nil is pseudo. - if statsTbl == nil || statsTbl.RealtimeCount < AutoAnalyzeMinCnt { + if statsTbl == nil || statsTbl.RealtimeCount < AutoAnalyzeMinCnt || statsTbl.Pseudo { return false } From 226801033d971f45a8b46fbb193f75e6492a5146 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Fri, 5 Jan 2024 11:41:49 +0800 Subject: [PATCH 5/5] *: remove useless code Signed-off-by: Weizhen Wang --- pkg/statistics/handle/autoanalyze/autoanalyze.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/statistics/handle/autoanalyze/autoanalyze.go b/pkg/statistics/handle/autoanalyze/autoanalyze.go index bda7347bff4b1..f59e90d28fdbd 100644 --- a/pkg/statistics/handle/autoanalyze/autoanalyze.go +++ b/pkg/statistics/handle/autoanalyze/autoanalyze.go @@ -491,7 +491,7 @@ 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 == nil || statsTbl.RealtimeCount < AutoAnalyzeMinCnt || statsTbl.Pseudo {