diff --git a/executor/analyze.go b/executor/analyze.go index 7158425d5a430..b0568abefd789 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 3216e294f5838..ab4ef000fde30 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -229,7 +229,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") @@ -241,12 +241,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") @@ -255,27 +255,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))")