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: add binding info for explain stmt #26403

Merged
merged 6 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,24 @@ func (s *testSuite) TestReCreateBind(c *C) {
c.Assert(rows[0][3], Equals, "using")
}

func (s *testSuite) TestExplainShowBindSQL(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, key(a))")

tk.MustExec("create global binding for select * from t using select * from t use index(a)")
tk.MustQuery("select original_sql, bind_sql from mysql.bind_info where default_db != 'mysql'").Check(testkit.Rows(
"select * from `test` . `t` SELECT * FROM `test`.`t` USE INDEX (`a`)",
))

tk.MustExec("explain select * from t")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 Using the bindSQL: SELECT * FROM `test`.`t` USE INDEX (`a`)"))
tk.MustExec("explain analyze select * from t")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 Using the bindSQL: SELECT * FROM `test`.`t` USE INDEX (`a`)"))
}

func (s *testSuite) TestDMLIndexHintBind(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
Expand Down
12 changes: 11 additions & 1 deletion planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,17 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
if binding != nil && binding.Status == bindinfo.Using {
if sctx.GetSessionVars().UsePlanBaselines {
stmtHints, warns = handleStmtHints(binding.Hint.GetFirstTableHints())
if _, ok := stmtNode.(*ast.ExplainStmt); ok {
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("Using the bindSQL: %v", binding.BindSQL))
}
}
return bestPlan, names, nil
}
bestCostAmongHints := math.MaxFloat64
var bestPlanAmongHints plannercore.Plan
var (
bestPlanAmongHints plannercore.Plan
bestPlanBindSQL string
)
originHints := hint.CollectHint(stmtNode)
// Try to find the best binding.
for _, binding := range bindRecord.Bindings {
Expand All @@ -201,8 +207,12 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
}
bestCostAmongHints = cost
bestPlanAmongHints = plan
bestPlanBindSQL = binding.BindSQL
}
}
if _, ok := stmtNode.(*ast.ExplainStmt); ok && bestPlanBindSQL != "" {
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("Using the bindSQL: %v", bestPlanBindSQL))
}
// 1. If it is a select query.
// 2. If there is already a evolution task, we do not need to handle it again.
// 3. If the origin binding contain `read_from_storage` hint, we should ignore the evolve task.
Expand Down