Skip to content

Commit

Permalink
bindinfo: reduce duplicate DigestHash call (#10352)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored and ngaut committed May 6, 2019
1 parent 461512b commit 190990b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
16 changes: 10 additions & 6 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func (s *testSuite) TestBindParse(c *C) {
c.Check(err, IsNil)
c.Check(bindHandle.Size(), Equals, 1)

bindData := bindHandle.GetBindRecord("select * from t", "test")
sql, hash := parser.NormalizeDigest("select * from t")
bindData := bindHandle.GetBindRecord(hash, sql, "test")
c.Check(bindData, NotNil)
c.Check(bindData.OriginalSQL, Equals, "select * from t")
c.Check(bindData.BindSQL, Equals, "select * from t use index(index_t)")
Expand Down Expand Up @@ -151,7 +152,9 @@ func (s *testSuite) TestGlobalBinding(c *C) {
_, err = tk.Exec("create global binding for select * from t where i>99 using select * from t use index(index_t) where i>99")
c.Assert(err, IsNil)

bindData := s.domain.BindHandle().GetBindRecord("select * from t where i > ?", "test")
sql, hash := parser.NormalizeDigest("select * from t where i > ?")

bindData := s.domain.BindHandle().GetBindRecord(hash, sql, "test")
c.Check(bindData, NotNil)
c.Check(bindData.OriginalSQL, Equals, "select * from t where i > ?")
c.Check(bindData.BindSQL, Equals, "select * from t use index(index_t) where i>99")
Expand Down Expand Up @@ -183,7 +186,7 @@ func (s *testSuite) TestGlobalBinding(c *C) {
c.Check(err, IsNil)
c.Check(bindHandle.Size(), Equals, 1)

bindData = bindHandle.GetBindRecord("select * from t where i > ?", "test")
bindData = bindHandle.GetBindRecord(hash, sql, "test")
c.Check(bindData, NotNil)
c.Check(bindData.OriginalSQL, Equals, "select * from t where i > ?")
c.Check(bindData.BindSQL, Equals, "select * from t use index(index_t) where i>99")
Expand All @@ -196,15 +199,15 @@ func (s *testSuite) TestGlobalBinding(c *C) {

_, err = tk.Exec("DROP global binding for select * from t where i>100")
c.Check(err, IsNil)
bindData = s.domain.BindHandle().GetBindRecord("select * from t where i > ?", "test")
bindData = s.domain.BindHandle().GetBindRecord(hash, sql, "test")
c.Check(bindData, IsNil)

bindHandle = bindinfo.NewBindHandle(tk.Se)
err = bindHandle.Update(true)
c.Check(err, IsNil)
c.Check(bindHandle.Size(), Equals, 0)

bindData = bindHandle.GetBindRecord("select * from t where i > ?", "test")
bindData = bindHandle.GetBindRecord(hash, sql, "test")
c.Check(bindData, IsNil)

rs, err = tk.Exec("show global bindings")
Expand Down Expand Up @@ -400,7 +403,8 @@ func (s *testSuite) TestErrorBind(c *C) {
_, err := tk.Exec("create global binding for select * from t where i>100 using select * from t use index(index_t) where i>100")
c.Assert(err, IsNil, Commentf("err %v", err))

bindData := s.domain.BindHandle().GetBindRecord("select * from t where i > ?", "test")
sql, hash := parser.NormalizeDigest("select * from t where i > ?")
bindData := s.domain.BindHandle().GetBindRecord(hash, sql, "test")
c.Check(bindData, NotNil)
c.Check(bindData.OriginalSQL, Equals, "select * from t where i > ?")
c.Check(bindData.BindSQL, Equals, "select * from t use index(index_t) where i>100")
Expand Down
7 changes: 3 additions & 4 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ func (h *BindHandle) Size() int {
}

// GetBindRecord return the bindMeta of the (normdOrigSQL,db) if bindMeta exist.
func (h *BindHandle) GetBindRecord(normdOrigSQL, db string) *BindMeta {
return h.bindInfo.Load().(cache).getBindRecord(normdOrigSQL, db)
func (h *BindHandle) GetBindRecord(hash, normdOrigSQL, db string) *BindMeta {
return h.bindInfo.Load().(cache).getBindRecord(hash, normdOrigSQL, db)
}

// GetAllBindRecord return all bind record in cache.
Expand Down Expand Up @@ -387,8 +387,7 @@ func copyInvalidBindRecordMap(oldMap map[string]*invalidBindRecordMap) map[strin
return newMap
}

func (c cache) getBindRecord(normdOrigSQL, db string) *BindMeta {
hash := parser.DigestHash(normdOrigSQL)
func (c cache) getBindRecord(hash, normdOrigSQL, db string) *BindMeta {
bindRecords := c[hash]
if bindRecords != nil {
for _, bindRecord := range bindRecords {
Expand Down
12 changes: 7 additions & 5 deletions executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,19 @@ func addHint(ctx sessionctx.Context, stmtNode ast.StmtNode) ast.StmtNode {
normalizeExplainSQL := parser.Normalize(x.Text())
idx := strings.Index(normalizeExplainSQL, "select")
normalizeSQL := normalizeExplainSQL[idx:]
x.Stmt = addHintForSelect(normalizeSQL, ctx, x.Stmt)
hash := parser.DigestHash(normalizeSQL)
x.Stmt = addHintForSelect(hash, normalizeSQL, ctx, x.Stmt)
}
return x
case *ast.SelectStmt:
return addHintForSelect(parser.Normalize(x.Text()), ctx, x)
normalizeSQL, hash := parser.NormalizeDigest(x.Text())
return addHintForSelect(hash, normalizeSQL, ctx, x)
default:
return stmtNode
}
}

func addHintForSelect(normdOrigSQL string, ctx sessionctx.Context, stmt ast.StmtNode) ast.StmtNode {
func addHintForSelect(hash, normdOrigSQL string, ctx sessionctx.Context, stmt ast.StmtNode) ast.StmtNode {
sessionHandle := ctx.Value(bindinfo.SessionBindInfoKeyType).(*bindinfo.SessionHandle)
bindRecord := sessionHandle.GetBindRecord(normdOrigSQL, ctx.GetSessionVars().CurrentDB)
if bindRecord != nil {
Expand All @@ -418,9 +420,9 @@ func addHintForSelect(normdOrigSQL string, ctx sessionctx.Context, stmt ast.Stmt
}
}
globalHandle := domain.GetDomain(ctx).BindHandle()
bindRecord = globalHandle.GetBindRecord(normdOrigSQL, ctx.GetSessionVars().CurrentDB)
bindRecord = globalHandle.GetBindRecord(hash, normdOrigSQL, ctx.GetSessionVars().CurrentDB)
if bindRecord == nil {
bindRecord = globalHandle.GetBindRecord(normdOrigSQL, "")
bindRecord = globalHandle.GetBindRecord(hash, normdOrigSQL, "")
}
if bindRecord != nil {
return bindinfo.BindHint(stmt, bindRecord.Ast)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e
github.com/pingcap/kvproto v0.0.0-20190327032727-3d8cb3a30d5d
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596
github.com/pingcap/parser v0.0.0-20190429120706-c378059f7f42
github.com/pingcap/parser v0.0.0-20190505092803-4542e963c7f1
github.com/pingcap/pd v0.0.0-20190424024702-bd1e2496a669
github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible
github.com/pingcap/tipb v0.0.0-20190428032612-535e1abaa330
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ github.com/pingcap/kvproto v0.0.0-20190327032727-3d8cb3a30d5d/go.mod h1:QMdbTAXC
github.com/pingcap/log v0.0.0-20190214045112-b37da76f67a7/go.mod h1:xsfkWVaFVV5B8e1K9seWfyJWFrIhbtUTAD8NV1Pq3+w=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 h1:t2OQTpPJnrPDGlvA+3FwJptMTt6MEPdzK1Wt99oaefQ=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw=
github.com/pingcap/parser v0.0.0-20190429120706-c378059f7f42 h1:iuZ/y1DLC/4gLTxw/xtEbo9R2SMRq7CKzLXhffff/kc=
github.com/pingcap/parser v0.0.0-20190429120706-c378059f7f42/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/parser v0.0.0-20190505092803-4542e963c7f1 h1:YvxFABfyD5Pnp80FUVV4w3zdlmkcwRhQbn7xpTjBwwU=
github.com/pingcap/parser v0.0.0-20190505092803-4542e963c7f1/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/pd v0.0.0-20190424024702-bd1e2496a669 h1:ZoKjndm/Ig7Ru/wojrQkc/YLUttUdQXoH77gtuWCvL4=
github.com/pingcap/pd v0.0.0-20190424024702-bd1e2496a669/go.mod h1:MUCxRzOkYiWZtlyi4MhxjCIj9PgQQ/j+BLNGm7aUsnM=
github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible h1:MkWCxgZpJBgY2f4HtwWMMFzSBb3+JPzeJgF3VrXE/bU=
Expand Down
9 changes: 5 additions & 4 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,19 +1046,20 @@ func (s *session) execute(ctx context.Context, sql string) (recordSets []sqlexec
}

func (s *session) handleInvalidBindRecord(ctx context.Context, stmtNode ast.StmtNode) {
var normdOrigSQL string
var normdOrigSQL, hash string
switch x := stmtNode.(type) {
case *ast.ExplainStmt:
switch x.Stmt.(type) {
case *ast.SelectStmt:
normalizeExplainSQL := parser.Normalize(x.Text())
idx := strings.Index(normalizeExplainSQL, "select")
normdOrigSQL = normalizeExplainSQL[idx:]
hash = parser.DigestHash(normdOrigSQL)
default:
return
}
case *ast.SelectStmt:
normdOrigSQL = parser.Normalize(x.Text())
normdOrigSQL, hash = parser.NormalizeDigest(x.Text())
default:
return
}
Expand All @@ -1070,9 +1071,9 @@ func (s *session) handleInvalidBindRecord(ctx context.Context, stmtNode ast.Stmt
}

globalHandle := domain.GetDomain(s).BindHandle()
bindMeta = globalHandle.GetBindRecord(normdOrigSQL, s.GetSessionVars().CurrentDB)
bindMeta = globalHandle.GetBindRecord(hash, normdOrigSQL, s.GetSessionVars().CurrentDB)
if bindMeta == nil {
bindMeta = globalHandle.GetBindRecord(normdOrigSQL, "")
bindMeta = globalHandle.GetBindRecord(hash, normdOrigSQL, "")
}
if bindMeta != nil {
record := &bindinfo.BindRecord{
Expand Down

0 comments on commit 190990b

Please sign in to comment.