Skip to content

Commit

Permalink
statistics: support dump/load correlation of histogram (#10573)
Browse files Browse the repository at this point in the history
  • Loading branch information
eurekaka authored and zz-jason committed May 23, 2019
1 parent abd013c commit 183648f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
29 changes: 29 additions & 0 deletions server/statistics_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (ds *testDumpStatsSuite) TestDumpStatsAPI(c *C) {
c.Assert(err, IsNil)
fp.Write(js)
ds.checkData(c, path)
ds.checkCorrelation(c)

// sleep for 1 seconds to ensure the existence of tidb.test
time.Sleep(time.Second)
Expand Down Expand Up @@ -179,6 +180,34 @@ func (ds *testDumpStatsSuite) prepare4DumpHistoryStats(c *C) {
dbt.mustExec("create table tidb.test (a int, b varchar(20))")
}

func (ds *testDumpStatsSuite) checkCorrelation(c *C) {
db, err := sql.Open("mysql", getDSN(nil))
c.Assert(err, IsNil, Commentf("Error connecting"))
dbt := &DBTest{c, db}
defer db.Close()

dbt.mustExec("use tidb")
rows := dbt.mustQuery("SELECT tidb_table_id FROM information_schema.tables WHERE table_name = 'test' AND table_schema = 'tidb'")
var tableID int64
if rows.Next() {
rows.Scan(&tableID)
dbt.Check(rows.Next(), IsFalse, Commentf("unexpected data"))
} else {
dbt.Error("no data")
}
rows.Close()
rows = dbt.mustQuery("select correlation from mysql.stats_histograms where table_id = ? and hist_id = 1 and is_index = 0", tableID)
if rows.Next() {
var corr float64
rows.Scan(&corr)
dbt.Check(corr, Equals, float64(1))
dbt.Check(rows.Next(), IsFalse, Commentf("unexpected data"))
} else {
dbt.Error("no data")
}
rows.Close()
}

func (ds *testDumpStatsSuite) checkData(c *C, path string) {
db, err := sql.Open("mysql", getDSN(func(config *mysql.Config) {
config.AllowAllFiles = true
Expand Down
6 changes: 4 additions & 2 deletions statistics/handle/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type jsonColumn struct {
NullCount int64 `json:"null_count"`
TotColSize int64 `json:"tot_col_size"`
LastUpdateVersion uint64 `json:"last_update_version"`
Correlation float64 `json:"correlation"`
}

func dumpJSONCol(hist *statistics.Histogram, CMSketch *statistics.CMSketch) *jsonColumn {
Expand All @@ -52,6 +53,7 @@ func dumpJSONCol(hist *statistics.Histogram, CMSketch *statistics.CMSketch) *jso
NullCount: hist.NullCount,
TotColSize: hist.TotColSize,
LastUpdateVersion: hist.LastUpdateVersion,
Correlation: hist.Correlation,
}
if CMSketch != nil {
jsonCol.CMSketch = statistics.CMSketchToProto(CMSketch)
Expand Down Expand Up @@ -191,7 +193,7 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
continue
}
hist := statistics.HistogramFromProto(jsonIdx.Histogram)
hist.ID, hist.NullCount, hist.LastUpdateVersion = idxInfo.ID, jsonIdx.NullCount, jsonIdx.LastUpdateVersion
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.Correlation = idxInfo.ID, jsonIdx.NullCount, jsonIdx.LastUpdateVersion, jsonIdx.Correlation
idx := &statistics.Index{
Histogram: *hist,
CMSketch: statistics.CMSketchFromProto(jsonIdx.CMSketch),
Expand All @@ -213,7 +215,7 @@ func TableStatsFromJSON(tableInfo *model.TableInfo, physicalID int64, jsonTbl *J
if err != nil {
return nil, errors.Trace(err)
}
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.TotColSize = colInfo.ID, jsonCol.NullCount, jsonCol.LastUpdateVersion, jsonCol.TotColSize
hist.ID, hist.NullCount, hist.LastUpdateVersion, hist.TotColSize, hist.Correlation = colInfo.ID, jsonCol.NullCount, jsonCol.LastUpdateVersion, jsonCol.TotColSize, jsonCol.Correlation
col := &statistics.Column{
PhysicalID: physicalID,
Histogram: *hist,
Expand Down
1 change: 1 addition & 0 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func (hg *Histogram) DecodeTo(tp *types.FieldType, timeZone *time.Location) erro
// ConvertTo converts the histogram bucket values into `Tp`.
func (hg *Histogram) ConvertTo(sc *stmtctx.StatementContext, tp *types.FieldType) (*Histogram, error) {
hist := NewHistogram(hg.ID, hg.NDV, hg.NullCount, hg.LastUpdateVersion, tp, hg.Len(), hg.TotColSize)
hist.Correlation = hg.Correlation
iter := chunk.NewIterator4Chunk(hg.Bounds)
for row := iter.Begin(); row != iter.End(); row = iter.Next() {
d := row.GetDatum(0, hg.Tp)
Expand Down

0 comments on commit 183648f

Please sign in to comment.