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: convert schema_name from NULL to "" for index advisor #56943

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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