forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We want to collect index recommendations per statement and save it to the statement_statistics table. To accomplish this, a new cache map was created. The key for the map is a combination of the statement fingerprint, the database and the plan hash, since those are the parameters that would affect an index recommendation (from the parameters we use as keys for the statement fingerprint ID at least). This cache has a limit of unique recommendations, limited by a new cluster setting called `sql.metrics.statement_details.max_mem_reported_idx_recommendations` with a default value of 100k. If this limit is reached, we delete entries that had their last update made more than 24h ago. A new recommendations is generated if has been more than 1h since the last recommendation (saved on cache) and it has at least a few executions (so we handle the case where we have just one execution of several different fingerprints, and is not worth calculating recommendations for them). The recommendations are saved as a list, with each recommendations being "type : sql command", e.g. `{"creation : CREATE INDEX ON t2 (i) STORING (k);"}` and `{"replacement : CREATE UNIQUE INDEX ON t1 (i) STORING (k); DROP INDEX t1@existing_t1_i;"}` Closes cockroachdb#83782 Release note (sql change): Index recommendations are generated for statements. New recommendations are generated every hour and available on `system.statement_statistics` and `crdb_internal.statement_statistics`. New cluster setting with default value of 100k sql.metrics.statement_details.max_mem_reported_idx_recommendations
- Loading branch information
Showing
16 changed files
with
620 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data") | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "idxrecommendations", | ||
srcs = [ | ||
"idx_recommendations.go", | ||
"idx_recommendations_cache.go", | ||
], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/sql/idxrecommendations", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/settings", | ||
"//pkg/settings/cluster", | ||
"//pkg/sql/sem/tree", | ||
"//pkg/sql/sqlstats", | ||
"//pkg/util/syncutil", | ||
"//pkg/util/timeutil", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "idxrecommendations_test", | ||
srcs = [ | ||
"idx_recommendations_cache_test.go", | ||
"main_test.go", | ||
], | ||
deps = [ | ||
":idxrecommendations", | ||
"//pkg/security/securityassets", | ||
"//pkg/security/securitytest", | ||
"//pkg/server", | ||
"//pkg/sql/tests", | ||
"//pkg/testutils/serverutils", | ||
"//pkg/testutils/sqlutils", | ||
"//pkg/testutils/testcluster", | ||
"//pkg/util/leaktest", | ||
"//pkg/util/log", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) | ||
|
||
get_x_data(name = "get_x_data") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package idxrecommendations | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
) | ||
|
||
// IdxRecommendations controls the generation of index recommendations | ||
// for specific statements, and update accordingly. | ||
type IdxRecommendations interface { | ||
ShouldGenerateIndexRecommendation( | ||
fingerprint string, planHash uint64, database string, stmtType tree.StatementType, | ||
) bool | ||
UpdateIndexRecommendations( | ||
fingerprint string, | ||
planHash uint64, | ||
database string, | ||
stmtType tree.StatementType, | ||
recommendations []string, | ||
reset bool, | ||
) []string | ||
} | ||
|
||
// FormatIdxRecommendations received a list with recommendations info, e.g.: | ||
//{ | ||
// "index recommendations: 2", | ||
// "1. type: index replacement", | ||
// "SQL commands: CREATE UNIQUE INDEX ON t1 (i) STORING (k); DROP INDEX t1@existing_t1_i;", | ||
// "2. type: index creation", | ||
// "SQL command: CREATE INDEX ON t2 (i) STORING (k);", | ||
//} | ||
// and returns a list of type and recommendations, e.g.: | ||
//{ | ||
// "replacement : CREATE UNIQUE INDEX ON t1 (i) STORING (k); DROP INDEX t1@existing_t1_i;", | ||
// "creation : CREATE INDEX ON t2 (i) STORING (k);", | ||
//} | ||
func FormatIdxRecommendations(idxRec []string) []string { | ||
recommendations := []string{} | ||
if len(idxRec) == 0 { | ||
return recommendations | ||
} | ||
|
||
var recType string | ||
var recCommand string | ||
var rec string | ||
|
||
for i := 1; i < len(idxRec); i++ { | ||
recType = strings.Split(idxRec[i], "type: index ")[1] | ||
recCommand = strings.Replace(idxRec[i+1], " SQL command: ", "", 1) | ||
recCommand = strings.Replace(recCommand, " SQL commands: ", "", 1) | ||
rec = fmt.Sprintf("%s : %s", recType, recCommand) | ||
recommendations = append(recommendations, rec) | ||
i++ | ||
} | ||
|
||
return recommendations | ||
} |
Oops, something went wrong.