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

statistics: add bucket ndv for index histogram #20580

Merged
merged 23 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
425d39e
add bucket ndv for index histogram
winoros Aug 20, 2020
b4bdd72
address comments
winoros Oct 26, 2020
1430708
Merge branch 'master' into bucket-ndv-index-hist
winoros Nov 3, 2020
8655fad
Merge branch 'master' into bucket-ndv-index-hist
winoros Dec 14, 2020
7e6638e
add version control and make feedback collect work again
winoros Dec 14, 2020
05d588b
fix ndv when extract topn out of hist
winoros Dec 15, 2020
7326222
address comments and fix
winoros Dec 21, 2020
3e0fa73
Merge branch 'master' into bucket-ndv-index-hist
winoros Dec 21, 2020
da25722
address comments
winoros Dec 23, 2020
1bf0175
fix tests and adderss comments
winoros Dec 23, 2020
878b5ca
fix go mod tidy
winoros Dec 23, 2020
f3a4523
Merge branch 'master' into bucket-ndv-index-hist
winoros Dec 23, 2020
78209d3
Merge branch 'master' into bucket-ndv-index-hist
winoros Dec 24, 2020
a48e59e
Merge branch 'master' into bucket-ndv-index-hist
qw4990 Dec 24, 2020
c4809ea
Merge branch 'master' into bucket-ndv-index-hist
winoros Dec 29, 2020
48cc1bd
address comments
winoros Dec 29, 2020
ac554e0
Merge branch 'bucket-ndv-index-hist' of https://github.com/winoros/ti…
winoros Dec 29, 2020
c36ab62
Merge branch 'master' into bucket-ndv-index-hist
qw4990 Dec 30, 2020
e5f83c6
Merge branch 'master' into bucket-ndv-index-hist
winoros Jan 5, 2021
6fa4644
Merge branch 'master' into bucket-ndv-index-hist
winoros Jan 7, 2021
2fbc952
Merge branch 'master' into bucket-ndv-index-hist
ti-srebot Jan 7, 2021
566c704
Merge branch 'master' into bucket-ndv-index-hist
winoros Jan 12, 2021
12c5ccd
Merge branch 'master' into bucket-ndv-index-hist
winoros Jan 13, 2021
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 cmd/explaintest/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ if [ "${TIDB_TEST_STORE_NAME}" = "tikv" ]; then
$tidb_server -P "$port" -status "$status" -config config.toml -store tikv -path "${TIKV_PATH}" > $explain_test_log 2>&1 &
SERVER_PID=$!
else
$tidb_server -P "$port" -status "$status" -config config.toml -store mocktikv -path "" > $explain_test_log 2>&1 &
$tidb_server -P "$port" -status "$status" -config config.toml -store unistore -path "" > $explain_test_log 2>&1 &
SERVER_PID=$!
fi
echo "tidb-server(PID: $SERVER_PID) started"
Expand Down
3 changes: 1 addition & 2 deletions distsql/select_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ func (r *selectResult) fetchResp(ctx context.Context) error {
sc.AppendWarning(dbterror.ClassTiKV.Synthesize(terror.ErrCode(warning.Code), warning.Msg))
}
if r.feedback != nil {
r.feedback.Update(resultSubset.GetStartKey(), r.selectResp.OutputCounts)
r.feedback.Update(resultSubset.GetStartKey(), r.selectResp.OutputCounts, r.selectResp.Ndvs)
}

r.partialCount++

