From fb81a7dfd81a2b2841edbc4ddbc56b47c45f330f Mon Sep 17 00:00:00 2001 From: Kenan Yao Date: Wed, 2 Jun 2021 14:51:38 +0800 Subject: [PATCH] planner: warn for incremental analyze in version 3 stats (#24689) --- planner/core/integration_test.go | 25 +++++++++++++++++++++++++ planner/core/planbuilder.go | 5 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 1e13d28b96..c691e5341b 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -3637,6 +3637,31 @@ func (s *testIntegrationSuite) TestIssue24281(c *C) { "UNION select 1 as v1, 2 as v2") } +func (s *testIntegrationSuite) TestIncrementalAnalyzeStatsVer3(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int primary key, b int, index idx_b(b))") + tk.MustExec("insert into t values(1,1),(2,2),(3,3)") + tk.MustExec("set @@session.tidb_analyze_version = 3") + tk.MustExec("analyze table t") + is := tk.Se.GetInfoSchema().(infoschema.InfoSchema) + tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) + c.Assert(err, IsNil) + tblID := tbl.Meta().ID + rows := tk.MustQuery(fmt.Sprintf("select distinct_count from mysql.stats_histograms where table_id = %d and is_index = 1", tblID)).Rows() + c.Assert(len(rows), Equals, 1) + c.Assert(rows[0][0], Equals, "3") + tk.MustExec("insert into t values(4,4),(5,5),(6,6)") + tk.MustExec("analyze incremental table t index idx_b") + c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 2) + c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings()[0].Err.Error(), Equals, "The version 3 would collect all statistics not only the selected indexes") + c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings()[1].Err.Error(), Equals, "The version 3 stats would ignore the INCREMENTAL keyword and do full sampling") + rows = tk.MustQuery(fmt.Sprintf("select distinct_count from mysql.stats_histograms where table_id = %d and is_index = 1", tblID)).Rows() + c.Assert(len(rows), Equals, 1) + c.Assert(rows[0][0], Equals, "6") +} + func (s *testIntegrationSuite) TestConflictReadFromStorage(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index f2de374321..3990cade06 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -1683,6 +1683,9 @@ func (b *PlanBuilder) buildAnalyzeFullSamplingTask( } idxInfos = append(idxInfos, idx) } + if as.Incremental { + b.ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("The version 3 stats would ignore the INCREMENTAL keyword and do full sampling")) + } for i, id := range physicalIDs { if id == tbl.TableInfo.ID { id = -1 @@ -1692,7 +1695,7 @@ func (b *PlanBuilder) buildAnalyzeFullSamplingTask( TableName: tbl.Name.O, PartitionName: names[i], TableID: AnalyzeTableID{TableID: tbl.TableInfo.ID, PartitionID: id}, - Incremental: as.Incremental, + Incremental: false, StatsVersion: version, } newTask := AnalyzeColumnsTask{