Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: use normalSQL to build the plan cache key #31441

Merged
merged 2 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (e *DeallocateExec) Next(ctx context.Context, req *chunk.Chunk) error {
delete(vars.PreparedStmtNameToID, e.Name)
if plannercore.PreparedPlanCacheEnabled() {
e.ctx.PreparedPlanCache().Delete(plannercore.NewPSTMTPlanCacheKey(
vars, id, prepared.SchemaVersion,
vars, preparedObj.NormalizedSQL, prepared.SchemaVersion,
))
}
vars.RemovePreparedStmt(id)
Expand Down
24 changes: 4 additions & 20 deletions planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func PreparedPlanCacheEnabled() bool {
type pstmtPlanCacheKey struct {
database string
connID uint64
pstmtID uint32
normalizedSQL string
schemaVersion int64
sqlMode mysql.SQLMode
timezoneOffset int
Expand All @@ -90,7 +90,7 @@ func (key *pstmtPlanCacheKey) Hash() []byte {
}
key.hash = append(key.hash, dbBytes...)
key.hash = codec.EncodeInt(key.hash, int64(key.connID))
key.hash = codec.EncodeInt(key.hash, int64(key.pstmtID))
key.hash = append(key.hash, hack.Slice(key.normalizedSQL)...)
key.hash = codec.EncodeInt(key.hash, key.schemaVersion)
key.hash = codec.EncodeInt(key.hash, int64(key.sqlMode))
key.hash = codec.EncodeInt(key.hash, int64(key.timezoneOffset))
Expand All @@ -108,32 +108,16 @@ func (key *pstmtPlanCacheKey) Hash() []byte {
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, pstmtID uint32, schemaVersion int64, isolationReadEngines map[kv.StoreType]struct{}) {
psStmtKey, isPsStmtKey := key.(*pstmtPlanCacheKey)
if !isPsStmtKey {
return
}
psStmtKey.pstmtID = pstmtID
psStmtKey.schemaVersion = schemaVersion
psStmtKey.isolationReadEngines = make(map[kv.StoreType]struct{})
for k, v := range isolationReadEngines {
psStmtKey.isolationReadEngines[k] = v
}
psStmtKey.hash = psStmtKey.hash[:0]
}

// NewPSTMTPlanCacheKey creates a new pstmtPlanCacheKey object.
func NewPSTMTPlanCacheKey(sessionVars *variable.SessionVars, pstmtID uint32, schemaVersion int64) kvcache.Key {
func NewPSTMTPlanCacheKey(sessionVars *variable.SessionVars, normalizedSQL string, schemaVersion int64) kvcache.Key {
timezoneOffset := 0
if sessionVars.TimeZone != nil {
_, timezoneOffset = time.Now().In(sessionVars.TimeZone).Zone()
}
key := &pstmtPlanCacheKey{
database: sessionVars.CurrentDB,
connID: sessionVars.ConnectionID,
pstmtID: pstmtID,
normalizedSQL: normalizedSQL,
schemaVersion: schemaVersion,
sqlMode: sessionVars.SQLMode,
timezoneOffset: timezoneOffset,
Expand Down
4 changes: 2 additions & 2 deletions planner/core/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ func TestCacheKey(t *testing.T) {
ctx.GetSessionVars().SQLMode = mysql.ModeNone
ctx.GetSessionVars().TimeZone = time.UTC
ctx.GetSessionVars().ConnectionID = 0
key := NewPSTMTPlanCacheKey(ctx.GetSessionVars(), 1, 1)
require.Equal(t, []byte{0x74, 0x65, 0x73, 0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x74, 0x69, 0x64, 0x62, 0x74, 0x69, 0x6b, 0x76, 0x74, 0x69, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, key.Hash())
key := NewPSTMTPlanCacheKey(ctx.GetSessionVars(), "", 1)
require.Equal(t, []byte{0x74, 0x65, 0x73, 0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x74, 0x69, 0x64, 0x62, 0x74, 0x69, 0x6b, 0x76, 0x74, 0x69, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, key.Hash())
}
4 changes: 2 additions & 2 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (e *Execute) getPhysicalPlan(ctx context.Context, sctx sessionctx.Context,
stmtCtx.UseCache = prepared.UseCache
var cacheKey kvcache.Key
if prepared.UseCache {
cacheKey = NewPSTMTPlanCacheKey(sctx.GetSessionVars(), e.ExecID, prepared.SchemaVersion)
cacheKey = NewPSTMTPlanCacheKey(sctx.GetSessionVars(), preparedStmt.NormalizedSQL, prepared.SchemaVersion)
}
tps := make([]*types.FieldType, len(e.UsingVars))
for i, param := range e.UsingVars {
Expand Down Expand Up @@ -485,7 +485,7 @@ REBUILD:
// rebuild key to exclude kv.TiFlash when stmt is not read only
if _, isolationReadContainTiFlash := sessVars.IsolationReadEngines[kv.TiFlash]; isolationReadContainTiFlash && !IsReadOnly(stmt, sessVars) {
delete(sessVars.IsolationReadEngines, kv.TiFlash)
cacheKey = NewPSTMTPlanCacheKey(sctx.GetSessionVars(), e.ExecID, prepared.SchemaVersion)
cacheKey = NewPSTMTPlanCacheKey(sctx.GetSessionVars(), preparedStmt.NormalizedSQL, prepared.SchemaVersion)
sessVars.IsolationReadEngines[kv.TiFlash] = struct{}{}
}
cached := NewPSTMTPlanCacheValue(p, names, stmtCtx.TblInfo2UnionScan, tps)
Expand Down
10 changes: 0 additions & 10 deletions server/driver_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"crypto/tls"
"sync/atomic"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
Expand Down Expand Up @@ -158,15 +157,6 @@ func (ts *TiDBStatement) Close() error {
return err
}
} else {
if core.PreparedPlanCacheEnabled() {
preparedPointer := ts.ctx.GetSessionVars().PreparedStmts[ts.id]
preparedObj, ok := preparedPointer.(*core.CachedPrepareStmt)
if !ok {
return errors.Errorf("invalid CachedPrepareStmt type")
}
ts.ctx.PreparedPlanCache().Delete(core.NewPSTMTPlanCacheKey(
ts.ctx.GetSessionVars(), ts.id, preparedObj.PreparedAst.SchemaVersion))
}
ts.ctx.GetSessionVars().RemovePreparedStmt(ts.id)
}
delete(ts.ctx.stmts, int(ts.id))
Expand Down
22 changes: 8 additions & 14 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,22 +305,16 @@ func (s *session) cleanRetryInfo() {
planCacheEnabled := plannercore.PreparedPlanCacheEnabled()
var cacheKey kvcache.Key
var preparedAst *ast.Prepared
if planCacheEnabled {
firstStmtID := retryInfo.DroppedPreparedStmtIDs[0]
if preparedPointer, ok := s.sessionVars.PreparedStmts[firstStmtID]; ok {
preparedObj, ok := preparedPointer.(*plannercore.CachedPrepareStmt)
if ok {
preparedAst = preparedObj.PreparedAst
cacheKey = plannercore.NewPSTMTPlanCacheKey(s.sessionVars, firstStmtID, preparedAst.SchemaVersion)
}
}
}
for i, stmtID := range retryInfo.DroppedPreparedStmtIDs {
for _, stmtID := range retryInfo.DroppedPreparedStmtIDs {
if planCacheEnabled {
if i > 0 && preparedAst != nil {
plannercore.SetPstmtIDSchemaVersion(cacheKey, stmtID, preparedAst.SchemaVersion, s.sessionVars.IsolationReadEngines)
if preparedPointer, ok := s.sessionVars.PreparedStmts[stmtID]; ok {
preparedObj, ok := preparedPointer.(*plannercore.CachedPrepareStmt)
if ok {
preparedAst = preparedObj.PreparedAst
cacheKey = plannercore.NewPSTMTPlanCacheKey(s.sessionVars, preparedObj.NormalizedSQL, preparedAst.SchemaVersion)
s.PreparedPlanCache().Delete(cacheKey)
}
}
s.PreparedPlanCache().Delete(cacheKey)
}
s.sessionVars.RemovePreparedStmt(stmtID)
}
Expand Down