Skip to content

Commit ca786ee

Browse files
committed
sql/hints: add per-node cache for system.statement_hints
This commit adds a per-node cache for the `system.statement_hints` table, similar to the table stats cache. The cache tracks the set of all statement fingerprint hashes that have hints in a map kept up-to-date via rangefeed. This allows queries to quickly determine whether they have hints, minimizing the overhead for unhinted queries. Hints are kept in a separate limited-capacity LRU cache. The LRU cache size can be configured by setting the `sql.hints.statement_hints_cache_size` cluster setting. Fixes #148160 Release note: None
1 parent 8d0f9c3 commit ca786ee

File tree

10 files changed

+616
-20
lines changed

10 files changed

+616
-20
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
/pkg/sql/execute.go @cockroachdb/sql-queries-prs
7171
/pkg/sql/executor_statement_metrics.go @cockroachdb/obs-prs
7272
/pkg/sql/flowinfra/ @cockroachdb/sql-queries-prs
73+
/pkg/sql/hints/ @cockroachdb/sql-queries-prs
7374
/pkg/sql/physicalplan/ @cockroachdb/sql-queries-prs
7475
/pkg/sql/row* @cockroachdb/sql-queries-prs
7576
/pkg/sql/control_job* @cockroachdb/sql-queries-prs @cockroachdb/jobs-prs

build/bazelutil/nogo_config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
"pkg/sql/contention/event_store.go": "flagged by linter, should be evaluated",
145145
"pkg/sql/distsql_running.go": "flagged by linter, should be evaluated",
146146
"pkg/sql/flowinfra/flow_registry.go": "flagged by linter, should be evaluated",
147+
"pkg/sql/hints/hint_cache.go": "flagged by linter, should be evaluated",
147148
"pkg/sql/idxusage/local_idx_usage_stats.go": "flagged by linter, should be evaluated",
148149
"pkg/sql/importer/import_processor.go": "flagged by linter, should be evaluated",
149150
"pkg/sql/importer/import_processor_planning.go": "flagged by linter, should be evaluated",

