Skip to content

Commit

Permalink
planner: convert schema_name from NULL to "" for index advisor (#56943)
Browse files Browse the repository at this point in the history
ref #12303
  • Loading branch information
qw4990 authored Oct 29, 2024
1 parent c32c7b2 commit a9c9208
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/planner/indexadvisor/indexadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ func prepareQuerySet(ctx context.Context, sctx sessionctx.Context,
}

func loadQuerySetFromStmtSummary(sctx sessionctx.Context, option *Option) (s.Set[Query], error) {
template := `SELECT any_value(schema_name) as schema_name,
template := `SELECT any_value(ifnull(schema_name, "")) as schema_name,
any_value(query_sample_text) as query_sample_text,
sum(cast(exec_count as double)) as exec_count
FROM information_schema.statements_summary_history
WHERE stmt_type = "Select" AND
summary_begin_time >= date_sub(now(), interval 1 day) AND
prepared = 0 AND
upper(schema_name) not in ("MYSQL", "INFORMATION_SCHEMA", "METRICS_SCHEMA", "PERFORMANCE_SCHEMA")
upper(ifnull(schema_name, "")) not in ("MYSQL", "INFORMATION_SCHEMA", "METRICS_SCHEMA", "PERFORMANCE_SCHEMA")
GROUP BY digest
ORDER BY sum(exec_count) DESC
LIMIT %?`
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/stmtsummary/v2/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_test(
"table_test.go",
],
flaky = True,
shard_count = 13,
shard_count = 14,
deps = [
"//pkg/config",
"//pkg/kv",
Expand Down
27 changes: 27 additions & 0 deletions pkg/util/stmtsummary/v2/tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ func TestStmtSummaryIndexAdvisor(t *testing.T) {
"b \"Column [b] appear in Equal or Range Predicate clause(s) in query: select `b` from `test` . `t` where `b` = ?\""))
}

func TestStmtSummaryIndexAdvisorNullSchema(t *testing.T) {
setupStmtSummary()
defer closeStmtSummary()
store := testkit.CreateMockStore(t)
tk := newTestKitWithRoot(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int, b int, c int)`)

tk.MustQueryToErr(`recommend index run`) // no query

tk = testkit.NewTestKit(t, store)
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil, nil))
tk.MustQuery(`select a from test.t where a=1`) // schema_name in statement_summary is NULL

rs := tk.MustQuery(`recommend index run`).Sort().Rows()
require.Equal(t, rs[0][2], "idx_a")

tk.MustQuery(`select b from test.t where b=1`)
rs = tk.MustQuery(`recommend index run`).Sort().Rows()
require.Equal(t, rs[0][2], "idx_a")
require.Equal(t, rs[1][2], "idx_b")

tk.MustQuery(`select index_columns, index_details->'$.Reason' from mysql.index_advisor_results`).Check(
testkit.Rows("a \"Column [a] appear in Equal or Range Predicate clause(s) in query: select `a` from `test` . `t` where `a` = ?\"",
"b \"Column [b] appear in Equal or Range Predicate clause(s) in query: select `b` from `test` . `t` where `b` = ?\""))
}

func TestStmtSummaryTable(t *testing.T) {
setupStmtSummary()
defer closeStmtSummary()
Expand Down

0 comments on commit a9c9208

Please sign in to comment.