Skip to content

Commit

Permalink
planner: output create-index statements for recommend index run (pi…
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored Dec 16, 2024
1 parent 0dcadef commit 57a73ea
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/executor/recommend_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ func (e *RecommendIndexExec) Next(ctx context.Context, req *chunk.Chunk) error {
req.AppendString(3, strings.Join(r.IndexColumns, ","))
req.AppendString(4, fmt.Sprintf("%v", r.IndexDetail.IndexSize))
req.AppendString(5, r.IndexDetail.Reason)

jData, err := json.Marshal(r.TopImpactedQueries)
if err != nil {
return err
}
req.AppendString(6, string(jData))
req.AppendString(7, fmt.Sprintf("CREATE INDEX %s ON %s(%s);",
r.IndexName, r.Table, strings.Join(r.IndexColumns, ",")))
}
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5875,6 +5875,7 @@ func (*PlanBuilder) buildRecommendIndex(v *ast.RecommendIndexStmt) (base.Plan, e
schema.Append(buildColumnWithName("", "index_size", mysql.TypeVarchar, 256))
schema.Append(buildColumnWithName("", "reason", mysql.TypeVarchar, 256))
schema.Append(buildColumnWithName("", "top_impacted_query", mysql.TypeBlob, -1))
schema.Append(buildColumnWithName("", "create_index_statement", mysql.TypeBlob, -1))
p.setSchemaAndNames(schema.col2Schema(), schema.names)
case "set":
if len(p.Options) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/planner/indexadvisor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ go_test(
"utils_test.go",
],
flaky = True,
shard_count = 46,
shard_count = 47,
deps = [
":indexadvisor",
"//pkg/parser/mysql",
Expand Down
11 changes: 11 additions & 0 deletions pkg/planner/indexadvisor/indexadvisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,14 @@ func TestIndexAdvisorStorage(t *testing.T) {
"b,c \"Column [b c] appear in Equal or Range Predicate clause(s) in query: select `c` , `b` from `test` . `t` where `c` = ? and `b` = ?\"",
"d \"Column [d] appear in Equal or Range Predicate clause(s) in query: select `d` from `test` . `t` where `d` = ?\""))
}

func TestIndexAdvisorCreateIndexStmt(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int, b int, c int)`)
results := tk.MustQuery(`recommend index run for "select a from t where a=1"`).Rows()
require.Len(t, results, 1)
ddl := results[0][7].(string)
require.Equal(t, "CREATE INDEX idx_a ON t(a);", ddl)
}

0 comments on commit 57a73ea

Please sign in to comment.