From d60c8e4e076ee48d58ea0678c483bc9e6bd4ce95 Mon Sep 17 00:00:00 2001 From: Rustin Liu Date: Thu, 30 Nov 2023 13:49:20 +0800 Subject: [PATCH] statistics: move the previous auto-analyze selection into a func (#49027) --- .../handle/autoanalyze/autoanalyze.go | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/pkg/statistics/handle/autoanalyze/autoanalyze.go b/pkg/statistics/handle/autoanalyze/autoanalyze.go index 66f9ed161d..4e4a4321f7 100644 --- a/pkg/statistics/handle/autoanalyze/autoanalyze.go +++ b/pkg/statistics/handle/autoanalyze/autoanalyze.go @@ -171,10 +171,8 @@ func HandleAutoAnalyze( } }() - dbs := is.AllSchemaNames() parameters := getAutoAnalyzeParameters(sctx) autoAnalyzeRatio := parseAutoAnalyzeRatio(parameters[variable.TiDBAutoAnalyzeRatio]) - // Get the available time period for auto analyze and check if the current time is in the period. start, end, err := parseAnalyzePeriod( parameters[variable.TiDBAutoAnalyzeStartTime], @@ -190,8 +188,32 @@ func HandleAutoAnalyze( if !timeutil.WithinDayTimePeriod(start, end, time.Now()) { return false } - pruneMode := variable.PartitionPruneMode(sctx.GetSessionVars().PartitionPruneMode.Load()) + + return randomPickOneTableAndTryAutoAnalyze( + sctx, + statsHandle, + sysProcTracker, + is, + autoAnalyzeRatio, + pruneMode, + ) +} + +// randomPickOneTableAndTryAutoAnalyze randomly picks one table and tries to analyze it. +// 1. If the table is not analyzed, analyze it. +// 2. If the table is analyzed, analyze it when "tbl.ModifyCount/tbl.Count > autoAnalyzeRatio". +// 3. If the table is analyzed, analyze its indices when the index is not analyzed. +// 4. If the table is locked, skip it. +func randomPickOneTableAndTryAutoAnalyze( + sctx sessionctx.Context, + statsHandle statstypes.StatsHandle, + sysProcTracker sessionctx.SysProcTracker, + is infoschema.InfoSchema, + autoAnalyzeRatio float64, + pruneMode variable.PartitionPruneMode, +) bool { + dbs := is.AllSchemaNames() // Shuffle the database and table slice to randomize the order of analyzing tables. rd := rand.New(rand.NewSource(time.Now().UnixNano())) // #nosec G404 rd.Shuffle(len(dbs), func(i, j int) {