forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 7
/
plan_cache_utils.go
296 lines (276 loc) · 11.2 KB
/
plan_cache_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package core
import (
"math"
"strconv"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/kvcache"
atomic2 "go.uber.org/atomic"
)
var (
// PreparedPlanCacheMaxMemory stores the max memory size defined in the global config "performance-server-memory-quota".
PreparedPlanCacheMaxMemory = *atomic2.NewUint64(math.MaxUint64)
)
func getValidPlanFromCache(sctx sessionctx.Context, isGeneralPlanCache bool, key kvcache.Key, paramTypes []*types.FieldType) (*PlanCacheValue, bool) {
cache := sctx.GetPlanCache(isGeneralPlanCache)
val, exist := cache.Get(key)
if !exist {
return nil, exist
}
candidates := val.([]*PlanCacheValue)
for _, candidate := range candidates {
if candidate.varTypesUnchanged(paramTypes) {
return candidate, true
}
}
return nil, false
}
func putPlanIntoCache(sctx sessionctx.Context, isGeneralPlanCache bool, key kvcache.Key, plan *PlanCacheValue) {
cache := sctx.GetPlanCache(isGeneralPlanCache)
val, exist := cache.Get(key)
if !exist {
cache.Put(key, []*PlanCacheValue{plan})
return
}
candidates := val.([]*PlanCacheValue)
for i, candidate := range candidates {
if candidate.varTypesUnchanged(plan.ParamTypes) {
// hit an existing cached plan
candidates[i] = plan
return
}
}
// add to current candidate list
// TODO: limit the candidate list length
candidates = append(candidates, plan)
cache.Put(key, candidates)
}
// planCacheKey is used to access Plan Cache. We put some variables that do not affect the plan into planCacheKey, such as the sql text.
// Put the parameters that may affect the plan in planCacheValue.
// However, due to some compatibility reasons, we will temporarily keep some system variable-related values in planCacheKey.
// At the same time, because these variables have a small impact on plan, we will move them to PlanCacheValue later if necessary.
type planCacheKey struct {
database string
connID uint64
stmtText string
schemaVersion int64
// Only be set in rc or for update read and leave it default otherwise.
// In Rc or ForUpdateRead, we should check whether the information schema has been changed when using plan cache.
// If it changed, we should rebuild the plan. lastUpdatedSchemaVersion help us to decide whether we should rebuild
// the plan in rc or for update read.
lastUpdatedSchemaVersion int64
sqlMode mysql.SQLMode
timezoneOffset int
isolationReadEngines map[kv.StoreType]struct{}
selectLimit uint64
bindSQL string
inRestrictedSQL bool
restrictedReadOnly bool
TiDBSuperReadOnly bool
hash []byte
}
// Hash implements Key interface.
func (key *planCacheKey) Hash() []byte {
if len(key.hash) == 0 {
var (
dbBytes = hack.Slice(key.database)
bufferSize = len(dbBytes) + 8*6 + 3*8
)
if key.hash == nil {
key.hash = make([]byte, 0, bufferSize)
}
key.hash = append(key.hash, dbBytes...)
key.hash = codec.EncodeInt(key.hash, int64(key.connID))
key.hash = append(key.hash, hack.Slice(key.stmtText)...)
key.hash = codec.EncodeInt(key.hash, key.schemaVersion)
key.hash = codec.EncodeInt(key.hash, key.lastUpdatedSchemaVersion)
key.hash = codec.EncodeInt(key.hash, int64(key.sqlMode))
key.hash = codec.EncodeInt(key.hash, int64(key.timezoneOffset))
if _, ok := key.isolationReadEngines[kv.TiDB]; ok {
key.hash = append(key.hash, kv.TiDB.Name()...)
}
if _, ok := key.isolationReadEngines[kv.TiKV]; ok {
key.hash = append(key.hash, kv.TiKV.Name()...)
}
if _, ok := key.isolationReadEngines[kv.TiFlash]; ok {
key.hash = append(key.hash, kv.TiFlash.Name()...)
}
key.hash = codec.EncodeInt(key.hash, int64(key.selectLimit))
key.hash = append(key.hash, hack.Slice(key.bindSQL)...)
key.hash = append(key.hash, hack.Slice(strconv.FormatBool(key.inRestrictedSQL))...)
key.hash = append(key.hash, hack.Slice(strconv.FormatBool(key.restrictedReadOnly))...)
key.hash = append(key.hash, hack.Slice(strconv.FormatBool(key.TiDBSuperReadOnly))...)
}
return key.hash
}
// SetPstmtIDSchemaVersion implements PstmtCacheKeyMutator interface to change pstmtID and schemaVersion of cacheKey.
// so we can reuse Key instead of new every time.
func SetPstmtIDSchemaVersion(key kvcache.Key, stmtText string, schemaVersion int64, isolationReadEngines map[kv.StoreType]struct{}) {
psStmtKey, isPsStmtKey := key.(*planCacheKey)
if !isPsStmtKey {
return
}
psStmtKey.stmtText = stmtText
psStmtKey.schemaVersion = schemaVersion
psStmtKey.isolationReadEngines = make(map[kv.StoreType]struct{})
for k, v := range isolationReadEngines {
psStmtKey.isolationReadEngines[k] = v
}
psStmtKey.hash = psStmtKey.hash[:0]
}
// NewPlanCacheKey creates a new planCacheKey object.
// Note: lastUpdatedSchemaVersion will only be set in the case of rc or for update read in order to
// differentiate the cache key. In other cases, it will be 0.
func NewPlanCacheKey(sessionVars *variable.SessionVars, stmtText, stmtDB string, schemaVersion int64,
lastUpdatedSchemaVersion int64, bindSQL string) (kvcache.Key, error) {
if stmtText == "" {
return nil, errors.New("no statement text")
}
if schemaVersion == 0 {
return nil, errors.New("Schema version uninitialized")
}
if stmtDB == "" {
stmtDB = sessionVars.CurrentDB
}
timezoneOffset := 0
if sessionVars.TimeZone != nil {
_, timezoneOffset = time.Now().In(sessionVars.TimeZone).Zone()
}
key := &planCacheKey{
database: stmtDB,
connID: sessionVars.ConnectionID,
stmtText: stmtText,
schemaVersion: schemaVersion,
lastUpdatedSchemaVersion: lastUpdatedSchemaVersion,
sqlMode: sessionVars.SQLMode,
timezoneOffset: timezoneOffset,
isolationReadEngines: make(map[kv.StoreType]struct{}),
selectLimit: sessionVars.SelectLimit,
bindSQL: bindSQL,
inRestrictedSQL: sessionVars.InRestrictedSQL,
restrictedReadOnly: variable.RestrictedReadOnly.Load(),
TiDBSuperReadOnly: variable.VarTiDBSuperReadOnly.Load(),
}
for k, v := range sessionVars.IsolationReadEngines {
key.isolationReadEngines[k] = v
}
return key, nil
}
// FieldSlice is the slice of the types.FieldType
type FieldSlice []*types.FieldType
// CheckTypesCompatibility4PC compares FieldSlice with []*types.FieldType
// Currently this is only used in plan cache to check whether the types of parameters are compatible.
// If the types of parameters are compatible, we can use the cached plan.
func (s FieldSlice) CheckTypesCompatibility4PC(tps []*types.FieldType) bool {
if len(s) != len(tps) {
return false
}
for i := range tps {
// We only use part of logic of `func (ft *FieldType) Equal(other *FieldType)` here because (1) only numeric and
// string types will show up here, and (2) we don't need flen and decimal to be matched exactly to use plan cache
tpEqual := (s[i].GetType() == tps[i].GetType()) ||
(s[i].GetType() == mysql.TypeVarchar && tps[i].GetType() == mysql.TypeVarString) ||
(s[i].GetType() == mysql.TypeVarString && tps[i].GetType() == mysql.TypeVarchar) ||
// TypeNull should be considered the same as other types.
(s[i].GetType() == mysql.TypeNull || tps[i].GetType() == mysql.TypeNull)
if !tpEqual || s[i].GetCharset() != tps[i].GetCharset() || s[i].GetCollate() != tps[i].GetCollate() ||
(s[i].EvalType() == types.ETInt && mysql.HasUnsignedFlag(s[i].GetFlag()) != mysql.HasUnsignedFlag(tps[i].GetFlag())) {
return false
}
// When the type is decimal, we should compare the Flen and Decimal.
// We can only use the plan when both Flen and Decimal should less equal than the cached one.
// We assume here that there is no correctness problem when the precision of the parameters is less than the precision of the parameters in the cache.
if tpEqual && s[i].GetType() == mysql.TypeNewDecimal && !(s[i].GetFlen() >= tps[i].GetFlen() && s[i].GetDecimal() >= tps[i].GetDecimal()) {
return false
}
}
return true
}
// PlanCacheValue stores the cached Statement and StmtNode.
type PlanCacheValue struct {
Plan Plan
OutPutNames []*types.FieldName
TblInfo2UnionScan map[*model.TableInfo]bool
ParamTypes FieldSlice
}
func (v *PlanCacheValue) varTypesUnchanged(txtVarTps []*types.FieldType) bool {
return v.ParamTypes.CheckTypesCompatibility4PC(txtVarTps)
}
// NewPlanCacheValue creates a SQLCacheValue.
func NewPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.TableInfo]bool,
paramTypes []*types.FieldType) *PlanCacheValue {
dstMap := make(map[*model.TableInfo]bool)
for k, v := range srcMap {
dstMap[k] = v
}
userParamTypes := make([]*types.FieldType, len(paramTypes))
for i, tp := range paramTypes {
userParamTypes[i] = tp.Clone()
}
return &PlanCacheValue{
Plan: plan,
OutPutNames: names,
TblInfo2UnionScan: dstMap,
ParamTypes: userParamTypes,
}
}
// PlanCacheStmt store prepared ast from PrepareExec and other related fields
type PlanCacheStmt struct {
PreparedAst *ast.Prepared
StmtDB string // which DB the statement will be processed over
VisitInfos []visitInfo
ColumnInfos interface{}
Executor interface{}
NormalizedSQL string
NormalizedPlan string
SQLDigest *parser.Digest
PlanDigest *parser.Digest
ForUpdateRead bool
SnapshotTSEvaluator func(sessionctx.Context) (uint64, error)
NormalizedSQL4PC string
SQLDigest4PC string
// the different between NormalizedSQL, NormalizedSQL4PC and StmtText:
// for the query `select * from t where a>1 and b<?`, then
// NormalizedSQL: select * from `t` where `a` > ? and `b` < ? --> constants are normalized to '?',
// NormalizedSQL4PC: select * from `test` . `t` where `a` > ? and `b` < ? --> schema name is added,
// StmtText: select * from t where a>1 and b <? --> just format the original query;
StmtText string
}
// GetPreparedStmt extract the prepared statement from the execute statement.
func GetPreparedStmt(stmt *ast.ExecuteStmt, vars *variable.SessionVars) (*PlanCacheStmt, error) {
if stmt.PrepStmt != nil {
return stmt.PrepStmt.(*PlanCacheStmt), nil
}
if stmt.Name != "" {
prepStmt, err := vars.GetPreparedStmtByName(stmt.Name)
if err != nil {
return nil, err
}
stmt.PrepStmt = prepStmt
return prepStmt.(*PlanCacheStmt), nil
}
return nil, ErrStmtNotFound
}