pkg/server/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ go_library(
242242
"//pkg/sql/flowinfra",
243243
"//pkg/sql/gcjob",
244244
"//pkg/sql/gcjob/gcjobnotifier",
245+
"//pkg/sql/hints",
245246
"//pkg/sql/idxusage",
246247
"//pkg/sql/importer",
247248
"//pkg/sql/inspect",

pkg/server/server_sql.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import (
8383
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
8484
"github.com/cockroachdb/cockroach/pkg/sql/flowinfra"
8585
"github.com/cockroachdb/cockroach/pkg/sql/gcjob/gcjobnotifier"
86+
"github.com/cockroachdb/cockroach/pkg/sql/hints"
8687
"github.com/cockroachdb/cockroach/pkg/sql/idxusage"
8788
"github.com/cockroachdb/cockroach/pkg/sql/isql"
8889
"github.com/cockroachdb/cockroach/pkg/sql/optionalnodeliveness"
@@ -1039,7 +1040,10 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
10391040
cfg.stopper,
10401041
),
10411042

1042-
QueryCache: querycache.New(cfg.QueryCacheSize),
1043+
QueryCache: querycache.New(cfg.QueryCacheSize),
1044+
StatementHintsCache: hints.NewStatementHintsCache(
1045+
cfg.clock, cfg.rangeFeedFactory, cfg.stopper, codec, cfg.internalDB, cfg.Settings,
1046+
),
10431047
VecIndexManager: vecIndexManager,
10441048
RowMetrics: &rowMetrics,
10451049
InternalRowMetrics: &internalRowMetrics,
@@ -1775,6 +1779,9 @@ func (s *SQLServer) preStart(
17751779
if err := s.execCfg.TableStatsCache.Start(ctx, s.execCfg.Codec, s.execCfg.RangeFeedFactory); err != nil {
17761780
return err
17771781
}
1782+
if err = s.execCfg.StatementHintsCache.Start(ctx, s.execCfg.SystemTableIDResolver); err != nil {
1783+
return err
1784+
}
17781785

17791786
scheduledlogging.Start(
17801787
ctx, stopper, s.execCfg.InternalDB, s.execCfg.Settings,

pkg/sql/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ go_library(
441441
"//pkg/sql/faketreeeval",
442442
"//pkg/sql/flowinfra",
443443
"//pkg/sql/gcjob/gcjobnotifier",
444+
"//pkg/sql/hints",
444445
"//pkg/sql/idxrecommendations",
445446
"//pkg/sql/idxusage",
446447
"//pkg/sql/inverted",

pkg/sql/catalog/systemschema/system.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5433,9 +5433,9 @@ var (
54335433
},
54345434
)
54355435

5436-
statementHintsComputeExpr = "fnv64(fingerprint)"
5437-
5438-
StatementHintsTable = makeSystemTable(
5436+
statementHintsComputeExpr = "fnv64(fingerprint)"
5437+
StatementHintsHashIndexID descpb.IndexID = 2
5438+
StatementHintsTable = makeSystemTable(
54395439
StatementHintsTableSchema,
54405440
systemTable(
54415441
catconstants.StatementHintsTableName,
@@ -5465,7 +5465,7 @@ var (
54655465
},
54665466
descpb.IndexDescriptor{
54675467
Name: "hash_idx",
5468-
ID: 2,
5468+
ID: StatementHintsHashIndexID,
54695469
Unique: false,
54705470
Version: descpb.StrictIndexColumnIDGuaranteesVersion,
54715471
KeyColumnNames: []string{"hash"},

pkg/sql/exec_util.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import (
6767
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
6868
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
6969
"github.com/cockroachdb/cockroach/pkg/sql/gcjob/gcjobnotifier"
70+
"github.com/cockroachdb/cockroach/pkg/sql/hints"
7071
"github.com/cockroachdb/cockroach/pkg/sql/idxusage"
7172
"github.com/cockroachdb/cockroach/pkg/sql/isql"
7273
"github.com/cockroachdb/cockroach/pkg/sql/lex"
@@ -1617,19 +1618,20 @@ type ExecutorConfig struct {
16171618
NodesStatusServer serverpb.OptionalNodesStatusServer
16181619
// SQLStatusServer gives access to a subset of the Status service and is
16191620
// available when not running as a system tenant.
1620-
SQLStatusServer serverpb.SQLStatusServer
1621-
TenantStatusServer serverpb.TenantStatusServer
1622-
MetricsRecorder limitedMetricsRecorder
1623-
SessionRegistry *SessionRegistry
1624-
ClosedSessionCache *ClosedSessionCache
1625-
SQLLiveness sqlliveness.Provider
1626-
JobRegistry *jobs.Registry
1627-
VirtualSchemas *VirtualSchemaHolder
1628-
DistSQLPlanner *DistSQLPlanner
1629-
TableStatsCache *stats.TableStatisticsCache
1630-
StatsRefresher *stats.Refresher
1631-
QueryCache *querycache.C
1632-
VecIndexManager *vecindex.Manager
1621+
SQLStatusServer serverpb.SQLStatusServer
1622+
TenantStatusServer serverpb.TenantStatusServer
1623+
MetricsRecorder limitedMetricsRecorder
1624+
SessionRegistry *SessionRegistry
1625+
ClosedSessionCache *ClosedSessionCache
1626+
SQLLiveness sqlliveness.Provider
1627+
JobRegistry *jobs.Registry
1628+
VirtualSchemas *VirtualSchemaHolder
1629+
DistSQLPlanner *DistSQLPlanner
1630+
TableStatsCache *stats.TableStatisticsCache
1631+
StatsRefresher *stats.Refresher
1632+
QueryCache *querycache.C
1633+
StatementHintsCache *hints.StatementHintsCache
1634+
VecIndexManager *vecindex.Manager
16331635

16341636
SchemaChangerMetrics *SchemaChangerMetrics
16351637
FeatureFlagMetrics *featureflag.DenialMetrics

pkg/sql/hints/BUILD.bazel

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,43 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
44

55
go_library(
66
name = "hints",
7-
srcs = ["statement_hint.go"],
7+
srcs = [
8+
"hint_cache.go",
9+
"statement_hint.go",
10+
],
811
embed = [":hints_go_proto"],
912
importpath = "github.com/cockroachdb/cockroach/pkg/sql/hints",
1013
visibility = ["//visibility:public"],
11-
deps = ["//pkg/util/protoutil"],
14+
deps = [
15+
"//pkg/keys",
16+
"//pkg/kv/kvclient/rangefeed",
17+
"//pkg/kv/kvclient/rangefeed/rangefeedbuffer",
18+
"//pkg/kv/kvclient/rangefeed/rangefeedcache",
19+
"//pkg/kv/kvpb",
20+
"//pkg/roachpb",
21+
"//pkg/settings",
22+
"//pkg/settings/cluster",
23+
"//pkg/sql/catalog",
24+
"//pkg/sql/catalog/catenumpb",
25+
"//pkg/sql/catalog/descpb",
26+
"//pkg/sql/catalog/descs",
27+
"//pkg/sql/catalog/systemschema",
28+
"//pkg/sql/rowenc",
29+
"//pkg/sql/sem/tree",
30+
"//pkg/sql/sessiondata",
31+
"//pkg/sql/types",
32+
"//pkg/util/buildutil",
33+
"//pkg/util/cache",
34+
"//pkg/util/hlc",
35+
"//pkg/util/log",
36+
"//pkg/util/metamorphic",
37+
"//pkg/util/protoutil",
38+
"//pkg/util/retry",
39+
"//pkg/util/startup",
40+
"//pkg/util/stop",
41+
"//pkg/util/syncutil",
42+
"@com_github_cockroachdb_errors//:errors",
43+
],
1244
)
1345

1446
go_proto_library(

0 commit comments

Comments
 (0)