Skip to content

Commit

Permalink
Add VEXPLAIN KEYS for improved sharding key selection (vitessio#16830)
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay authored Sep 25, 2024
1 parent 5e9c69c commit 3743f09
Show file tree
Hide file tree
Showing 10 changed files with 4,479 additions and 4,088 deletions.
25 changes: 25 additions & 0 deletions changelog/21.0/21.0.0/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- **[New Backup Engine](#new-backup-engine)**
- **[Dynamic VReplication Configuration](#dynamic-vreplication-configuration)**
- **[Reference Table Materialization](#reference-table-materialization)**
- **[New VEXPLAIN Modes: TRACE and KEYS](#new-vexplain-modes)**

## <a id="major-changes"/>Major Changes

Expand Down Expand Up @@ -172,3 +173,27 @@ There is a new option in [`Materialize` workflows](https://vitess.io/docs/refere
a synced copy of [reference or lookup tables](https://vitess.io/docs/reference/vreplication/reference_tables/)
(countries, states, zip_codes, etc) from an unsharded keyspace, which holds the source of truth for the reference
table, to all shards in a sharded keyspace.
### <a id="new-vexplain-modes"/>New VEXPLAIN Modes: TRACE and KEYS

#### VEXPLAIN TRACE

The new TRACE mode for VEXPLAIN provides a detailed execution trace of queries, showing how they're processed through various operators and interactions with tablets. This mode is particularly useful for:

- Identifying performance bottlenecks
- Understanding query execution patterns
- Optimizing complex queries
- Debugging unexpected query behavior

TRACE mode runs the query and logs all interactions, returning a JSON representation of the query execution plan with additional statistics like number of calls, average rows processed, and number of shards queried.

#### VEXPLAIN KEYS

The KEYS mode for VEXPLAIN offers a concise summary of query structure, highlighting columns used in joins, filters, and grouping operations. This information is crucial for:

- Identifying potential sharding key candidates
- Optimizing query performance
- Analyzing query patterns to inform database design decisions

KEYS mode analyzes the query structure without executing it, providing JSON output that includes grouping columns, join columns, filter columns (potential candidates for indexes, primary keys, or sharding keys), and the statement type.

These new VEXPLAIN modes enhance Vitess's query analysis capabilities, allowing for more informed decisions about sharding strategies and query optimization.
2 changes: 2 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,8 @@ func (ty VExplainType) ToString() string {
return AllVExplainStr
case TraceVExplainType:
return TraceStr
case KeysVExplainType:
return KeysStr
default:
return "Unknown VExplainType"
}
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ const (
AllVExplainStr = "all"
PlanStr = "plan"
TraceStr = "trace"
KeysStr = "keys"

// Lock Types
ReadStr = "read"
Expand Down Expand Up @@ -848,6 +849,7 @@ const (
PlanVExplainType
AllVExplainType
TraceVExplainType
KeysVExplainType
)

// Constant for Enum Type - SelectIntoType
Expand Down
5 changes: 3 additions & 2 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2574,8 +2574,9 @@ var (
input: "vexplain select * from t",
output: "vexplain plan select * from t",
}, {
input: "vexplain trace select * from t",
output: "vexplain trace select * from t",
input: "vexplain trace select * from t",
}, {
input: "vexplain keys select * from t",
}, {
input: "explain analyze select * from t",
}, {
Expand Down
Loading

0 comments on commit 3743f09

Please sign in to comment.