Skip to content

Commit 430072a

Browse files
ti-chi-bothawkingrei
authored andcommitted
statistics: add metrics for unneeded analyze table (pingcap#54822) (pingcap#57100)
close pingcap#54823
1 parent df6a0a7 commit 430072a

File tree

20 files changed

+112
-115
lines changed

20 files changed

+112
-115
lines changed

pkg/executor/test/analyzetest/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ go_test(
2626
"//pkg/sessionctx",
2727
"//pkg/sessionctx/variable",
2828
"//pkg/statistics",
29-
"//pkg/statistics/handle/autoanalyze/exec",
3029
"//pkg/testkit",
3130
"//pkg/util/dbterror/exeerrors",
3231
"@com_github_pingcap_errors//:errors",

pkg/executor/test/analyzetest/analyze_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"github.com/pingcap/tidb/pkg/sessionctx"
4040
"github.com/pingcap/tidb/pkg/sessionctx/variable"
4141
"github.com/pingcap/tidb/pkg/statistics"
42-
"github.com/pingcap/tidb/pkg/statistics/handle/autoanalyze/exec"
4342
"github.com/pingcap/tidb/pkg/testkit"
4443
"github.com/pingcap/tidb/pkg/util/dbterror/exeerrors"
4544
"github.com/stretchr/testify/require"
@@ -689,11 +688,11 @@ func TestSavedAnalyzeOptions(t *testing.T) {
689688
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_ratio = %v", originalVal2))
690689
}()
691690
tk.MustExec("set global tidb_auto_analyze_ratio = 0.01")
692-
originalVal3 := exec.AutoAnalyzeMinCnt
691+
originalVal3 := statistics.AutoAnalyzeMinCnt
693692
defer func() {
694-
exec.AutoAnalyzeMinCnt = originalVal3
693+
statistics.AutoAnalyzeMinCnt = originalVal3
695694
}()
696-
exec.AutoAnalyzeMinCnt = 0
695+
statistics.AutoAnalyzeMinCnt = 0
697696

698697
tk.MustExec("use test")
699698
tk.MustExec("set @@session.tidb_analyze_version = 2")
@@ -1031,11 +1030,11 @@ func TestSavedAnalyzeColumnOptions(t *testing.T) {
10311030
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_ratio = %v", originalVal2))
10321031
}()
10331032
tk.MustExec("set global tidb_auto_analyze_ratio = 0.01")
1034-
originalVal3 := exec.AutoAnalyzeMinCnt
1033+
originalVal3 := statistics.AutoAnalyzeMinCnt
10351034
defer func() {
1036-
exec.AutoAnalyzeMinCnt = originalVal3
1035+
statistics.AutoAnalyzeMinCnt = originalVal3
10371036
}()
1038-
exec.AutoAnalyzeMinCnt = 0
1037+
statistics.AutoAnalyzeMinCnt = 0
10391038
originalVal4 := tk.MustQuery("select @@tidb_enable_column_tracking").Rows()[0][0].(string)
10401039
defer func() {
10411040
tk.MustExec(fmt.Sprintf("set global tidb_enable_column_tracking = %v", originalVal4))
@@ -1888,9 +1887,9 @@ func testKillAutoAnalyze(t *testing.T, ver int) {
18881887
tk := testkit.NewTestKit(t, store)
18891888
oriStart := tk.MustQuery("select @@tidb_auto_analyze_start_time").Rows()[0][0].(string)
18901889
oriEnd := tk.MustQuery("select @@tidb_auto_analyze_end_time").Rows()[0][0].(string)
1891-
exec.AutoAnalyzeMinCnt = 0
1890+
statistics.AutoAnalyzeMinCnt = 0
18921891
defer func() {
1893-
exec.AutoAnalyzeMinCnt = 1000
1892+
statistics.AutoAnalyzeMinCnt = 1000
18941893
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_start_time='%v'", oriStart))
18951894
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_end_time='%v'", oriEnd))
18961895
}()
@@ -1971,9 +1970,9 @@ func TestKillAutoAnalyzeIndex(t *testing.T) {
19711970
tk := testkit.NewTestKit(t, store)
19721971
oriStart := tk.MustQuery("select @@tidb_auto_analyze_start_time").Rows()[0][0].(string)
19731972
oriEnd := tk.MustQuery("select @@tidb_auto_analyze_end_time").Rows()[0][0].(string)
1974-
exec.AutoAnalyzeMinCnt = 0
1973+
statistics.AutoAnalyzeMinCnt = 0
19751974
defer func() {
1976-
exec.AutoAnalyzeMinCnt = 1000
1975+
statistics.AutoAnalyzeMinCnt = 1000
19771976
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_start_time='%v'", oriStart))
19781977
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_end_time='%v'", oriEnd))
19791978
}()
@@ -2729,12 +2728,12 @@ func TestAutoAnalyzeAwareGlobalVariableChange(t *testing.T) {
27292728
"3 0",
27302729
))
27312730

2732-
originalVal1 := exec.AutoAnalyzeMinCnt
2731+
originalVal1 := statistics.AutoAnalyzeMinCnt
27332732
originalVal2 := tk.MustQuery("select @@global.tidb_auto_analyze_ratio").Rows()[0][0].(string)
2734-
exec.AutoAnalyzeMinCnt = 0
2733+
statistics.AutoAnalyzeMinCnt = 0
27352734
tk.MustExec("set global tidb_auto_analyze_ratio = 0.001")
27362735
defer func() {
2737-
exec.AutoAnalyzeMinCnt = originalVal1
2736+
statistics.AutoAnalyzeMinCnt = originalVal1
27382737
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_ratio = %v", originalVal2))
27392738
}()
27402739

pkg/executor/test/analyzetest/memorycontrol/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ go_test(
1313
"//pkg/config",
1414
"//pkg/executor",
1515
"//pkg/sessionctx/variable",
16-
"//pkg/statistics/handle/autoanalyze/exec",
16+
"//pkg/statistics",
1717
"//pkg/testkit",
1818
"//pkg/util",
1919
"@com_github_pingcap_failpoint//:failpoint",

pkg/executor/test/analyzetest/memorycontrol/memory_control_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
"github.com/pingcap/failpoint"
2424
"github.com/pingcap/tidb/pkg/executor"
25-
"github.com/pingcap/tidb/pkg/statistics/handle/autoanalyze/exec"
25+
"github.com/pingcap/tidb/pkg/statistics"
2626
"github.com/pingcap/tidb/pkg/testkit"
2727
"github.com/pingcap/tidb/pkg/util"
2828
"github.com/stretchr/testify/require"
@@ -143,12 +143,12 @@ func TestGlobalMemoryControlForAutoAnalyze(t *testing.T) {
143143
require.Len(t, rs0.Rows(), 0)
144144

145145
h := dom.StatsHandle()
146-
originalVal4 := exec.AutoAnalyzeMinCnt
146+
originalVal4 := statistics.AutoAnalyzeMinCnt
147147
originalVal5 := tk.MustQuery("select @@global.tidb_auto_analyze_ratio").Rows()[0][0].(string)
148-
exec.AutoAnalyzeMinCnt = 0
148+
statistics.AutoAnalyzeMinCnt = 0
149149
tk.MustExec("set global tidb_auto_analyze_ratio = 0.001")
150150
defer func() {
151-
exec.AutoAnalyzeMinCnt = originalVal4
151+
statistics.AutoAnalyzeMinCnt = originalVal4
152152
tk.MustExec(fmt.Sprintf("set global tidb_auto_analyze_ratio = %v", originalVal5))
153153
}()
154154

pkg/statistics/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ go_test(
8888
"//pkg/parser/mysql",
8989
"//pkg/sessionctx",
9090
"//pkg/sessionctx/stmtctx",
91-
"//pkg/statistics/handle/autoanalyze/exec",
9291
"//pkg/testkit",
9392
"//pkg/testkit/testdata",
9493
"//pkg/testkit/testmain",

pkg/statistics/handle/autoanalyze/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ go_test(
4444
"//pkg/sessionctx",
4545
"//pkg/sessionctx/variable",
4646
"//pkg/statistics",
47-
"//pkg/statistics/handle/autoanalyze/exec",
4847
"//pkg/statistics/handle/util",
4948
"//pkg/statistics/handle/util/test",
5049
"//pkg/testkit",

pkg/statistics/handle/autoanalyze/autoanalyze.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func tryAutoAnalyzeTable(
476476
// Pseudo statistics can be created by the optimizer, so we need to double check it.
477477
// 2. If the table is too small, we don't want to waste time to analyze it.
478478
// Leave the opportunity to other bigger tables.
479-
if statsTbl == nil || statsTbl.Pseudo || statsTbl.RealtimeCount < exec.AutoAnalyzeMinCnt {
479+
if statsTbl == nil || statsTbl.Pseudo || statsTbl.RealtimeCount < statistics.AutoAnalyzeMinCnt {
480480
return false
481481
}
482482

@@ -573,7 +573,7 @@ func tryAutoAnalyzePartitionTableInDynamicMode(
573573
// Pseudo statistics can be created by the optimizer, so we need to double check it.
574574
// 2. If the table is too small, we don't want to waste time to analyze it.
575575
// Leave the opportunity to other bigger tables.
576-
if partitionStats == nil || partitionStats.Pseudo || partitionStats.RealtimeCount < exec.AutoAnalyzeMinCnt {
576+
if partitionStats == nil || partitionStats.Pseudo || partitionStats.RealtimeCount < statistics.AutoAnalyzeMinCnt {
577577
continue
578578
}
579579
if needAnalyze, reason := NeedAnalyzeTable(

pkg/statistics/handle/autoanalyze/autoanalyze_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/pingcap/tidb/pkg/sessionctx/variable"
3030
"github.com/pingcap/tidb/pkg/statistics"
3131
"github.com/pingcap/tidb/pkg/statistics/handle/autoanalyze"
32-
"github.com/pingcap/tidb/pkg/statistics/handle/autoanalyze/exec"
3332
statsutil "github.com/pingcap/tidb/pkg/statistics/handle/util"
3433
"github.com/pingcap/tidb/pkg/statistics/handle/util/test"
3534
"github.com/pingcap/tidb/pkg/testkit"
@@ -56,9 +55,9 @@ func TestEnableAutoAnalyzePriorityQueue(t *testing.T) {
5655
require.NoError(t, h.DumpStatsDeltaToKV(true))
5756
is := dom.InfoSchema()
5857
require.NoError(t, h.Update(is))
59-
exec.AutoAnalyzeMinCnt = 0
58+
statistics.AutoAnalyzeMinCnt = 0
6059
defer func() {
61-
exec.AutoAnalyzeMinCnt = 1000
60+
statistics.AutoAnalyzeMinCnt = 1000
6261
}()
6362
require.True(t, dom.StatsHandle().HandleAutoAnalyze())
6463
}
@@ -77,9 +76,9 @@ func TestAutoAnalyzeLockedTable(t *testing.T) {
7776
tk.MustExec("lock stats t")
7877
is := dom.InfoSchema()
7978
require.NoError(t, h.Update(is))
80-
exec.AutoAnalyzeMinCnt = 0
79+
statistics.AutoAnalyzeMinCnt = 0
8180
defer func() {
82-
exec.AutoAnalyzeMinCnt = 1000
81+
statistics.AutoAnalyzeMinCnt = 1000
8382
}()
8483
// Try to analyze the locked table, it should not analyze the table.
8584
require.False(t, dom.StatsHandle().HandleAutoAnalyze())
@@ -104,9 +103,9 @@ func TestDisableAutoAnalyze(t *testing.T) {
104103
require.NoError(t, h.Update(is))
105104

106105
tk.MustExec("set @@global.tidb_enable_auto_analyze = 0")
107-
exec.AutoAnalyzeMinCnt = 0
106+
statistics.AutoAnalyzeMinCnt = 0
108107
defer func() {
109-
exec.AutoAnalyzeMinCnt = 1000
108+
statistics.AutoAnalyzeMinCnt = 1000
110109
}()
111110
// Even auto analyze ratio is set to 0, we still need to analyze the unanalyzed tables.
112111
require.True(t, dom.StatsHandle().HandleAutoAnalyze())
@@ -129,9 +128,9 @@ func TestAutoAnalyzeOnChangeAnalyzeVer(t *testing.T) {
129128
tk.MustExec("insert into t values(1)")
130129
tk.MustExec("set @@global.tidb_analyze_version = 1")
131130
do := dom
132-
exec.AutoAnalyzeMinCnt = 0
131+
statistics.AutoAnalyzeMinCnt = 0
133132
defer func() {
134-
exec.AutoAnalyzeMinCnt = 1000
133+
statistics.AutoAnalyzeMinCnt = 1000
135134
}()
136135
h := do.StatsHandle()
137136
err := h.HandleDDLEvent(<-h.DDLEventCh())
@@ -309,10 +308,10 @@ func TestAutoAnalyzeSkipColumnTypes(t *testing.T) {
309308
require.NoError(t, h.Update(dom.InfoSchema()))
310309
tk.MustExec("set @@global.tidb_analyze_skip_column_types = 'json,blob,mediumblob,text,mediumtext'")
311310

312-
originalVal := exec.AutoAnalyzeMinCnt
313-
exec.AutoAnalyzeMinCnt = 0
311+
originalVal := statistics.AutoAnalyzeMinCnt
312+
statistics.AutoAnalyzeMinCnt = 0
314313
defer func() {
315-
exec.AutoAnalyzeMinCnt = originalVal
314+
statistics.AutoAnalyzeMinCnt = originalVal
316315
}()
317316
require.True(t, h.HandleAutoAnalyze())
318317
tk.MustQuery("select job_info from mysql.analyze_jobs where job_info like '%auto analyze table%'").Check(testkit.Rows("auto analyze table columns a, b, d with 256 buckets, 500 topn, 1 samplerate"))
@@ -341,7 +340,7 @@ func TestAutoAnalyzeOnEmptyTable(t *testing.T) {
341340
// to pass the stats.Pseudo check in autoAnalyzeTable
342341
tk.MustExec("analyze table t")
343342
// to pass the AutoAnalyzeMinCnt check in autoAnalyzeTable
344-
tk.MustExec("insert into t values (1)" + strings.Repeat(", (1)", int(exec.AutoAnalyzeMinCnt)))
343+
tk.MustExec("insert into t values (1)" + strings.Repeat(", (1)", int(statistics.AutoAnalyzeMinCnt)))
345344
require.NoError(t, dom.StatsHandle().DumpStatsDeltaToKV(true))
346345
require.NoError(t, dom.StatsHandle().Update(dom.InfoSchema()))
347346

@@ -376,7 +375,7 @@ func TestAutoAnalyzeOutOfSpecifiedTime(t *testing.T) {
376375
// to pass the stats.Pseudo check in autoAnalyzeTable
377376
tk.MustExec("analyze table t")
378377
// to pass the AutoAnalyzeMinCnt check in autoAnalyzeTable
379-
tk.MustExec("insert into t values (1)" + strings.Repeat(", (1)", int(exec.AutoAnalyzeMinCnt)))
378+
tk.MustExec("insert into t values (1)" + strings.Repeat(", (1)", int(statistics.AutoAnalyzeMinCnt)))
380379
require.NoError(t, dom.StatsHandle().DumpStatsDeltaToKV(true))
381380
require.NoError(t, dom.StatsHandle().Update(dom.InfoSchema()))
382381

pkg/statistics/handle/autoanalyze/exec/exec.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ import (
3535
"go.uber.org/zap"
3636
)
3737

38-
// AutoAnalyzeMinCnt means if the count of table is less than this value, we don't need to do auto analyze.
39-
// Exported for testing.
40-
var AutoAnalyzeMinCnt int64 = 1000
41-
4238
var execOptionForAnalyze = map[int]sqlexec.OptionFuncAlias{
4339
statistics.Version0: sqlexec.ExecOptionAnalyzeVer1,
4440
statistics.Version1: sqlexec.ExecOptionAnalyzeVer1,

pkg/statistics/handle/autoanalyze/refresher/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ go_test(
3535
":refresher",
3636
"//pkg/parser/model",
3737
"//pkg/statistics",
38-
"//pkg/statistics/handle/autoanalyze/exec",
3938
"//pkg/statistics/handle/autoanalyze/priorityqueue",
4039
"//pkg/testkit",
4140
"@com_github_stretchr_testify//require",

0 commit comments

Comments
 (0)