From 77320dae07a6adb72fcab0e9137e6b0498b52a57 Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Thu, 11 Mar 2021 14:14:17 +0800 Subject: [PATCH 1/2] planner: fix analyze clustered index table can't trigger index primary Signed-off-by: lzmhhh123 --- executor/analyze_test.go | 13 +++++++++++++ planner/core/planbuilder.go | 1 - 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/executor/analyze_test.go b/executor/analyze_test.go index 556f0b339a3de..314410e5ae440 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -926,3 +926,16 @@ func (s *testSerialSuite2) TestIssue20874(c *C) { "test t idxb 1 1 3 2 \x00C \x00C 0", )) } + +func (s *testSuite1) TestAnalyzeClusteredIndexPrimary(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("drop table if exists t0") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t0(a varchar(20), primary key(a) clustered)") + tk.MustExec("create table t1(a varchar(20), primary key(a))") + tk.MustExec("insert into t0 values('1111')") + tk.MustExec("insert into t1 values('1111')") + tk.MustExec("analyze table t0 index primary") + tk.MustExec("analyze table t1 index primary") + tk.MustQuery("show stats_buckets").Check(testkit.Rows("")) +} diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index fc1a7bea8813b..e4bb839738ad6 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -1749,7 +1749,6 @@ func (b *PlanBuilder) buildAnalyzeIndex(as *ast.AnalyzeTableStmt, opts map[ast.A } p.ColTasks = append(p.ColTasks, AnalyzeColumnsTask{HandleCols: handleCols, analyzeInfo: info, TblInfo: tblInfo}) } - continue } } idx := tblInfo.FindIndexByName(idxName.L) From 7f3e19e0333b01cd67b38466aa50662d6fb540e1 Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Thu, 11 Mar 2021 14:35:56 +0800 Subject: [PATCH 2/2] fix Signed-off-by: lzmhhh123 --- executor/analyze_test.go | 5 ++++- planner/core/planbuilder.go | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/executor/analyze_test.go b/executor/analyze_test.go index 314410e5ae440..daf12ba5f2ab5 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -929,6 +929,7 @@ func (s *testSerialSuite2) TestIssue20874(c *C) { func (s *testSuite1) TestAnalyzeClusteredIndexPrimary(c *C) { tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") tk.MustExec("drop table if exists t0") tk.MustExec("drop table if exists t1") tk.MustExec("create table t0(a varchar(20), primary key(a) clustered)") @@ -937,5 +938,7 @@ func (s *testSuite1) TestAnalyzeClusteredIndexPrimary(c *C) { tk.MustExec("insert into t1 values('1111')") tk.MustExec("analyze table t0 index primary") tk.MustExec("analyze table t1 index primary") - tk.MustQuery("show stats_buckets").Check(testkit.Rows("")) + tk.MustQuery("show stats_buckets").Check(testkit.Rows( + "test t0 PRIMARY 1 0 1 1 1111 1111 0", + "test t1 PRIMARY 1 0 1 1 1111 1111 0")) } diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index e4bb839738ad6..5b9d61d0a64ea 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -1735,7 +1735,8 @@ func (b *PlanBuilder) buildAnalyzeIndex(as *ast.AnalyzeTableStmt, opts map[ast.A for _, idxName := range as.IndexNames { if isPrimaryIndex(idxName) { handleCols := BuildHandleColsForAnalyze(b.ctx, tblInfo) - if handleCols != nil { + // Fast analyze use analyze column to solve int handle. + if handleCols != nil && handleCols.IsInt() && b.ctx.GetSessionVars().EnableFastAnalyze { for i, id := range physicalIDs { if id == tblInfo.ID { id = -1 @@ -1749,6 +1750,7 @@ func (b *PlanBuilder) buildAnalyzeIndex(as *ast.AnalyzeTableStmt, opts map[ast.A } p.ColTasks = append(p.ColTasks, AnalyzeColumnsTask{HandleCols: handleCols, analyzeInfo: info, TblInfo: tblInfo}) } + continue } } idx := tblInfo.FindIndexByName(idxName.L)