Skip to content

Commit

Permalink
stats: fix estimation in index point query (#4959)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and coocood committed Oct 31, 2017
1 parent 08bd8c5 commit 943d77b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
16 changes: 13 additions & 3 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,23 @@ func (idx *Index) getRowCount(sc *variable.StatementContext, indexRanges []*type
if err != nil {
return 0, errors.Trace(err)
}
if indexRange.LowExclude {
lb = append(lb, 0)
}
rb, err := codec.EncodeKey(nil, indexRange.HighVal...)
if err != nil {
return 0, errors.Trace(err)
}
if string(lb) == string(rb) {
if !indexRange.LowExclude && !indexRange.HighExclude {
rowCount, err := idx.equalRowCount(sc, types.NewBytesDatum(lb))
if err != nil {
return 0, errors.Trace(err)
}
totalCount += rowCount
}
continue
}
if indexRange.LowExclude {
lb = append(lb, 0)
}
if !indexRange.HighExclude {
rb = append(rb, 0)
}
Expand Down
62 changes: 62 additions & 0 deletions statistics/statistics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,65 @@ func (s *testStatisticsSuite) TestIntColumnRanges(c *C) {
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 1)
}

func (s *testStatisticsSuite) TestIndexRanges(c *C) {
bucketCount := int64(256)
ctx := mock.NewContext()
sc := ctx.GetSessionVars().StmtCtx

s.rc.(*recordSet).cursor = 0
rowCount, hg, err := BuildIndex(ctx, bucketCount, 0, s.rc)
calculateScalar(hg)
c.Check(err, IsNil)
c.Check(rowCount, Equals, int64(100000))
idxInfo := &model.IndexInfo{Columns: []*model.IndexColumn{{Offset: 0}}}
idx := &Index{Histogram: *hg, Info: idxInfo}
tbl := &Table{
Count: int64(idx.totalRowCount()),
Indices: make(map[int64]*Index),
}
ran := []*types.IndexRange{{
LowVal: []types.Datum{types.MinNotNullDatum()},
HighVal: []types.Datum{types.MaxValueDatum()},
}}
count, err := tbl.GetRowCountByIndexRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 99900)
ran[0].LowVal[0] = types.NewIntDatum(1000)
ran[0].HighVal[0] = types.NewIntDatum(2000)
count, err = tbl.GetRowCountByIndexRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 2500)
ran[0].LowVal[0] = types.NewIntDatum(1001)
ran[0].HighVal[0] = types.NewIntDatum(1999)
count, err = tbl.GetRowCountByIndexRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 2500)
ran[0].LowVal[0] = types.NewIntDatum(1000)
ran[0].HighVal[0] = types.NewIntDatum(1000)
count, err = tbl.GetRowCountByIndexRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 100)

tbl.Indices[0] = idx
ran[0].LowVal[0] = types.MinNotNullDatum()
ran[0].HighVal[0] = types.MaxValueDatum()
count, err = tbl.GetRowCountByIndexRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 100000)
ran[0].LowVal[0] = types.NewIntDatum(1000)
ran[0].HighVal[0] = types.NewIntDatum(2000)
count, err = tbl.GetRowCountByIndexRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 999)
ran[0].LowVal[0] = types.NewIntDatum(1001)
ran[0].HighVal[0] = types.NewIntDatum(1990)
count, err = tbl.GetRowCountByIndexRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 988)
ran[0].LowVal[0] = types.NewIntDatum(1000)
ran[0].HighVal[0] = types.NewIntDatum(1000)
count, err = tbl.GetRowCountByIndexRanges(sc, 0, ran)
c.Assert(err, IsNil)
c.Assert(int(count), Equals, 1)
}

0 comments on commit 943d77b

Please sign in to comment.