Documentation ¶
Index ¶
- Constants
- Variables
- func CollectTableNames(in ast.Node) []*ast.TableName
- func GenerateBindingSQL(stmtNode ast.StmtNode, planHint string, skipCheckIfHasParam bool, ...) string
- func HasAvailableBinding(br Bindings) bool
- func MatchSQLBindingForPlanCache(sctx sessionctx.Context, stmtNode ast.StmtNode, info *BindingMatchInfo) (bindingSQL string, ignoreBinding bool)
- func ParseCaptureTableFilter(tableFilter string) (f tablefilter.Filter, valid bool)
- type Binding
- type BindingCache
- type BindingMatchInfo
- type Bindings
- type FuzzyBindingCache
- type GlobalBindingHandle
- type SessionBindingHandle
Constants ¶
const ( // Enabled 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. Enabled = "enabled" // Disabled is the bind info's in disabled status. Disabled = "disabled" // Using is the bind info's in use status. // The 'Using' status is preserved for compatibility. Using = "using" // Invalid is the bind info's invalid status. Invalid = "invalid" // Manual indicates the binding is created by SQL like "create binding for ...". Manual = "manual" // Capture indicates the binding is captured by TiDB automatically. Capture = "capture" // Builtin indicates the binding is a builtin record for internal locking purpose. It is also the status for the builtin binding. Builtin = "builtin" // History indicate the binding is created from statement summary by plan digest History = "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 GetBindingReturnNil = stringutil.StringerStr("GetBindingReturnNil")
GetBindingReturnNil is only for test
var GetBindingReturnNilAlways = stringutil.StringerStr("getBindingReturnNilAlways")
GetBindingReturnNilAlways is only for test
var GetBindingReturnNilBool atomic.Bool
GetBindingReturnNilBool is only for test
var ( // GetGlobalBindingHandle is a function to get the global binding handle. // It is mainly used to resolve cycle import issue. GetGlobalBindingHandle func(sctx sessionctx.Context) GlobalBindingHandle )
var Lease = 3 * time.Second
Lease influences the duration of loading bind info and handling invalid bind.
var LoadBindingNothing = stringutil.StringerStr("LoadBindingNothing")
LoadBindingNothing is only for test
Functions ¶
func CollectTableNames ¶
CollectTableNames gets all table names from ast.Node. This function is mainly for binding fuzzy 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 ¶
func GenerateBindingSQL(stmtNode ast.StmtNode, planHint string, skipCheckIfHasParam bool, defaultDB string) string
GenerateBindingSQL generates binding sqls from stmt node and plan hints.
func HasAvailableBinding ¶
HasAvailableBinding checks if there are any available bindings in bind record. The available means the binding can be used or can be converted into a usable status. It includes the 'Enabled', 'Using' and 'Disabled' status.
func MatchSQLBindingForPlanCache ¶
func MatchSQLBindingForPlanCache(sctx sessionctx.Context, stmtNode ast.StmtNode, info *BindingMatchInfo) (bindingSQL string, ignoreBinding bool)
MatchSQLBindingForPlanCache matches binding for plan cache.
func ParseCaptureTableFilter ¶
func ParseCaptureTableFilter(tableFilter string) (f tablefilter.Filter, valid bool)
ParseCaptureTableFilter checks whether this filter is valid and parses it.
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 fuzzy matching. TableNames []*ast.TableName `json:"-"` }
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) IsBindingAvailable ¶
IsBindingAvailable returns whether the binding is available. The available means the binding can be used or can be converted into a usable status. It includes the 'Enabled', 'Using' and 'Disabled' status.
func (*Binding) IsBindingEnabled ¶
IsBindingEnabled returns whether the binding is enabled.
type BindingCache ¶
type BindingCache interface { // GetBinding gets the binding for the specified sqlDigest. GetBinding(sqlDigest string) Bindings // GetAllBindings gets all the bindings in the cache. GetAllBindings() Bindings // SetBinding sets the binding for the specified sqlDigest. SetBinding(sqlDigest string, meta Bindings) (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 // CopyBindingCache copies the cache. CopyBindingCache() (newCache BindingCache, err error) // Size returns the number of items in the cache. Size() int }
BindingCache is the interface for the cache of the SQL plan bindings.
type BindingMatchInfo ¶
BindingMatchInfo records necessary information for fuzzy binding matching. This is mainly for plan cache to avoid normalizing the same statement repeatedly.
type Bindings ¶
type Bindings []Binding
Bindings represents a sql bind record retrieved from the storage.
type FuzzyBindingCache ¶
type FuzzyBindingCache interface { // FuzzyMatchingBinding supports fuzzy matching on bindings. FuzzyMatchingBinding(sctx sessionctx.Context, fuzzyDigest string, tableNames []*ast.TableName) (bindings Binding, isMatched bool) // Copy copies this cache. Copy() (c FuzzyBindingCache, err error) BindingCache }
FuzzyBindingCache is based on BindingCache, and provide some more advanced features, like fuzzy matching, loading binding if cache miss automatically (TODO).
type GlobalBindingHandle ¶
type GlobalBindingHandle interface { // MatchGlobalBinding returns the matched binding for this statement. MatchGlobalBinding(sctx sessionctx.Context, fuzzyDigest string, tableNames []*ast.TableName) (matchedBinding Binding, isMatched bool) // GetAllGlobalBindings returns all bind records in cache. GetAllGlobalBindings() (bindings Bindings) // CreateGlobalBinding creates a Bindings to the storage and the cache. // It replaces all the exists bindings for the same normalized SQL. CreateGlobalBinding(sctx sessionctx.Context, bindings []*Binding) (err error) // DropGlobalBinding drop Bindings to the storage and Bindings int the cache. DropGlobalBinding(sqlDigests []string) (deletedRows uint64, err error) // SetGlobalBindingStatus set a Bindings's status to the storage and bind cache. SetGlobalBindingStatus(newStatus, sqlDigest string) (ok bool, err error) // AddInvalidGlobalBinding adds Bindings which needs to be deleted into invalidBindingCache. AddInvalidGlobalBinding(invalidBinding Binding) // DropInvalidGlobalBinding executes the drop Bindings tasks. DropInvalidGlobalBinding() // Reset is to reset the BindHandle and clean old info. Reset() // LoadFromStorageToCache loads global bindings from storage to the memory cache. LoadFromStorageToCache(fullLoad bool) (err error) // GCGlobalBinding physically removes the deleted bind records in mysql.bind_info. GCGlobalBinding() (err error) // Size returns the size of bind info cache. Size() int // SetBindingCacheCapacity reset the capacity for the bindingCache. SetBindingCacheCapacity(capacity int64) // GetMemUsage returns the memory usage for the bind cache. GetMemUsage() (memUsage int64) // GetMemCapacity returns the memory capacity for the bind cache. GetMemCapacity() (memCapacity int64) // Clear resets the bind handle. It is only used for test. Clear() // FlushGlobalBindings flushes the Bindings in temp maps to storage and loads them into cache. FlushGlobalBindings() error // CaptureBaselines is used to automatically capture plan baselines. CaptureBaselines() variable.Statistics }
GlobalBindingHandle is used to handle all global sql bind operations.
func NewGlobalBindingHandle ¶
func NewGlobalBindingHandle(sPool util.SessionPool) GlobalBindingHandle
NewGlobalBindingHandle creates a new GlobalBindingHandle.
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, fuzzyDigest string, tableNames []*ast.TableName) (matchedBinding Binding, isMatched bool) // GetAllSessionBindings return all bindings. GetAllSessionBindings() (bindings Bindings) // 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.