Skip to content

Commit a4e6a42

Browse files
committed
explain: display number of hints for EXPLAIN
This commit adds a top-level field to the output of `EXPLAIN` and `EXPLAIN ANALYZE` showing the number of hints applied to the statement, if nonzero. Informs #121502 Release note (sql change): EXPLAIN and EXPLAIN ANALYZE will now display the number of hints from `system.statement_hints` applied to the executed statement.
1 parent 35a1a5a commit a4e6a42

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

pkg/sql/explain_plan.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ func (e *explainPlanNode) startExec(params runParams) error {
120120
}
121121
}
122122

123+
if len(params.p.stmt.Hints) > 0 {
124+
ob.AddStmtHintCount(uint64(len(params.p.stmt.Hints)))
125+
}
126+
123127
if e.options.Flags[tree.ExplainFlagJSON] {
124128
// For the JSON flag, we only want to emit the diagram JSON.
125129
rows = []string{diagramJSON}

pkg/sql/instrumentation.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ type instrumentationHelper struct {
207207
// stats scanned by this query.
208208
nanosSinceStatsForecasted time.Duration
209209

210+
// stmtHintsCount is the number of hints from system.statement_hints applied
211+
// to the statement.
212+
stmtHintsCount uint64
213+
210214
// retryCount is the number of times the transaction was retried.
211215
retryCount uint64
212216

@@ -429,6 +433,7 @@ func (ih *instrumentationHelper) Setup(
429433
ih.implicitTxn = implicitTxn
430434
ih.txnPriority = txnPriority
431435
ih.txnBufferedWritesEnabled = p.txn.BufferedWritesEnabled()
436+
ih.stmtHintsCount = uint64(len(stmt.Hints))
432437
ih.retryCount = uint64(retryCount)
433438
ih.codec = cfg.Codec
434439
ih.origCtx = ctx
@@ -861,6 +866,7 @@ func (ih *instrumentationHelper) emitExplainAnalyzePlanToOutputBuilder(
861866
ob.AddDistribution(ih.distribution.String())
862867
ob.AddVectorized(ih.vectorized)
863868
ob.AddPlanType(ih.generic, ih.optimized)
869+
ob.AddStmtHintCount(ih.stmtHintsCount)
864870
ob.AddRetryCount("transaction", ih.retryCount)
865871
ob.AddRetryTime("transaction", phaseTimes.GetTransactionRetryLatency())
866872
ob.AddRetryCount("statement", ih.retryStmtCount)

pkg/sql/opt/exec/execbuilder/testdata/explain

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,3 +2573,41 @@ vectorized: true
25732573
table: t_float@t_float_pkey
25742574
spans: /1/0 /2/0 /3/0 /4/0 /5/0 /6/0 /7/0 /8/0 /9/0 /10/0 /11/0 /12/0 /13/0 /14/0 /15/0 /16/0 /17/0 /18/0 /19/0 /20/0 … (20 more)
25752575
parallel
2576+
2577+
# Verify that the number of applied statement hints is displayed.
2578+
statement ok
2579+
CREATE TABLE t_hints (k INT PRIMARY KEY, v INT, INDEX (v))
2580+
2581+
statement ok
2582+
SELECT crdb_internal.inject_hint(
2583+
'SELECT k FROM t_hints WHERE k >= _',
2584+
'SELECT k FROM t_hints@t_hints_v_idx WHERE k >= _'
2585+
)
2586+
2587+
statement ok
2588+
SELECT crdb_internal.await_statement_hints_cache()
2589+
2590+
query T
2591+
EXPLAIN SELECT k FROM t_hints WHERE k >= 100
2592+
----
2593+
distribution: local
2594+
vectorized: true
2595+
statement hints count: 1
2596+
·
2597+
• scan
2598+
missing stats
2599+
table: t_hints@t_hints_pkey
2600+
spans: [/100 - ]
2601+
2602+
query T
2603+
EXPLAIN (VERBOSE) SELECT k FROM t_hints WHERE k >= 100
2604+
----
2605+
distribution: local
2606+
vectorized: true
2607+
statement hints count: 1
2608+
·
2609+
• scan
2610+
columns: (k)
2611+
estimated row count: 333 (missing stats)
2612+
table: t_hints@t_hints_pkey
2613+
spans: /100-

pkg/sql/opt/exec/execbuilder/testdata/explain_analyze

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LogicTest: local
22

33
statement ok
4-
CREATE TABLE kv (k INT PRIMARY KEY, v INT, FAMILY (k, v))
4+
CREATE TABLE kv (k INT PRIMARY KEY, v INT, FAMILY (k, v), INDEX (v))
55

66
query T
77
EXPLAIN ANALYZE (PLAN) SELECT k FROM kv WHERE k >= 2
@@ -176,7 +176,7 @@ quality of service: regular
176176
│ │ └── • virtual table
177177
│ │ sql nodes: <hidden>
178178
│ │ regions: <hidden>
179-
│ │ actual row count: 361
179+
│ │ actual row count: 362
180180
│ │ execution time: 0µs
181181
│ │ table: pg_class@primary
182182
│ │
@@ -276,3 +276,43 @@ quality of service: regular
276276
actual row count: 1
277277
execution time: 0µs
278278
size: 2 columns, 1 row
279+
280+
# Verify that the number of applied statement hints is displayed.
281+
statement ok
282+
SELECT crdb_internal.inject_hint(
283+
'SELECT k FROM kv WHERE k >= _',
284+
'SELECT k FROM kv@kv_v_idx WHERE k >= _'
285+
)
286+
287+
statement ok
288+
SELECT crdb_internal.await_statement_hints_cache()
289+
290+
query T
291+
EXPLAIN ANALYZE SELECT k FROM kv WHERE k >= 100
292+
----
293+
planning time: 10µs
294+
execution time: 100µs
295+
distribution: <hidden>
296+
vectorized: <hidden>
297+
plan type: custom
298+
statement hints count: 1
299+
maximum memory usage: <hidden>
300+
DistSQL network usage: <hidden>
301+
regions: <hidden>
302+
isolation level: serializable
303+
priority: normal
304+
quality of service: regular
305+
·
306+
• scan
307+
sql nodes: <hidden>
308+
kv nodes: <hidden>
309+
regions: <hidden>
310+
actual row count: 0
311+
KV time: 0µs
312+
KV rows decoded: 0
313+
KV bytes read: 0 B
314+
KV gRPC calls: 0
315+
estimated max memory allocated: 0 B
316+
missing stats
317+
table: kv@kv_pkey
318+
spans: [/100 - ]

pkg/sql/opt/exec/explain/output.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,15 @@ func (ob *OutputBuilder) AddPlanType(generic, optimized bool) {
316316
}
317317
}
318318

319+
// AddStmtHintCount adds a top-level field displaying the number of statement
320+
// hints applied to the query. Cannot be called while inside a node.
321+
func (ob *OutputBuilder) AddStmtHintCount(hintCount uint64) {
322+
if hintCount == 0 {
323+
return
324+
}
325+
ob.AddTopLevelField("statement hints count", string(humanizeutil.Count(hintCount)))
326+
}
327+
319328
// AddPlanningTime adds a top-level planning time field. Cannot be called
320329
// while inside a node.
321330
func (ob *OutputBuilder) AddPlanningTime(delta time.Duration) {

0 commit comments

Comments
 (0)