*: show cmd to check if all needed histograms are loaded (#29672)

This commit is contained in:
Xiaoju Wu
2021-12-10 13:45:57 +08:00
committed by GitHub
parent 4f42291a67
commit aa002f28a2
10 changed files with 9229 additions and 9174 deletions

View File

@ -196,6 +196,9 @@ func (e *ShowExec) fetchAll(ctx context.Context) error {
case ast.ShowStatsHealthy:
e.fetchShowStatsHealthy()
return nil
case ast.ShowHistogramsInFlight:
e.fetchShowHistogramsInFlight()
return nil
case ast.ShowColumnStatsUsage:
return e.fetchShowColumnStatsUsage()
case ast.ShowPlugins:

View File

@ -443,6 +443,10 @@ func (e *ShowExec) appendTableForStatsHealthy(dbName, tblName, partitionName str
})
}
func (e *ShowExec) fetchShowHistogramsInFlight() {
e.appendRow([]interface{}{statistics.HistogramNeededColumns.Length()})
}
func (e *ShowExec) fetchShowAnalyzeStatus() {
rows := dataForAnalyzeStatusHelper(e.baseExecutor.ctx)
for _, row := range rows {

View File

@ -350,3 +350,16 @@ func TestShowColumnStatsUsage(t *testing.T) {
require.Equal(t, rows[0], []interface{}{"test", "t2", "global", t1.Meta().Columns[0].Name.O, "2021-10-20 09:00:00", "<nil>"})
require.Equal(t, rows[1], []interface{}{"test", "t2", p0.Name.O, t1.Meta().Columns[0].Name.O, "2021-10-20 09:00:00", "<nil>"})
}
func TestShowHistogramsInFlight(t *testing.T) {
t.Parallel()
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
result := tk.MustQuery("show histograms_in_flight")
rows := result.Rows()
require.Equal(t, len(rows), 1)
require.Equal(t, rows[0][0], "0")
}

View File

@ -2580,6 +2580,7 @@ const (
ShowStatsTopN
ShowStatsBuckets
ShowStatsHealthy
ShowHistogramsInFlight
ShowColumnStatsUsage
ShowPlugins
ShowProfile
@ -2768,6 +2769,11 @@ func (n *ShowStmt) Restore(ctx *format.RestoreCtx) error {
if err := restoreShowLikeOrWhereOpt(); err != nil {
return err
}
case ShowHistogramsInFlight:
ctx.WriteKeyWord("HISTOGRAMS_IN_FLIGHT")
if err := restoreShowLikeOrWhereOpt(); err != nil {
return err
}
case ShowColumnStatsUsage:
ctx.WriteKeyWord("COLUMN_STATS_USAGE")
if err := restoreShowLikeOrWhereOpt(); err != nil {

View File

@ -670,6 +670,7 @@ var tokenMap = map[string]int{
"STATS_HISTOGRAMS": statsHistograms,
"STATS_TOPN": statsTopN,
"STATS_META": statsMeta,
"HISTOGRAMS_IN_FLIGHT": histogramsInFlight,
"STATS_PERSISTENT": statsPersistent,
"STATS_SAMPLE_PAGES": statsSamplePages,
"STATS": stats,

File diff suppressed because it is too large Load Diff

View File

@ -736,6 +736,7 @@ import (
statsBuckets "STATS_BUCKETS"
statsHealthy "STATS_HEALTHY"
statsTopN "STATS_TOPN"
histogramsInFlight "HISTOGRAMS_IN_FLIGHT"
telemetry "TELEMETRY"
telemetryID "TELEMETRY_ID"
tidb "TIDB"
@ -6079,6 +6080,7 @@ TiDBKeyword:
| "STATS_TOPN"
| "STATS_BUCKETS"
| "STATS_HEALTHY"
| "HISTOGRAMS_IN_FLIGHT"
| "TELEMETRY"
| "TELEMETRY_ID"
| "TIDB"
@ -10665,6 +10667,10 @@ ShowTargetFilterable:
{
$$ = &ast.ShowStmt{Tp: ast.ShowStatsHealthy}
}
| "HISTOGRAMS_IN_FLIGHT"
{
$$ = &ast.ShowStmt{Tp: ast.ShowHistogramsInFlight}
}
| "COLUMN_STATS_USAGE"
{
$$ = &ast.ShowStmt{Tp: ast.ShowColumnStatsUsage}

View File

@ -1128,6 +1128,8 @@ func TestDBAStmt(t *testing.T) {
// for show stats_topn.
{"show stats_topn", true, "SHOW STATS_TOPN"},
{"show stats_topn where table_name = 't'", true, "SHOW STATS_TOPN WHERE `table_name`=_UTF8MB4't'"},
// for show histograms_in_flight.
{"show histograms_in_flight", true, "SHOW HISTOGRAMS_IN_FLIGHT"},
// for show column_stats_usage.
{"show column_stats_usage", true, "SHOW COLUMN_STATS_USAGE"},
{"show column_stats_usage where table_name = 't'", true, "SHOW COLUMN_STATS_USAGE WHERE `table_name`=_UTF8MB4't'"},

View File

@ -2589,7 +2589,7 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan,
p.setSchemaAndNames(buildShowNextRowID())
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, show.Table.Schema.L, show.Table.Name.L, "", ErrPrivilegeCheckFail)
return p, nil
case ast.ShowStatsBuckets, ast.ShowStatsHistograms, ast.ShowStatsMeta, ast.ShowStatsExtended, ast.ShowStatsHealthy, ast.ShowStatsTopN, ast.ShowColumnStatsUsage:
case ast.ShowStatsBuckets, ast.ShowStatsHistograms, ast.ShowStatsMeta, ast.ShowStatsExtended, ast.ShowStatsHealthy, ast.ShowStatsTopN, ast.ShowHistogramsInFlight, ast.ShowColumnStatsUsage:
user := b.ctx.GetSessionVars().User
var err error
if user != nil {
@ -4352,6 +4352,9 @@ func buildShowSchema(s *ast.ShowStmt, isView bool, isSequence bool) (schema *exp
case ast.ShowStatsHealthy:
names = []string{"Db_name", "Table_name", "Partition_name", "Healthy"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeLonglong}
case ast.ShowHistogramsInFlight:
names = []string{"HistogramsInFlight"}
ftypes = []byte{mysql.TypeLonglong}
case ast.ShowColumnStatsUsage:
names = []string{"Db_name", "Table_name", "Partition_name", "Column_name", "Last_used_at", "Last_analyzed_at"}
ftypes = []byte{mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeVarchar, mysql.TypeDatetime, mysql.TypeDatetime}

View File

@ -236,17 +236,17 @@ type tableColumnID struct {
}
type neededColumnMap struct {
m sync.Mutex
m sync.RWMutex
cols map[tableColumnID]struct{}
}
func (n *neededColumnMap) AllCols() []tableColumnID {
n.m.Lock()
n.m.RLock()
keys := make([]tableColumnID, 0, len(n.cols))
for key := range n.cols {
keys = append(keys, key)
}
n.m.Unlock()
n.m.RUnlock()
return keys
}
@ -262,6 +262,12 @@ func (n *neededColumnMap) Delete(col tableColumnID) {
n.m.Unlock()
}
func (n *neededColumnMap) Length() int {
n.m.RLock()
defer n.m.RUnlock()
return len(n.cols)
}
// RatioOfPseudoEstimate means if modifyCount / statsTblCount is greater than this ratio, we think the stats is invalid
// and use pseudo estimation.
var RatioOfPseudoEstimate = atomic.NewFloat64(0.7)