hasStats, ok := resultSubset.(CopRuntimeStats)
Expand Down
2 changes: 1 addition & 1 deletion distsql/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (r *streamResult) readDataFromResponse(ctx context.Context, resp kv.Respons
if err != nil {
return false, errors.Trace(err)
}
r.feedback.Update(resultSubset.GetStartKey(), stream.OutputCounts)
r.feedback.Update(resultSubset.GetStartKey(), stream.OutputCounts, stream.Ndvs)
r.partialCount++

hasStats, ok := resultSubset.(CopRuntimeStats)
Expand Down
12 changes: 8 additions & 4 deletions executor/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ func (e *AnalyzeIndexExec) buildStatsFromResult(result distsql.SelectResult, nee
cms = statistics.NewCMSketch(int32(e.opts[ast.AnalyzeOptCMSketchDepth]), int32(e.opts[ast.AnalyzeOptCMSketchWidth]))
topn = statistics.NewTopN(int(e.opts[ast.AnalyzeOptNumTopN]))
}
statsVer := statistics.Version1
if e.analyzePB.IdxReq.Version != nil {
statsVer = int(*e.analyzePB.IdxReq.Version)
}
for {
data, err := result.NextRaw(context.TODO())
if err != nil {
Expand All @@ -350,7 +354,7 @@ func (e *AnalyzeIndexExec) buildStatsFromResult(result distsql.SelectResult, nee
}
respHist := statistics.HistogramFromProto(resp.Hist)
e.job.Update(int64(respHist.TotalRowCount()))
hist, err = statistics.MergeHistograms(e.ctx.GetSessionVars().StmtCtx, hist, respHist, int(e.opts[ast.AnalyzeOptNumBuckets]))
hist, err = statistics.MergeHistograms(e.ctx.GetSessionVars().StmtCtx, hist, respHist, int(e.opts[ast.AnalyzeOptNumBuckets]), statsVer)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -556,7 +560,7 @@ func (e *AnalyzeColumnsExec) buildStats(ranges []*ranger.Range, needExtStats boo
if hasPkHist(e.handleCols) {
respHist := statistics.HistogramFromProto(resp.PkHist)
rowCount = int64(respHist.TotalRowCount())
pkHist, err = statistics.MergeHistograms(sc, pkHist, respHist, int(e.opts[ast.AnalyzeOptNumBuckets]))
pkHist, err = statistics.MergeHistograms(sc, pkHist, respHist, int(e.opts[ast.AnalyzeOptNumBuckets]), statistics.Version1)
if err != nil {
return nil, nil, nil, nil, err
}
Expand Down Expand Up @@ -1244,7 +1248,7 @@ func analyzeIndexIncremental(idxExec *analyzeIndexIncrementalExec) analyzeResult
if err != nil {
return analyzeResult{Err: err, job: idxExec.job}
}
hist, err = statistics.MergeHistograms(idxExec.ctx.GetSessionVars().StmtCtx, idxExec.oldHist, hist, int(idxExec.opts[ast.AnalyzeOptNumBuckets]))
hist, err = statistics.MergeHistograms(idxExec.ctx.GetSessionVars().StmtCtx, idxExec.oldHist, hist, int(idxExec.opts[ast.AnalyzeOptNumBuckets]), statistics.Version1)
if err != nil {
return analyzeResult{Err: err, job: idxExec.job}
}
Expand Down Expand Up @@ -1295,7 +1299,7 @@ func analyzePKIncremental(colExec *analyzePKIncrementalExec) analyzeResult {
return analyzeResult{Err: err, job: colExec.job}
}
hist := hists[0]
hist, err = statistics.MergeHistograms(colExec.ctx.GetSessionVars().StmtCtx, colExec.oldHist, hist, int(colExec.opts[ast.AnalyzeOptNumBuckets]))
hist, err = statistics.MergeHistograms(colExec.ctx.GetSessionVars().StmtCtx, colExec.oldHist, hist, int(colExec.opts[ast.AnalyzeOptNumBuckets]), statistics.Version1)
if err != nil {
return analyzeResult{Err: err, job: colExec.job}
}
Expand Down
97 changes: 49 additions & 48 deletions executor/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ func (s *testFastAnalyze) TestFastAnalyze(c *C) {
tk.MustExec("insert into t2 values (0), (18446744073709551615)")
tk.MustExec("analyze table t2")
tk.MustQuery("show stats_buckets where table_name = 't2'").Check(testkit.Rows(
"test t2 a 0 0 1 1 0 0",
"test t2 a 0 1 2 1 18446744073709551615 18446744073709551615"))
"test t2 a 0 0 1 1 0 0 0",
"test t2 a 0 1 2 1 18446744073709551615 18446744073709551615 0"))

tk.MustExec(`set @@tidb_partition_prune_mode='` + string(variable.StaticOnly) + `'`)
tk.MustExec(`create table t3 (id int, v int, primary key(id), index k(v)) partition by hash (id) partitions 4`)
Expand Down Expand Up @@ -531,6 +531,7 @@ func (s *testSuite1) TestAnalyzeIncremental(c *C) {
}

func (s *testSuite1) TestAnalyzeIncrementalStreaming(c *C) {
c.Skip("unistore hasn't support streaming yet.")
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.Se.GetSessionVars().EnableStreaming = true
Expand All @@ -545,13 +546,13 @@ func (s *testSuite1) testAnalyzeIncremental(tk *testkit.TestKit, c *C) {
tk.MustQuery("show stats_buckets").Check(testkit.Rows())
tk.MustExec("insert into t values (1,1)")
tk.MustExec("analyze incremental table t index")
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1", "test t idx 1 0 1 1 1 1"))
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1 0", "test t idx 1 0 1 1 1 1 0"))
tk.MustExec("insert into t values (2,2)")
tk.MustExec("analyze incremental table t index")
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1", "test t a 0 1 2 1 2 2", "test t idx 1 0 1 1 1 1", "test t idx 1 1 2 1 2 2"))
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1 0", "test t a 0 1 2 1 2 2 0", "test t idx 1 0 1 1 1 1 0", "test t idx 1 1 2 1 2 2 0"))
tk.MustExec("analyze incremental table t index")
// Result should not change.
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1", "test t a 0 1 2 1 2 2", "test t idx 1 0 1 1 1 1", "test t idx 1 1 2 1 2 2"))
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1 0", "test t a 0 1 2 1 2 2 0", "test t idx 1 0 1 1 1 1 0", "test t idx 1 1 2 1 2 2 0"))

// Test analyze incremental with feedback.
tk.MustExec("insert into t values (3,3)")
Expand All @@ -574,7 +575,7 @@ func (s *testSuite1) testAnalyzeIncremental(tk *testkit.TestKit, c *C) {
c.Assert(h.DumpStatsFeedbackToKV(), IsNil)
c.Assert(h.HandleUpdateStats(is), IsNil)
c.Assert(h.Update(is), IsNil)
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1", "test t a 0 1 3 0 2 2147483647", "test t idx 1 0 1 1 1 1", "test t idx 1 1 2 1 2 2"))
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1 0", "test t a 0 1 3 0 2 2147483647 0", "test t idx 1 0 1 1 1 1 0", "test t idx 1 1 2 1 2 2 0"))
tblStats := h.GetTableStats(tblInfo)
val, err := codec.EncodeKey(tk.Se.GetSessionVars().StmtCtx, nil, types.NewIntDatum(3))
c.Assert(err, IsNil)
Expand All @@ -583,8 +584,8 @@ func (s *testSuite1) testAnalyzeIncremental(tk *testkit.TestKit, c *C) {
c.Assert(statistics.IsAnalyzed(tblStats.Columns[tblInfo.Columns[0].ID].Flag), IsFalse)

tk.MustExec("analyze incremental table t index")
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1", "test t a 0 1 2 1 2 2", "test t a 0 2 3 1 3 3",
"test t idx 1 0 1 1 1 1", "test t idx 1 1 2 1 2 2", "test t idx 1 2 3 1 3 3"))
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t a 0 0 1 1 1 1 0", "test t a 0 1 2 1 2 2 0", "test t a 0 2 3 1 3 3 0",
"test t idx 1 0 1 1 1 1 0", "test t idx 1 1 2 1 2 2 0", "test t idx 1 2 3 1 3 3 0"))
tblStats = h.GetTableStats(tblInfo)
c.Assert(tblStats.Indices[tblInfo.Indices[0].ID].QueryBytes(val), Equals, uint64(1))
}
Expand Down Expand Up @@ -769,36 +770,36 @@ func (s *testSuite1) TestNormalAnalyzeOnCommonHandle(c *C) {
tk.MustExec("analyze table t1, t2, t3")

tk.MustQuery(`show stats_buckets where table_name in ("t1", "t2", "t3")`).Sort().Check(testkit.Rows(
"test t1 a 0 0 1 1 1 1",
"test t1 a 0 1 2 1 2 2",
"test t1 a 0 2 3 1 3 3",
"test t1 b 0 0 1 1 1 1",
"test t1 b 0 1 2 1 2 2",
"test t1 b 0 2 3 1 3 3",
"test t2 PRIMARY 1 0 1 1 111 111",
"test t2 PRIMARY 1 1 2 1 222 222",
"test t2 PRIMARY 1 2 3 1 333 333",
"test t2 a 0 0 1 1 111 111",
"test t2 a 0 1 2 1 222 222",
"test t2 a 0 2 3 1 333 333",
"test t2 b 0 0 1 1 1 1",
"test t2 b 0 1 2 1 2 2",
"test t2 b 0 2 3 1 3 3",
"test t3 PRIMARY 1 0 1 1 (1, 1) (1, 1)",
"test t3 PRIMARY 1 1 2 1 (2, 2) (2, 2)",
"test t3 PRIMARY 1 2 3 1 (3, 3) (3, 3)",
"test t3 a 0 0 1 1 1 1",
"test t3 a 0 1 2 1 2 2",
"test t3 a 0 2 3 1 3 3",
"test t3 b 0 0 1 1 1 1",
"test t3 b 0 1 2 1 2 2",
"test t3 b 0 2 3 1 3 3",
"test t3 c 0 0 1 1 1 1",
"test t3 c 0 1 2 1 2 2",
"test t3 c 0 2 3 1 3 3",
"test t3 c 1 0 1 1 1 1",
"test t3 c 1 1 2 1 2 2",
"test t3 c 1 2 3 1 3 3"))
"test t1 a 0 0 1 1 1 1 0",
"test t1 a 0 1 2 1 2 2 0",
"test t1 a 0 2 3 1 3 3 0",
"test t1 b 0 0 1 1 1 1 0",
"test t1 b 0 1 2 1 2 2 0",
"test t1 b 0 2 3 1 3 3 0",
"test t2 PRIMARY 1 0 1 1 111 111 0",
"test t2 PRIMARY 1 1 2 1 222 222 0",
"test t2 PRIMARY 1 2 3 1 333 333 0",
"test t2 a 0 0 1 1 111 111 0",
"test t2 a 0 1 2 1 222 222 0",
"test t2 a 0 2 3 1 333 333 0",
"test t2 b 0 0 1 1 1 1 0",
"test t2 b 0 1 2 1 2 2 0",
"test t2 b 0 2 3 1 3 3 0",
"test t3 PRIMARY 1 0 1 1 (1, 1) (1, 1) 0",
"test t3 PRIMARY 1 1 2 1 (2, 2) (2, 2) 0",
"test t3 PRIMARY 1 2 3 1 (3, 3) (3, 3) 0",
"test t3 a 0 0 1 1 1 1 0",
"test t3 a 0 1 2 1 2 2 0",
"test t3 a 0 2 3 1 3 3 0",
"test t3 b 0 0 1 1 1 1 0",
"test t3 b 0 1 2 1 2 2 0",
"test t3 b 0 2 3 1 3 3 0",
"test t3 c 0 0 1 1 1 1 0",
"test t3 c 0 1 2 1 2 2 0",
"test t3 c 0 2 3 1 3 3 0",
"test t3 c 1 0 1 1 1 1 0",
"test t3 c 1 1 2 1 2 2 0",
"test t3 c 1 2 3 1 3 3 0"))
}

func (s *testSuite1) TestDefaultValForAnalyze(c *C) {
Expand Down Expand Up @@ -846,15 +847,15 @@ func (s *testSerialSuite2) TestIssue20874(c *C) {
tk.MustExec("insert into t values ('#', 'C'), ('$', 'c'), ('a', 'a')")
tk.MustExec("analyze table t")
tk.MustQuery("show stats_buckets where db_name = 'test' and table_name = 't'").Sort().Check(testkit.Rows(
"test t a 0 0 1 1 \x02\xd2 \x02\xd2",
"test t a 0 1 2 1 \x0e\x0f \x0e\x0f",
"test t a 0 2 3 1 \x0e3 \x0e3",
"test t b 0 0 1 1 \x00A \x00A",
"test t b 0 1 3 2 \x00C \x00C",
"test t idxa 1 0 1 1 \x02\xd2 \x02\xd2",
"test t idxa 1 1 2 1 \x0e\x0f \x0e\x0f",
"test t idxa 1 2 3 1 \x0e3 \x0e3",
"test t idxb 1 0 1 1 \x00A \x00A",
"test t idxb 1 1 3 2 \x00C \x00C",
"test t a 0 0 1 1 \x02\xd2 \x02\xd2 0",
"test t a 0 1 2 1 \x0e\x0f \x0e\x0f 0",
"test t a 0 2 3 1 \x0e3 \x0e3 0",
"test t b 0 0 1 1 \x00A \x00A 0",
"test t b 0 1 3 2 \x00C \x00C 0",
"test t idxa 1 0 1 1 \x02\xd2 \x02\xd2 0",
"test t idxa 1 1 2 1 \x0e\x0f \x0e\x0f 0",
"test t idxa 1 2 3 1 \x0e3 \x0e3 0",
"test t idxb 1 0 1 1 \x00A \x00A 0",
"test t idxb 1 1 3 2 \x00C \x00C 0",
))
}
1 change: 1 addition & 0 deletions executor/show_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func (e *ShowExec) bucketsToRows(dbName, tblName, partitionName, colName string,
hist.Buckets[i].Repeat,
lowerBoundStr,
upperBoundStr,
hist.Buckets[i].NDV,
})
}
return nil
Expand Down
24 changes: 12 additions & 12 deletions executor/show_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,36 @@ func (s *testShowStatsSuite) TestShowStatsBuckets(c *C) {
tk.MustExec("insert into t values (1,1)")
tk.MustExec("analyze table t")
result := tk.MustQuery("show stats_buckets").Sort()
result.Check(testkit.Rows("test t a 0 0 1 1 1 1", "test t b 0 0 1 1 1 1", "test t idx 1 0 1 1 (1, 1) (1, 1)"))
result.Check(testkit.Rows("test t a 0 0 1 1 1 1 0", "test t b 0 0 1 1 1 1 0", "test t idx 1 0 1 1 (1, 1) (1, 1) 0"))
result = tk.MustQuery("show stats_buckets where column_name = 'idx'")
result.Check(testkit.Rows("test t idx 1 0 1 1 (1, 1) (1, 1)"))
result.Check(testkit.Rows("test t idx 1 0 1 1 (1, 1) (1, 1) 0"))

tk.MustExec("drop table t")
tk.MustExec("create table t (`a` datetime, `b` int, key `idx`(`a`, `b`))")
tk.MustExec("insert into t values (\"2020-01-01\", 1)")
tk.MustExec("analyze table t")
result = tk.MustQuery("show stats_buckets").Sort()
result.Check(testkit.Rows("test t a 0 0 1 1 2020-01-01 00:00:00 2020-01-01 00:00:00", "test t b 0 0 1 1 1 1", "test t idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1)"))
result.Check(testkit.Rows("test t a 0 0 1 1 2020-01-01 00:00:00 2020-01-01 00:00:00 0", "test t b 0 0 1 1 1 1 0", "test t idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1) 0"))
result = tk.MustQuery("show stats_buckets where column_name = 'idx'")
result.Check(testkit.Rows("test t idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1)"))
result.Check(testkit.Rows("test t idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1) 0"))

tk.MustExec("drop table t")
tk.MustExec("create table t (`a` date, `b` int, key `idx`(`a`, `b`))")
tk.MustExec("insert into t values (\"2020-01-01\", 1)")
tk.MustExec("analyze table t")
result = tk.MustQuery("show stats_buckets").Sort()
result.Check(testkit.Rows("test t a 0 0 1 1 2020-01-01 2020-01-01", "test t b 0 0 1 1 1 1", "test t idx 1 0 1 1 (2020-01-01, 1) (2020-01-01, 1)"))
result.Check(testkit.Rows("test t a 0 0 1 1 2020-01-01 2020-01-01 0", "test t b 0 0 1 1 1 1 0", "test t idx 1 0 1 1 (2020-01-01, 1) (2020-01-01, 1) 0"))
result = tk.MustQuery("show stats_buckets where column_name = 'idx'")
result.Check(testkit.Rows("test t idx 1 0 1 1 (2020-01-01, 1) (2020-01-01, 1)"))
result.Check(testkit.Rows("test t idx 1 0 1 1 (2020-01-01, 1) (2020-01-01, 1) 0"))

tk.MustExec("drop table t")
tk.MustExec("create table t (`a` timestamp, `b` int, key `idx`(`a`, `b`))")
tk.MustExec("insert into t values (\"2020-01-01\", 1)")
tk.MustExec("analyze table t")
result = tk.MustQuery("show stats_buckets").Sort()
result.Check(testkit.Rows("test t a 0 0 1 1 2020-01-01 00:00:00 2020-01-01 00:00:00", "test t b 0 0 1 1 1 1", "test t idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1)"))
result.Check(testkit.Rows("test t a 0 0 1 1 2020-01-01 00:00:00 2020-01-01 00:00:00 0", "test t b 0 0 1 1 1 1 0", "test t idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1) 0"))
result = tk.MustQuery("show stats_buckets where column_name = 'idx'")
result.Check(testkit.Rows("test t idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1)"))
result.Check(testkit.Rows("test t idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1) 0"))
}

