diff --git a/executor/analyze.go b/executor/analyze.go index 5a2ee83560fb7..130ad4597e36b 100644 --- a/executor/analyze.go +++ b/executor/analyze.go @@ -965,7 +965,9 @@ func (e *AnalyzeFastExec) handleSampTasks(bo *tikv.Backoffer, workID int, err *e if *err != nil { return } - kvMap[string(iter.Key())] = iter.Value() + if iter.Valid() { + kvMap[string(iter.Key())] = iter.Value() + } } *err = e.handleBatchSeekResponse(kvMap) diff --git a/executor/analyze_test.go b/executor/analyze_test.go index 135e42ece6c91..ec3d98e188d42 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -239,7 +239,7 @@ func (s *testSuite1) TestFastAnalyze(c *C) { dom, err = session.BootstrapSession(store) c.Assert(err, IsNil) tk := testkit.NewTestKit(c, store) - executor.MaxSampleSize = 1000 + executor.MaxSampleSize = 6 executor.RandSeed = 123 tk.MustExec("use test") @@ -251,12 +251,12 @@ func (s *testSuite1) TestFastAnalyze(c *C) { c.Assert(err, IsNil) tid := tblInfo.Meta().ID - // construct 5 regions split by {600, 1200, 1800, 2400} - splitKeys := generateTableSplitKeyForInt(tid, []int{600, 1200, 1800, 2400}) + // construct 6 regions split by {10, 20, 30, 40, 50} + splitKeys := generateTableSplitKeyForInt(tid, []int{10, 20, 30, 40, 50}) manipulateCluster(cluster, splitKeys) - for i := 0; i < 3000; i++ { - tk.MustExec(fmt.Sprintf(`insert into t values (%d, %d, "char")`, i, i)) + for i := 0; i < 20; i++ { + tk.MustExec(fmt.Sprintf(`insert into t values (%d, %d, "char")`, i*3, i*3)) } tk.MustExec("analyze table t with 5 buckets") @@ -265,27 +265,21 @@ func (s *testSuite1) TestFastAnalyze(c *C) { c.Assert(err, IsNil) tableInfo := table.Meta() tbl := dom.StatsHandle().GetTableStats(tableInfo) - c.Assert(tbl.String(), Equals, "Table:41 Count:3000\n"+ - "column:1 ndv:3000 totColSize:0\n"+ - "num: 603 lower_bound: 0 upper_bound: 658 repeats: 1\n"+ - "num: 603 lower_bound: 663 upper_bound: 1248 repeats: 1\n"+ - "num: 603 lower_bound: 1250 upper_bound: 1823 repeats: 1\n"+ - "num: 603 lower_bound: 1830 upper_bound: 2379 repeats: 1\n"+ - "num: 588 lower_bound: 2380 upper_bound: 2998 repeats: 1\n"+ - "column:2 ndv:3000 totColSize:0\n"+ - "num: 603 lower_bound: 0 upper_bound: 658 repeats: 1\n"+ - "num: 603 lower_bound: 663 upper_bound: 1248 repeats: 1\n"+ - "num: 603 lower_bound: 1250 upper_bound: 1823 repeats: 1\n"+ - "num: 603 lower_bound: 1830 upper_bound: 2379 repeats: 1\n"+ - "num: 588 lower_bound: 2380 upper_bound: 2998 repeats: 1\n"+ - "column:3 ndv:1 totColSize:12000\n"+ - "num: 3000 lower_bound: char upper_bound: char repeats: 3000\n"+ - "index:1 ndv:3000\n"+ - "num: 603 lower_bound: 0 upper_bound: 658 repeats: 1\n"+ - "num: 603 lower_bound: 663 upper_bound: 1248 repeats: 1\n"+ - "num: 603 lower_bound: 1250 upper_bound: 1823 repeats: 1\n"+ - "num: 603 lower_bound: 1830 upper_bound: 2379 repeats: 1\n"+ - "num: 588 lower_bound: 2380 upper_bound: 2998 repeats: 1") + c.Assert(tbl.String(), Equals, "Table:41 Count:20\n"+ + "column:1 ndv:20 totColSize:0\n"+ + "num: 6 lower_bound: 3 upper_bound: 15 repeats: 1\n"+ + "num: 7 lower_bound: 18 upper_bound: 33 repeats: 1\n"+ + "num: 7 lower_bound: 39 upper_bound: 57 repeats: 1\n"+ + "column:2 ndv:20 totColSize:0\n"+ + "num: 6 lower_bound: 3 upper_bound: 15 repeats: 1\n"+ + "num: 7 lower_bound: 18 upper_bound: 33 repeats: 1\n"+ + "num: 7 lower_bound: 39 upper_bound: 57 repeats: 1\n"+ + "column:3 ndv:1 totColSize:72\n"+ + "num: 20 lower_bound: char upper_bound: char repeats: 18\n"+ + "index:1 ndv:20\n"+ + "num: 6 lower_bound: 3 upper_bound: 15 repeats: 1\n"+ + "num: 7 lower_bound: 18 upper_bound: 33 repeats: 1\n"+ + "num: 7 lower_bound: 39 upper_bound: 57 repeats: 1") // Test CM Sketch built from fast analyze. tk.MustExec("create table t1(a int, b int, index idx(a, b))")