Skip to content

Commit

Permalink
*: Revert pingcap#33519 for performance fallback and fix the pingcap#…
Browse files Browse the repository at this point in the history
…33509 in another way

This reverts commit 5004a81.
  • Loading branch information
lcwangchao committed May 6, 2022
1 parent 49aafa5 commit 9e071a0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ type CachedPrepareStmt struct {
PreparedAst *ast.Prepared
StmtDB string // which DB the statement will be processed over
VisitInfos []visitInfo
ColumnInfos interface{}
Executor interface{}
NormalizedSQL string
NormalizedPlan string
Expand Down
1 change: 1 addition & 0 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func (e *Execute) OptimizePreparedPlan(ctx context.Context, sctx sessionctx.Cont
// schema version like prepared plan cache key
prepared.CachedPlan = nil
preparedObj.Executor = nil
preparedObj.ColumnInfos = nil
// If the schema version has changed we need to preprocess it again,
// if this time it failed, the real reason for the error is schema changed.
// Example:
Expand Down
25 changes: 19 additions & 6 deletions server/driver_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func (ts *TiDBStatement) Execute(ctx context.Context, args []types.Datum) (rs Re
return
}
rs = &tidbResultSet{
recordSet: tidbRecordset,
recordSet: tidbRecordset,
preparedStmt: ts.ctx.GetSessionVars().PreparedStmts[ts.id].(*core.CachedPrepareStmt),
}
return
}
Expand Down Expand Up @@ -307,10 +308,11 @@ func (tc *TiDBContext) GetStmtStats() *stmtstats.StatementStats {
}

type tidbResultSet struct {
recordSet sqlexec.RecordSet
columns []*ColumnInfo
rows []chunk.Row
closed int32
recordSet sqlexec.RecordSet
columns []*ColumnInfo
rows []chunk.Row
closed int32
preparedStmt *core.CachedPrepareStmt
}

func (trs *tidbResultSet) NewChunk(alloc chunk.Allocator) *chunk.Chunk {
Expand Down Expand Up @@ -352,12 +354,23 @@ func (trs *tidbResultSet) Columns() []*ColumnInfo {
if trs.columns != nil {
return trs.columns
}

// for prepare statement, try to get cached columnInfo array
if trs.preparedStmt != nil {
ps := trs.preparedStmt
if colInfos, ok := ps.ColumnInfos.([]*ColumnInfo); ok {
trs.columns = colInfos
}
}
if trs.columns == nil {
fields := trs.recordSet.Fields()
for _, v := range fields {
trs.columns = append(trs.columns, convertColumnInfo(v))
}
if trs.preparedStmt != nil {
// if ColumnInfo struct has allocated object,
// here maybe we need deep copy ColumnInfo to do caching
trs.preparedStmt.ColumnInfos = trs.columns
}
}
return trs.columns
}
Expand Down
1 change: 1 addition & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,7 @@ func (s *session) IsCachedExecOk(ctx context.Context, preparedStmt *plannercore.
is := s.GetInfoSchema().(infoschema.InfoSchema)
if prepared.SchemaVersion != is.SchemaMetaVersion() {
prepared.CachedPlan = nil
preparedStmt.ColumnInfos = nil
return false, nil
}
// maybe we'd better check cached plan type here, current
Expand Down

0 comments on commit 9e071a0

Please sign in to comment.