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: fix selectivity estimation for primary key (#8134) #8149

Merged
merged 3 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion statistics/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, tables stat
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 @@ -216,6 +216,7 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
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 @@ -258,7 +258,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 @@ -720,8 +720,9 @@ func (e *ErrorRate) merge(rate *ErrorRate) {
type Column struct {
Histogram
*CMSketch
Count int64
Info *model.ColumnInfo
Count int64
Info *model.ColumnInfo
isHandle bool
ErrorRate
}

Expand Down
2 changes: 1 addition & 1 deletion statistics/selectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (coll *HistColl) Selectivity(ctx sessionctx.Context, exprs []expression.Exp
return 0, errors.Trace(err)
}
sets = append(sets, &exprSet{tp: colType, ID: id, mask: maskCovered, ranges: ranges, numCols: 1})
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 @@ -324,6 +324,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(
"IndexLookUp_10 3333.33 root ",
"├─IndexScan_8 3333.33 cop table:t, index:a, range:(\"t\",+inf], keep order:false, stats:pseudo",
"└─TableScan_9 3333.33 cop table:t, keep order:false, stats:pseudo"))

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(
"TableReader_6 3333.33 root data:TableScan_5",
"└─TableScan_5 3333.33 cop table:t, range:(1,+inf], keep order:false, stats:pseudo"))
}

func BenchmarkSelectivity(b *testing.B) {
c := &C{}
s := &testSelectivitySuite{}
Expand Down
4 changes: 3 additions & 1 deletion statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func (h *Handle) columnStatsFromStorage(row chunk.Row, table *Table, tableInfo *
Info: colInfo,
Count: count + nullCount,
ErrorRate: errorRate,
isHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
}
break
}
Expand All @@ -206,6 +207,7 @@ func (h *Handle) columnStatsFromStorage(row chunk.Row, table *Table, tableInfo *
CMSketch: cms,
Count: int64(hg.totalRowCount()),
ErrorRate: errorRate,
isHandle: tableInfo.PKIsHandle && mysql.HasPriKeyFlag(colInfo.Flag),
}
break
}
Expand Down Expand Up @@ -610,7 +612,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