Skip to content

Commit

Permalink
stats: fix selectivity estimation for primary key (#8134) (#8150)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and zz-jason committed Nov 2, 2018
1 parent a168d43 commit e04bba1
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion statistics/boostrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func initStatsHistograms4Chunk(is infoschema.InfoSchema, tables statsCache, iter
continue
}
hist := NewHistogram(id, ndv, nullCount, version, &colInfo.FieldType, 0, totColSize)
table.Columns[hist.ID] = &Column{Histogram: *hist, Info: colInfo, Count: nullCount}
table.Columns[hist.ID] = &Column{Histogram: *hist, Info: colInfo, Count: nullCount, isHandle: tbl.Meta().PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag)}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions statistics/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func (h *Handle) LoadStatsFromJSON(tableInfo *model.TableInfo, jsonTbl *JSONTabl
CMSketch: CMSketchFromProto(jsonCol.CMSketch),
Info: colInfo,
Count: count,
isHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
}
tbl.Columns[col.ID] = col
}
Expand Down
2 changes: 1 addition & 1 deletion statistics/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (h *Handle) LoadNeededHistograms() error {
if err != nil {
return errors.Trace(err)
}
tbl.Columns[c.ID] = &Column{Histogram: *hg, Info: c.Info, CMSketch: cms, Count: int64(hg.totalRowCount())}
tbl.Columns[c.ID] = &Column{Histogram: *hg, Info: c.Info, CMSketch: cms, Count: int64(hg.totalRowCount()), isHandle: c.isHandle}
h.UpdateTableStats([]*Table{tbl}, nil)
histogramNeededColumns.delete(col)
}
Expand Down
5 changes: 3 additions & 2 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,9 @@ func (hg *Histogram) outOfRange(val types.Datum) bool {
type Column struct {
Histogram
*CMSketch
Count int64
Info *model.ColumnInfo
Count int64
Info *model.ColumnInfo
isHandle bool
}

func (c *Column) String() string {
Expand Down
2 changes: 1 addition & 1 deletion statistics/selectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (t *Table) Selectivity(ctx sessionctx.Context, exprs []expression.Expressio
return 0, errors.Trace(err)
}
sets = append(sets, &exprSet{tp: colType, ID: col.ID, mask: maskCovered, ranges: ranges})
if mysql.HasPriKeyFlag(colInfo.Info.Flag) {
if colInfo.isHandle {
sets[len(sets)-1].tp = pkType
}
}
Expand Down
17 changes: 17 additions & 0 deletions statistics/selectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,23 @@ func (s *testSelectivitySuite) TestEstimationForUnknownValues(c *C) {
c.Assert(count, Equals, 0.0)
}

func (s *testSelectivitySuite) TestPrimaryKeySelectivity(c *C) {
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t")
testKit.MustExec("create table t(a char(10) primary key, b int)")
testKit.MustQuery(`explain select * from t where a > "t"`).Check(testkit.Rows(
"IndexScan_8 cop table:t, index:a, range:(t,+inf], keep order:false 3333.33",
"TableScan_9 cop table:t, keep order:false 3333.33",
"IndexLookUp_10 root index:IndexScan_8, table:TableScan_9 3333.33"))

testKit.MustExec("drop table t")
testKit.MustExec("create table t(a int primary key, b int)")
testKit.MustQuery(`explain select * from t where a > 1`).Check(testkit.Rows(
"TableScan_5 cop table:t, range:(1,+inf], keep order:false 3333.33",
"TableReader_6 root data:TableScan_5 3333.33"))
}

func BenchmarkSelectivity(b *testing.B) {
c := &C{}
s := &testSelectivitySuite{}
Expand Down
8 changes: 5 additions & 3 deletions statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ func (h *Handle) columnStatsFromStorage(row types.Row, table *Table, tableInfo *
LastUpdateVersion: histVer,
TotColSize: totColSize,
},
Info: colInfo,
Count: count + nullCount,
Info: colInfo,
Count: count + nullCount,
isHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
}
break
}
Expand All @@ -167,6 +168,7 @@ func (h *Handle) columnStatsFromStorage(row types.Row, table *Table, tableInfo *
Info: colInfo,
CMSketch: cms,
Count: int64(hg.totalRowCount()),
isHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
}
}
break
Expand Down Expand Up @@ -372,7 +374,7 @@ func PseudoTable(tblInfo *model.TableInfo) *Table {
}
for _, col := range tblInfo.Columns {
if col.State == model.StatePublic {
t.Columns[col.ID] = &Column{Info: col}
t.Columns[col.ID] = &Column{Info: col, isHandle: tblInfo.PKIsHandle && mysql.HasPriKeyFlag(col.Flag)}
}
}
for _, idx := range tblInfo.Indices {
Expand Down

0 comments on commit e04bba1

Please sign in to comment.