perf: cache reserved bind variables in queries#7698
Conversation
Signed-off-by: Vicent Marti <vmg@strn.cat>
Signed-off-by: Vicent Marti <vmg@strn.cat>
Signed-off-by: Vicent Marti <vmg@strn.cat>
harshit-gangal
left a comment
There was a problem hiding this comment.
Overall looks good.
I have one question.
Can the reserved Bindvars be exposed as a method on Statement Interface than passing it through all the methods?
|
@harshit-gangal: that was the first design I attempted: type BindableStatement struct {
Statement
KnownBinds BindVars
}I tried wrapping all the statements that had bindable variables with a |
harshit-gangal
left a comment
There was a problem hiding this comment.
more conflicts to resolve
Description
This week we're starting our performance mission earlier with a long-standing TODO from @sougou in the SQL parser:
GetBindvarsis not as cheap as it could be. It is, in fact, quite expensive! Walking the AST is hard! If we look at a profile for aNormalizebenchmark, we find a surprising result:GetBindvarsaccounts for almost 50% the time spent when normalizing a query! And we're doing it twice in the normal flow of a request: once when normalizing the incoming query, and again in the planbuilder when rewriting the query into its final plan. @sougou is quite right that "ideally, this should be done only once"... But we can do even better: ideally this shouldn't be done at all!This PR removes all the usages of
GetBindvarsfrom the codebase -- instead, it updates our SQL grammar so it keeps track of the bind variables as it finds them while parsing. Then, we propagate these reserved variable names everywhere they are needed. A bit verbose, but it makes normalization twice as fast (as one would expect from looking at the flame graph):Obviously, this is in a synthetic benchmark -- in a production environment, the speedup will be even more significant because we're no longer calling
GetBindvarsredundantly from the query planner.Related Issue(s)
Checklist
Deployment Notes
Impacted Areas in Vitess
Components that this PR will affect: