From 3981e35016f6f90dfa14c9f42d0ea79fcc029ef5 Mon Sep 17 00:00:00 2001 From: Haibin Xie Date: Mon, 3 Sep 2018 14:13:15 +0800 Subject: [PATCH 1/2] stats: fix panic when log feedback info --- statistics/feedback.go | 10 +++++++--- statistics/update_test.go | 11 +++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/statistics/feedback.go b/statistics/feedback.go index 0e079010f8ff9..1ad32cd77304c 100644 --- a/statistics/feedback.go +++ b/statistics/feedback.go @@ -844,16 +844,20 @@ func logForIndex(prefix string, t *Table, idx *Index, ranges []*ranger.Range, ac } colName := idx.Info.Columns[rangePosition].Name.L // prefer index stats over column stats - if idxHist := t.indexStartWithColumnForDebugLog(colName); idxHist != nil { + if idxHist := t.indexStartWithColumnForDebugLog(colName); idxHist != nil && idxHist.Histogram.Len() > 0 { rangeString := logForIndexRange(idxHist, &rang, -1, factor) log.Debugf("%s index: %s, actual: %d, equality: %s, expected equality: %d, %s", prefix, idx.Info.Name.O, actual[i], equalityString, equalityCount, rangeString) - } else if colHist := t.columnByNameForDebugLog(colName); colHist != nil { + } else if colHist := t.columnByNameForDebugLog(colName); colHist != nil && colHist.Histogram.Len() > 0 { rangeString := colRangeToStr(colHist, &rang, -1, factor) log.Debugf("%s index: %s, actual: %d, equality: %s, expected equality: %d, %s", prefix, idx.Info.Name.O, actual[i], equalityString, equalityCount, rangeString) } else { - return + count, err := getPseudoRowCountByColumnRanges(sc, float64(t.Count), []*ranger.Range{&rang}, 0) + if err == nil { + log.Debugf("%s index: %s, actual: %d, equality: %s, expected equality: %d, range: %s, pseudo count: %.0f", prefix, idx.Info.Name.O, + actual[i], equalityString, equalityCount, rang.String(), count) + } } } } diff --git a/statistics/update_test.go b/statistics/update_test.go index f91c130952940..86611dd0e424a 100644 --- a/statistics/update_test.go +++ b/statistics/update_test.go @@ -869,23 +869,26 @@ func (s *testStatsUpdateSuite) TestLogDetailedInfo(c *C) { oriMinError := statistics.MinLogErrorRate oriLevel := log.GetLevel() oriBucketNum := executor.GetMaxBucketSizeForTest() + oriLease := s.do.StatsHandle().Lease defer func() { statistics.FeedbackProbability = oriProbability statistics.MinLogScanCount = oriMinLogCount statistics.MinLogErrorRate = oriMinError executor.SetMaxBucketSizeForTest(oriBucketNum) + s.do.StatsHandle().Lease = oriLease log.SetLevel(oriLevel) }() executor.SetMaxBucketSizeForTest(4) statistics.FeedbackProbability = 1 statistics.MinLogScanCount = 0 statistics.MinLogErrorRate = 0 + s.do.StatsHandle().Lease = 1 testKit := testkit.NewTestKit(c, s.store) testKit.MustExec("use test") - testKit.MustExec("create table t (a bigint(64), b bigint(64), primary key(a), index idx(b), index idx_ba(b,a))") + testKit.MustExec("create table t (a bigint(64), b bigint(64), c bigint(64), primary key(a), index idx(b), index idx_ba(b,a), index idx_bc(b,c))") for i := 0; i < 20; i++ { - testKit.MustExec(fmt.Sprintf("insert into t values (%d, %d)", i, i)) + testKit.MustExec(fmt.Sprintf("insert into t values (%d, %d, %d)", i, i, i)) } testKit.MustExec("analyze table t") tests := []struct { @@ -906,6 +909,10 @@ func (s *testStatsUpdateSuite) TestLogDetailedInfo(c *C) { sql: "select b from t use index(idx_ba) where b = 1 and a <= 5", result: "[stats-feedback] test.t, index: idx_ba, actual: 1, equality: 1, expected equality: 1, range: [-inf,6], actual: -1, expected: 6, buckets: {num: 8 lower_bound: 0 upper_bound: 7 repeats: 1}", }, + { + sql: "select b from t use index(idx_bc) where b = 1 and c <= 5", + result: "[stats-feedback] test.t, index: idx_bc, actual: 1, equality: 1, expected equality: 1, range: [-inf,6], pseudo count: 7", + }, { sql: "select b from t use index(idx_ba) where b = 1", result: "[stats-feedback] test.t, index: idx_ba, value: 1, actual: 1, expected: 1", From 629b976da63b21c58d175c2abaea9e3708e8f1df Mon Sep 17 00:00:00 2001 From: Haibin Xie Date: Mon, 3 Sep 2018 14:58:43 +0800 Subject: [PATCH 2/2] fix format --- statistics/update_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/statistics/update_test.go b/statistics/update_test.go index 86611dd0e424a..306c213bdf90f 100644 --- a/statistics/update_test.go +++ b/statistics/update_test.go @@ -910,7 +910,7 @@ func (s *testStatsUpdateSuite) TestLogDetailedInfo(c *C) { result: "[stats-feedback] test.t, index: idx_ba, actual: 1, equality: 1, expected equality: 1, range: [-inf,6], actual: -1, expected: 6, buckets: {num: 8 lower_bound: 0 upper_bound: 7 repeats: 1}", }, { - sql: "select b from t use index(idx_bc) where b = 1 and c <= 5", + sql: "select b from t use index(idx_bc) where b = 1 and c <= 5", result: "[stats-feedback] test.t, index: idx_bc, actual: 1, equality: 1, expected equality: 1, range: [-inf,6], pseudo count: 7", }, {