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: optimize the performance of GetParamSQLFromAST for non-prep plan cache #43161

Merged
merged 1 commit into from
Apr 18, 2023
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
13 changes: 10 additions & 3 deletions planner/core/plan_cache_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ var (
restoreCtx := format.NewRestoreCtx(format.RestoreForNonPrepPlanCache|format.RestoreStringWithoutCharset|format.RestoreStringSingleQuotes|format.RestoreNameBackQuotes, buf)
return restoreCtx
}}
paramMakerPool = sync.Pool{New: func() interface{} {
return ast.NewParamMarkerExpr(0)
}}
)

// paramReplacer is an ast.Visitor that replaces all values with `?` and collects them.
Expand Down Expand Up @@ -72,8 +75,9 @@ func (pr *paramReplacer) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
}
case *driver.ValueExpr:
pr.params = append(pr.params, n)
param := ast.NewParamMarkerExpr(len(pr.params) - 1) // offset is used as order in non-prepared plan cache.
n.Datum.Copy(&param.(*driver.ParamMarkerExpr).Datum) // init the ParamMakerExpr's Datum
param := paramMakerPool.Get().(*driver.ParamMarkerExpr)
param.Offset = len(pr.params) - 1 // offset is used as order in non-prepared plan cache.
n.Datum.Copy(&param.Datum) // init the ParamMakerExpr's Datum
return param, true
}
return in, false
Expand Down Expand Up @@ -129,7 +133,9 @@ func (pr *paramRestorer) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
return nil, true
}
// offset is used as order in non-prepared plan cache.
return pr.params[n.Offset], true
offset := n.Offset
paramMakerPool.Put(n)
return pr.params[offset], true
}
if pr.err != nil {
return nil, true
Expand Down Expand Up @@ -162,6 +168,7 @@ func RestoreASTWithParams(ctx context.Context, _ sessionctx.Context, stmt ast.St
func Params2Expressions(params []*driver.ValueExpr) []expression.Expression {
exprs := make([]expression.Expression, 0, len(params))
for _, p := range params {
// TODO: add a sync.Pool for type.FieldType and expression.Constant here.
tp := new(types.FieldType)
types.InferParamTypeFromDatum(&p.Datum, tp)
exprs = append(exprs, &expression.Constant{
Expand Down
12 changes: 12 additions & 0 deletions planner/core/plan_cache_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,15 @@ func BenchmarkParameterizeInsert(b *testing.B) {
ParameterizeAST(context.Background(), sctx, stmt)
}
}

func BenchmarkGetParamSQL(b *testing.B) {
paymentInsertHistory := `INSERT INTO history (h_c_d_id, h_c_w_id, h_c_id, h_d_id, h_w_id, h_date, h_amount, h_data) VALUES (1, 2, 3, 4, 5, 6, 7, 8)`
stmt, err := parser.New().ParseOneStmt(paymentInsertHistory, "", "")
require.Nil(b, err)
sctx := MockContext()

b.ResetTimer()
for i := 0; i < b.N; i++ {
GetParamSQLFromAST(context.Background(), sctx, stmt)
}
}