Documentation
¶
Index ¶
- Constants
- Variables
- func CollectTableNames(in ast.Node) []*ast.TableName
- func GenerateBindingSQL(stmtNode ast.StmtNode, planHint string, defaultDB string) string
- func IsSimplePointPlan(plan string) bool
- func MatchSQLBindingForPlanCache(sctx sessionctx.Context, stmtNode ast.StmtNode, info *BindingMatchInfo) (bindingSQL string)
- func NormalizeStmtForBinding(stmtNode ast.StmtNode, specifiedDB string, noDB bool) (normalizedStmt, sqlDigest string)
- func RestoreDBForBinding(node ast.StmtNode, defaultDB string) string
- type Binding
- type BindingCache
- type BindingCacheUpdater
- type BindingHandle
- type BindingMatchInfo
- type BindingOperator
- type BindingPlanEvolution
- type BindingPlanInfo
- type PlanGenerator
- type PlanPerfPredictor
- type SessionBindingHandle
Constants ¶
const ( // StatusEnabled is the bind info's in enabled status. // It is the same as the previous 'Using' status. // Only use 'Enabled' status in the future, not the 'Using' status. // The 'Using' status is preserved for compatibility. StatusEnabled = "enabled" // StatusDisabled is the bind info's in disabled status. StatusDisabled = "disabled" // StatusUsing is the bind info's in use status. // The 'Using' status is preserved for compatibility. StatusUsing = "using" // StatusDeleted is the bind info's deleted status. StatusDeleted = "deleted" // StatusBuiltin indicates the binding is a builtin record for internal locking purpose. It is also the status for the builtin binding. StatusBuiltin = "builtin" // SourceManual indicates the binding is created by SQL like "create binding for ...". SourceManual = "manual" // SourceHistory indicate the binding is created from statement summary by plan digest SourceHistory = "history" )
const ( // OwnerKey is the bindinfo owner path that is saved to etcd. OwnerKey = "/tidb/bindinfo/owner" // Prompt is the prompt for bindinfo owner manager. Prompt = "bindinfo" // BuiltinPseudoSQL4BindLock is used to simulate LOCK TABLE for mysql.bind_info. BuiltinPseudoSQL4BindLock = "builtin_pseudo_sql_for_bind_lock" // LockBindInfoSQL simulates LOCK TABLE by updating a same row in each pessimistic transaction. LockBindInfoSQL = `UPDATE mysql.bind_info SET source= 'builtin' WHERE original_sql= 'builtin_pseudo_sql_for_bind_lock'` // StmtRemoveDuplicatedPseudoBinding is used to remove duplicated pseudo binding. // After using BR to sync bind_info between two clusters, the pseudo binding may be duplicated, and // BR use this statement to remove duplicated rows, and this SQL should only be executed by BR. StmtRemoveDuplicatedPseudoBinding = `` /* 269-byte string literal not displayed */ )
const SessionBindInfoKeyType sessionBindInfoKeyType = 0
SessionBindInfoKeyType is a variable key for store session bind info.
Variables ¶
var ( // UpdateBindingUsageInfoBatchSize indicates the batch size when updating binding usage info to storage. UpdateBindingUsageInfoBatchSize = 100 // MaxWriteInterval indicates the interval at which a write operation needs to be performed after a binding has not been read. MaxWriteInterval = 6 * time.Hour )
var CalculatePlanDigest func(sctx sessionctx.Context, stmt ast.StmtNode) (planDigest string, err error)
CalculatePlanDigest is used to get the plan digest of this SQL. This function will call the optimizer.
var GenBriefPlanWithSCtx func(sctx sessionctx.Context, stmt ast.StmtNode) (planDigest, planHintStr string, planText [][]string, err error)
GenBriefPlanWithSCtx generates the plan for the given SQL statement under the given session context. This function will call the optimizer, the output is same as "EXPLAIN FORMAT='brief' {SQL}". PlanHintStr is a set of hints to reproduce this plan, which is used to create binding. PlanText is the results of EXPLAIN of the current plan.
var ( // GetBindingHandle is a function to get the global binding handle. // It is mainly used to resolve cycle import issue. GetBindingHandle func(sctx sessionctx.Context) BindingHandle )
var Lease = 3 * time.Second
Lease influences the duration of loading bind info and handling invalid bind.
var RecordRelevantOptVarsAndFixes func(sctx sessionctx.Context, stmt ast.StmtNode) (varNames []string, fixIDs []uint64, err error)
RecordRelevantOptVarsAndFixes is used to get the relevant optimizer variables for this SQL. This function will call the optimizer.
var TestTimeLagInLoadingBinding stringutil.StringerStr = "TestTimeLagInLoadingBinding"
TestTimeLagInLoadingBinding is used for test only.
Functions ¶
func CollectTableNames ¶
CollectTableNames gets all table names from ast.Node. This function is mainly for binding cross-db matching. ** the return is read-only. For example:
`select * from t1 where a < 1` --> [t1] `select * from db1.t1, t2 where a < 1` --> [db1.t1, t2]
You can see more example at the TestExtractTableName.
func GenerateBindingSQL ¶
GenerateBindingSQL generates binding sqls from stmt node and plan hints.
func IsSimplePointPlan ¶
IsSimplePointPlan checks whether the plan is a simple point plan. Expose this function for testing.
func MatchSQLBindingForPlanCache ¶
func MatchSQLBindingForPlanCache(sctx sessionctx.Context, stmtNode ast.StmtNode, info *BindingMatchInfo) (bindingSQL string)
MatchSQLBindingForPlanCache matches binding for plan cache.
func NormalizeStmtForBinding ¶
func NormalizeStmtForBinding(stmtNode ast.StmtNode, specifiedDB string, noDB bool) (normalizedStmt, sqlDigest string)
NormalizeStmtForBinding normalizes a statement for binding. This function skips Explain automatically, and literals in in-lists will be normalized as '...'. For normal bindings, DB name will be completed automatically: when noDB is false, schema names will be completed automatically: `select * from t` --> `select * from db . t`. when noDB is true, schema names will be eliminated automatically: `select * from db . t` --> `select * from t`.
e.g. `select * from t where a in (1, 2, 3)` --> `select * from test.t where a in (...)`
Types ¶
type Binding ¶
type Binding struct {
OriginalSQL string
Db string
BindSQL string
// Status represents the status of the binding. It can only be one of the following values:
// 1. deleted: Bindings is deleted, can not be used anymore.
// 2. enabled, using: Binding is in the normal active mode.
Status string
CreateTime types.Time
UpdateTime types.Time
Source string
Charset string
Collation string
// Hint is the parsed hints, it is used to bind hints to stmt node.
Hint *hint.HintsSet `json:"-"`
// ID is the string form of Hint. It would be non-empty only when the status is `Using` or `PendingVerify`.
ID string `json:"-"`
SQLDigest string
PlanDigest string
// TableNames records all schema and table names in this binding statement, which are used for cross-db matching.
TableNames []*ast.TableName `json:"-"`
// UsageInfo is to track the usage information `last_used_time` of this binding
// and it will be updated when this binding is used.
UsageInfo bindingInfoUsageInfo
}
Binding stores the basic bind hint info.
func MatchSQLBinding ¶
func MatchSQLBinding(sctx sessionctx.Context, stmtNode ast.StmtNode) (binding *Binding, matched bool, scope string)
MatchSQLBinding returns the matched binding for this statement.
func (*Binding) IsBindingEnabled ¶
IsBindingEnabled returns whether the binding is enabled.
func (*Binding) UpdateLastSavedAt ¶
UpdateLastSavedAt is to update the last saved time
func (*Binding) UpdateLastUsedAt ¶
func (b *Binding) UpdateLastUsedAt()
UpdateLastUsedAt is to update binding usage info when this binding is used.
type BindingCache ¶
type BindingCache interface {
// MatchingBinding supports cross-db matching on bindings.
MatchingBinding(sctx sessionctx.Context, noDBDigest string, tableNames []*ast.TableName) (binding *Binding, isMatched bool)
// GetBinding gets the binding for the specified sqlDigest.
GetBinding(sqlDigest string) *Binding
// GetAllBindings gets all the bindings in the cache.
GetAllBindings() []*Binding
// SetBinding sets the binding for the specified sqlDigest.
SetBinding(sqlDigest string, binding *Binding) (err error)
// RemoveBinding removes the binding for the specified sqlDigest.
RemoveBinding(sqlDigest string)
// SetMemCapacity sets the memory capacity for the cache.
SetMemCapacity(capacity int64)
// GetMemUsage gets the memory usage of the cache.
GetMemUsage() int64
// GetMemCapacity gets the memory capacity of the cache.
GetMemCapacity() int64
// Size returns the number of items in the cache.
Size() int
// Close closes the cache.
Close()
}
BindingCache is the interface for the cache of the SQL plan bindings.
type BindingCacheUpdater ¶
type BindingCacheUpdater interface {
BindingCache
// LoadFromStorageToCache loads global bindings from storage to the memory cache.
LoadFromStorageToCache(fullLoad bool) (err error)
// UpdateBindingUsageInfoToStorage is to update the binding usage info into storage
UpdateBindingUsageInfoToStorage() error
// LastUpdateTime returns the last update time.
LastUpdateTime() types.Time
}
BindingCacheUpdater maintains the binding cache and provide update APIs.
func NewBindingCacheUpdater ¶
func NewBindingCacheUpdater(sPool util.DestroyableSessionPool) BindingCacheUpdater
NewBindingCacheUpdater creates a new BindingCacheUpdater.
type BindingHandle ¶
type BindingHandle interface {
BindingCacheUpdater
BindingOperator
BindingPlanEvolution
variable.Statistics
}
BindingHandle is used to handle all sql bind operations.
func NewBindingHandle ¶
func NewBindingHandle(sPool util.DestroyableSessionPool) BindingHandle
NewBindingHandle creates a new BindingHandle.
type BindingMatchInfo ¶
BindingMatchInfo records necessary information for cross-db binding matching. This is mainly for plan cache to avoid normalizing the same statement repeatedly.
type BindingOperator ¶
type BindingOperator interface {
// CreateBinding creates a Bindings to the storage and the cache.
// It replaces all the exists bindings for the same normalized SQL.
CreateBinding(sctx sessionctx.Context, bindings []*Binding) (err error)
// DropBinding drop Bindings to the storage and Bindings int the cache.
DropBinding(sqlDigests []string) (deletedRows uint64, err error)
// SetBindingStatus set a Bindings's status to the storage and bind cache.
SetBindingStatus(newStatus, sqlDigest string) (ok bool, err error)
// GCBinding physically removes the deleted bind records in mysql.bind_info.
GCBinding() (err error)
}
BindingOperator is used to operate (create/drop/update/GC) bindings.
type BindingPlanEvolution ¶
type BindingPlanEvolution interface {
// ExplorePlansForSQL explores plans for this SQL.
ExplorePlansForSQL(stmtSCtx base.PlanContext, sqlOrDigest string, analyze bool) ([]*BindingPlanInfo, error)
}
BindingPlanEvolution represents a series of APIs that help manage bindings automatically.
type BindingPlanInfo ¶
type BindingPlanInfo struct {
*Binding
// Info from StmtStats
Plan string
AvgLatency float64
ExecTimes int64
AvgScanRows float64
AvgReturnedRows float64
LatencyPerReturnRow float64
ScanRowsPerReturnRow float64
// Recommendation
Recommend string
Reason string
}
BindingPlanInfo contains the binding info and its corresponding plan execution info, which is used by "EXPLAIN EXPLORE <SQL>" to help users understand the historical plans for a specific SQL.
type PlanGenerator ¶
type PlanGenerator interface {
Generate(defaultSchema, sql, charset, collation string) (plans []*BindingPlanInfo, err error)
}
PlanGenerator is used to generate new Plan Candidates for this specified query.
type PlanPerfPredictor ¶
type PlanPerfPredictor interface {
PerfPredicate(plans []*BindingPlanInfo) (scores []float64, explanations []string, err error)
}
PlanPerfPredictor is used to score these plan candidates, returns their scores and gives some explanations. If all scores are 0, means that no plan is recommended.
type SessionBindingHandle ¶
type SessionBindingHandle interface {
// CreateSessionBinding creates a binding to the cache.
CreateSessionBinding(sctx sessionctx.Context, bindings []*Binding) (err error)
// DropSessionBinding drops a binding by the sql digest.
DropSessionBinding(sqlDigests []string) error
// MatchSessionBinding returns the matched binding for this statement.
MatchSessionBinding(sctx sessionctx.Context, noDBDigest string, tableNames []*ast.TableName) (matchedBinding *Binding, isMatched bool)
// GetAllSessionBindings return all bindings.
GetAllSessionBindings() (bindings []*Binding)
// Close closes the SessionBindingHandle.
Close()
sessionctx.SessionStatesHandler
}
SessionBindingHandle is used to handle all session sql bind operations.
func NewSessionBindingHandle ¶
func NewSessionBindingHandle() SessionBindingHandle
NewSessionBindingHandle creates a new SessionBindingHandle.