Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stats: cherry pick #5545, #5552 #5556

Merged
merged 2 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
stats: fix bug when estimating row count for outdated histograms (#5552)
  • Loading branch information
alivxxx authored and Haibin Xie committed Jan 4, 2018
commit 20973c966a5b76e9f7fe22a95c018e0ab83f69d8
30 changes: 30 additions & 0 deletions plan/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,36 @@ func (s *testAnalyzeSuite) TestAnalyze(c *C) {
}
}

func (s *testAnalyzeSuite) TestOutdatedAnalyze(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
testKit := testkit.NewTestKit(c, store)
defer func() {
dom.Close()
store.Close()
}()
testKit.MustExec("use test")
testKit.MustExec("create table t (a int, b int, index idx(a))")
for i := 0; i < 10; i++ {
testKit.MustExec(fmt.Sprintf("insert into t values (%d,%d)", i, i))
}
h := dom.StatsHandle()
h.DumpStatsDeltaToKV()
testKit.MustExec("analyze table t")
testKit.MustExec("insert into t select * from t")
testKit.MustExec("insert into t select * from t")
testKit.MustExec("insert into t select * from t")
h.DumpStatsDeltaToKV()
c.Assert(h.Update(dom.InfoSchema()), IsNil)
// FIXME: The count for table scan is wrong.
testKit.MustQuery("explain select * from t where a <= 5 and b <= 5").Check(testkit.Rows(
"TableScan_4 Selection_5 cop table:t, range:(-inf,+inf), keep order:false 28.799999999999997",
"Selection_5 TableScan_4 cop le(test.t.a, 5), le(test.t.b, 5) 28.799999999999997",
"TableReader_6 root data:Selection_5 28.799999999999997",
))
}

func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) {
store, err := tikv.NewMockTikvStore()
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,13 @@ func (c *Column) String() string {
}

// getIntColumnRowCount estimates the row count by a slice of IntColumnRange.
func (c *Column) getIntColumnRowCount(sc *variable.StatementContext, intRanges []types.IntColumnRange,
totalRowCount float64) (float64, error) {
func (c *Column) getIntColumnRowCount(sc *variable.StatementContext, intRanges []types.IntColumnRange) (float64, error) {
var rowCount float64
for _, rg := range intRanges {
var cnt float64
var err error
if rg.LowVal == math.MinInt64 && rg.HighVal == math.MaxInt64 {
cnt = totalRowCount
cnt = c.totalRowCount()
} else if rg.LowVal == math.MinInt64 {
cnt, err = c.lessAndEqRowCount(sc, types.NewIntDatum(rg.HighVal))
} else if rg.HighVal == math.MaxInt64 {
Expand All @@ -469,8 +468,8 @@ func (c *Column) getIntColumnRowCount(sc *variable.StatementContext, intRanges [
}
rowCount += cnt
}
if rowCount > totalRowCount {
rowCount = totalRowCount
if rowCount > c.totalRowCount() {
rowCount = c.totalRowCount()
}
return rowCount, nil
}
Expand Down
8 changes: 7 additions & 1 deletion statistics/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ func (s *testSelectivitySuite) TestSelectivity(c *C) {
c.Assert(sel, NotNil, comment)
ratio, err := statsTbl.Selectivity(ctx, sel.Conditions)
c.Assert(err, IsNil, comment)
c.Assert(math.Abs(ratio-tt.selectivity) < eps, IsTrue, comment)
c.Assert(math.Abs(ratio-tt.selectivity) < eps, IsTrue, Commentf("for %s, needed: %v, got: %v", tt.exprs, tt.selectivity, ratio))

statsTbl.Count *= 10
ratio, err = statsTbl.Selectivity(ctx, sel.Conditions)
c.Assert(err, IsNil, comment)
c.Assert(math.Abs(ratio-tt.selectivity) < eps, IsTrue, Commentf("for %s, needed: %v, got: %v", tt.exprs, tt.selectivity, ratio))
statsTbl.Count /= 10
}
}
5 changes: 5 additions & 0 deletions statistics/statistics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,11 @@ func (s *testStatisticsSuite) TestIntColumnRanges(c *C) {
count, err = tbl.GetRowCountByIntColumnRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 1)

tbl.Count *= 10
count, err = tbl.GetRowCountByIntColumnRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 10)
}

func (s *testStatisticsSuite) TestIndexRanges(c *C) {
Expand Down
8 changes: 6 additions & 2 deletions statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ func (t *Table) GetRowCountByIntColumnRanges(sc *variable.StatementContext, colI
if t.Pseudo || c == nil || len(c.Buckets) == 0 {
return getPseudoRowCountByIntRanges(intRanges, float64(t.Count)), nil
}
return c.getIntColumnRowCount(sc, intRanges, float64(t.Count))
result, err := c.getIntColumnRowCount(sc, intRanges)
result *= c.getIncreaseFactor(t.Count)
return result, errors.Trace(err)
}

// GetRowCountByColumnRanges estimates the row count by a slice of ColumnRange.
Expand All @@ -223,7 +225,9 @@ func (t *Table) GetRowCountByColumnRanges(sc *variable.StatementContext, colID i
if t.Pseudo || c == nil || len(c.Buckets) == 0 {
return getPseudoRowCountByColumnRanges(sc, float64(t.Count), colRanges)
}
return c.getColumnRowCount(sc, colRanges)
result, err := c.getColumnRowCount(sc, colRanges)
result *= c.getIncreaseFactor(t.Count)
return result, errors.Trace(err)
}

// GetRowCountByIndexRanges estimates the row count by a slice of IndexRange.
Expand Down