func (s *testShowStatsSuite) TestShowStatsHasNullValue(c *C) {
Expand All @@ -124,14 +124,14 @@ func (s *testShowStatsSuite) TestShowStatsHasNullValue(c *C) {
tk.MustExec("insert into t values(1)")
tk.MustExec("analyze table t")
tk.MustQuery("show stats_buckets").Sort().Check(testkit.Rows(
"test t a 0 0 1 1 1 1",
"test t idx 1 0 1 1 1 1",
"test t a 0 0 1 1 1 1 0",
"test t idx 1 0 1 1 1 1 0",
))
tk.MustExec("drop table t")
tk.MustExec("create table t (a int, b int, index idx(a, b))")
tk.MustExec("insert into t values(NULL, NULL)")
tk.MustExec("analyze table t")
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t idx 1 0 1 1 (NULL, NULL) (NULL, NULL)"))
tk.MustQuery("show stats_buckets").Check(testkit.Rows("test t idx 1 0 1 1 (NULL, NULL) (NULL, NULL) 0"))

tk.MustExec("drop table t")
tk.MustExec("create table t(a int, b int, c int, index idx_b(b), index idx_c_a(c, a))")
Expand Down Expand Up @@ -201,7 +201,7 @@ func (s *testShowStatsSuite) TestShowPartitionStats(c *C) {
c.Assert(result.Rows()[2][3], Equals, "idx")

result = tk.MustQuery("show stats_buckets").Sort()
result.Check(testkit.Rows("test t p0 a 0 0 1 1 1 1", "test t p0 b 0 0 1 1 1 1", "test t p0 idx 1 0 1 1 1 1"))
result.Check(testkit.Rows("test t p0 a 0 0 1 1 1 1 0", "test t p0 b 0 0 1 1 1 1 0", "test t p0 idx 1 0 1 1 1 1 0"))

result = tk.MustQuery("show stats_healthy")
result.Check(testkit.Rows("test t p0 100"))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ require (
github.com/pingcap/sysutil v0.0.0-20201130064824-f0c8aa6a6966
github.com/pingcap/tidb-lightning v4.0.9-0.20201106041742-a1ac97827a27+incompatible
github.com/pingcap/tidb-tools v4.0.9-0.20201127090955-2707c97b3853+incompatible
github.com/pingcap/tipb v0.0.0-20201209065231-aa39b1b86217
github.com/pingcap/tipb v0.0.0-20201215091753-bd0cb2b314a4
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ github.com/pingcap/tidb-tools v4.0.9-0.20201127090955-2707c97b3853+incompatible
github.com/pingcap/tidb-tools v4.0.9-0.20201127090955-2707c97b3853+incompatible/go.mod h1:XGdcy9+yqlDSEMTpOXnwf3hiTeqrV6MN/u1se9N8yIM=
github.com/pingcap/tipb v0.0.0-20190428032612-535e1abaa330/go.mod h1:RtkHW8WbcNxj8lsbzjaILci01CtYnYbIkQhjyZWrWVI=
github.com/pingcap/tipb v0.0.0-20200417094153-7316d94df1ee/go.mod h1:RtkHW8WbcNxj8lsbzjaILci01CtYnYbIkQhjyZWrWVI=
github.com/pingcap/tipb v0.0.0-20201209065231-aa39b1b86217 h1:Ophn4Ud/QHp1BH0FJOzbAVBW9Mw8BlX0gtWkK7ubDy0=
github.com/pingcap/tipb v0.0.0-20201209065231-aa39b1b86217/go.mod h1:RtkHW8WbcNxj8lsbzjaILci01CtYnYbIkQhjyZWrWVI=
github.com/pingcap/tipb v0.0.0-20201215091753-bd0cb2b314a4 h1:x64INZ8imEXO3MFcWD99lYlp52V9ZdYrxj74ynfyg3c=
github.com/pingcap/tipb v0.0.0-20201215091753-bd0cb2b314a4/go.mod h1:RtkHW8WbcNxj8lsbzjaILci01CtYnYbIkQhjyZWrWVI=
github.com/pingcap/tiup v1.2.3 h1:8OCQF7sHhT6VqE8pZU1JTSogPA90OFuWWM/B746x0YY=
github.com/pingcap/tiup v1.2.3/go.mod h1:q8WzflNHjE1U49k2qstTL0clx2pKh8pkOzUFV4RTvQo=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
Expand Down
4 changes: 2 additions & 2 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3476,9 +3476,9 @@ func buildShowSchema(s *ast.ShowStmt, isView bool, isSequence bool) (schema *exp
mysql.TypeLonglong, mysql.TypeLonglong, mysql.TypeDouble, mysql.TypeDouble}
case ast.ShowStatsBuckets:
names = []string{"Db_name", "Table_name", "Partition_name", "Column_name", "Is_index", "Bucket_id", "Count",
"Repeats", "Lower_Bound", "Upper_Bound"}
"Repeats", "Lower_Bound", "Upper_Bound", "Ndv"}
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeTiny, mysql.TypeLonglong,
mysql.TypeLonglong, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeVarchar}
mysql.TypeLonglong, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong}
case ast.ShowStatsTopN:
names = []string{"Db_name", "Table_name", "Partition_name", "Column_name", "Is_index", "Value", "Count"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeTiny, mysql.TypeVarchar, mysql.TypeLonglong}
Expand Down
15 changes: 13 additions & 2 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ const (
count BIGINT(64) NOT NULL,
repeats BIGINT(64) NOT NULL,
upper_bound BLOB NOT NULL,
lower_bound BLOB ,
lower_bound BLOB ,
ndv BIGINT NOT NULL DEFAULT 0,
UNIQUE INDEX tbl(table_id, is_index, hist_id, bucket_id)
);`

Expand Down Expand Up @@ -444,9 +445,11 @@ const (
version57 = 57
// version58 add `Repl_client_priv` and `Repl_slave_priv` to `mysql.user`
version58 = 58
// version59 add column ndv for mysql.stats_buckets.
version59 = 59

// please make sure this is the largest version
currentBootstrapVersion = version58
currentBootstrapVersion = version59
)

var (
Expand Down Expand Up @@ -509,6 +512,7 @@ var (
upgradeToVer56,
upgradeToVer57,
upgradeToVer58,
upgradeToVer59,
}
)

Expand Down Expand Up @@ -1274,6 +1278,13 @@ func writeMemoryQuotaQuery(s Session) {
mustExecute(s, sql)
}

func upgradeToVer59(s Session, ver int64) {
winoros marked this conversation as resolved.
Show resolved Hide resolved
if ver >= version59 {
return
}
doReentrantDDL(s, "ALTER TABLE mysql.stats_buckets ADD COLUMN `ndv` bigint not null default 0", infoschema.ErrColumnExists)
}

// updateBootstrapVer updates bootstrap version variable in mysql.TiDB table.
func updateBootstrapVer(s Session) {
// Update bootstrap version.
Expand Down
Loading