Skip to content

Commit

Permalink
*: support show stats_healthy. (pingcap#5769)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored Feb 5, 2018
1 parent fc699c9 commit a04b2f9
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ const (
ShowStatsMeta
ShowStatsHistograms
ShowStatsBuckets
ShowStatsHealthy
ShowPlugins
ShowProfiles
)
Expand Down
3 changes: 3 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ func (e *ShowExec) fetchAll() error {
return e.fetchShowStatsHistogram()
case ast.ShowStatsBuckets:
return e.fetchShowStatsBuckets()
case ast.ShowStatsHealthy:
e.fetchShowStatsHealthy()
return nil
case ast.ShowPlugins:
return e.fetchShowPlugins()
case ast.ShowProfiles:
Expand Down
24 changes: 24 additions & 0 deletions executor/show_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,27 @@ func (e *ShowExec) valueToString(value *types.Datum, size int) (string, error) {
}
return str, nil
}

func (e *ShowExec) fetchShowStatsHealthy() {
do := domain.GetDomain(e.ctx)
h := do.StatsHandle()
dbs := do.InfoSchema().AllSchemas()
for _, db := range dbs {
for _, tbl := range db.Tables {
statsTbl := h.GetTableStats(tbl.ID)
if !statsTbl.Pseudo {
var healthy int64
if statsTbl.ModifyCount < statsTbl.Count {
healthy = int64((1.0 - float64(statsTbl.ModifyCount)/float64(statsTbl.Count)) * 100.0)
} else if statsTbl.ModifyCount == 0 {
healthy = 100
}
e.appendRow([]interface{}{
db.Name.O,
tbl.Name.O,
healthy,
})
}
}
}
}
26 changes: 26 additions & 0 deletions executor/show_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package executor_test

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/util/testkit"
)

Expand Down Expand Up @@ -62,3 +63,28 @@ func (s *testSuite) TestShowStatsBuckets(c *C) {
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)"))
}

func (s *testSuite) TestShowStatsHealthy(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int)")
tk.MustExec("create index idx on t(a)")
tk.MustExec("analyze table t")
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t 100"))
tk.MustExec("insert into t values (1), (2)")
do, _ := tidb.GetDomain(s.store)
do.StatsHandle().DumpStatsDeltaToKV()
tk.MustExec("analyze table t")
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t 100"))
tk.MustExec("insert into t values (3), (4), (5), (6), (7), (8), (9), (10)")
do.StatsHandle().DumpStatsDeltaToKV()
do.StatsHandle().Update(do.InfoSchema())
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t 19"))
tk.MustExec("analyze table t")
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t 100"))
tk.MustExec("delete from t")
do.StatsHandle().DumpStatsDeltaToKV()
do.StatsHandle().Update(do.InfoSchema())
tk.MustQuery("show stats_healthy").Check(testkit.Rows("test t 0"))
}
1 change: 1 addition & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ var tokenMap = map[string]int{
"STATS": stats,
"STATS_BUCKETS": statsBuckets,
"STATS_HISTOGRAMS": statsHistograms,
"STATS_HEALTHY": statsHealthy,
"STATS_META": statsMeta,
"STATS_PERSISTENT": statsPersistent,
"STATUS": status,
Expand Down
17 changes: 16 additions & 1 deletion parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ import (
statsMeta "STATS_META"
statsHistograms "STATS_HISTOGRAMS"
statsBuckets "STATS_BUCKETS"
statsHealthy "STATS_HEALTHY"
tidb "TIDB"
tidbHJ "TIDB_HJ"
tidbSMJ "TIDB_SMJ"
Expand Down Expand Up @@ -2505,7 +2506,7 @@ UnReservedKeyword:
| "MAX_USER_CONNECTIONS" | "REPLICATION" | "CLIENT" | "SLAVE" | "RELOAD" | "TEMPORARY" | "ROUTINE" | "EVENT" | "ALGORITHM" | "DEFINER" | "INVOKER" | "MERGE" | "TEMPTABLE" | "UNDEFINED" | "SECURITY" | "CASCADED"

TiDBKeyword:
"ADMIN" | "CANCEL" | "DDL" | "JOBS" | "STATS" | "STATS_META" | "STATS_HISTOGRAMS" | "STATS_BUCKETS" | "TIDB" | "TIDB_HJ" | "TIDB_SMJ" | "TIDB_INLJ"
"ADMIN" | "CANCEL" | "DDL" | "JOBS" | "STATS" | "STATS_META" | "STATS_HISTOGRAMS" | "STATS_BUCKETS" | "STATS_HEALTHY" | "TIDB" | "TIDB_HJ" | "TIDB_SMJ" | "TIDB_INLJ"

NotKeywordToken:
"ADDDATE" | "BIT_AND" | "BIT_OR" | "BIT_XOR" | "CAST" | "COUNT" | "CURTIME" | "DATE_ADD" | "DATE_SUB" | "EXTRACT" | "GET_FORMAT" | "GROUP_CONCAT" | "MIN" | "MAX" | "NOW" | "POSITION"
Expand Down Expand Up @@ -4842,6 +4843,20 @@ ShowStmt:
}
$$ = stmt
}
| "SHOW" "STATS_HEALTHY" ShowLikeOrWhereOpt
{
stmt := &ast.ShowStmt{
Tp: ast.ShowStatsHealthy,
}
if $3 != nil {
if x, ok := $3.(*ast.PatternLikeExpr); ok {
stmt.Pattern = x
} else {
stmt.Where = $3.(ast.ExprNode)
}
}
$$ = stmt
}
| "SHOW" "PROFILES"
{
$$ = &ast.ShowStmt{
Expand Down
5 changes: 4 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *testParserSuite) TestSimple(c *C) {
"enable", "disable", "reverse", "space", "privileges", "get_lock", "release_lock", "sleep", "no", "greatest", "least",
"binlog", "hex", "unhex", "function", "indexes", "from_unixtime", "processlist", "events", "less", "than", "timediff",
"ln", "log", "log2", "log10", "timestampdiff", "pi", "quote", "none", "super", "shared", "exclusive",
"always", "stats", "stats_meta", "stats_histogram", "stats_buckets", "tidb_version", "replication", "slave", "client",
"always", "stats", "stats_meta", "stats_histogram", "stats_buckets", "stats_healthy", "tidb_version", "replication", "slave", "client",
"max_connections_per_hour", "max_queries_per_hour", "max_updates_per_hour", "max_user_connections", "event", "reload", "routine", "temporary",
}
for _, kw := range unreservedKws {
Expand Down Expand Up @@ -507,6 +507,9 @@ func (s *testParserSuite) TestDBAStmt(c *C) {
// for show stats_buckets
{"show stats_buckets", true},
{"show stats_buckets where col_name = 'a'", true},
// for show stats_healthy.
{"show stats_healthy", true},
{"show stats_healthy where table_name = 't'", true},

// for load stats
{"load stats '/tmp/stats.json'", true},
Expand Down
3 changes: 3 additions & 0 deletions plan/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,9 @@ func buildShowSchema(s *ast.ShowStmt) (schema *expression.Schema) {
"Repeats", "Lower_Bound", "Upper_Bound"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeTiny, mysql.TypeLonglong,
mysql.TypeLonglong, mysql.TypeLonglong, mysql.TypeVarchar, mysql.TypeVarchar}
case ast.ShowStatsHealthy:
names = []string{"Db_name", "Table_name", "Healthy"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong}
case ast.ShowProfiles: // ShowProfiles is deprecated.
names = []string{"Query_ID", "Duration", "Query"}
ftypes = []byte{mysql.TypeLong, mysql.TypeDouble, mysql.TypeVarchar}
Expand Down

0 comments on commit a04b2f9

Please sign in to comment.