Skip to content

Commit a2d740d

Browse files
authored
Merge pull request #70 from planetscale/rohit/dbinfo
Enhance dbinfo sub-command with column names and global variables
2 parents 4466450 + 4a0b6e2 commit a2d740d

14 files changed

+48061
-162
lines changed

README.md

+38-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
# VT Utilities
22

3-
The `vt` binary encapsulates several utility tools for Vitess, providing a comprehensive suite for testing, summarizing, and query analysis.
3+
The `vt` binary encapsulates several utility tools for Vitess, providing a comprehensive suite for testing, summarizing,
4+
and query analysis.
45

56
## Tools Included
6-
- **`vt test`**: A testing utility using the same test files as the [MySQL Test Framework](https://github.com/mysql/mysql-server/tree/8.0/mysql-test). It compares the results of identical queries executed on both MySQL and Vitess (vtgate), helping to ensure compatibility.
7-
- **`vt keys`**: A utility that analyzes query logs and provides information about queries, tables, joins, and column usage.
8-
- **`vt transactions`**: A tool that analyzes query logs to identify transaction patterns and outputs a JSON report detailing these patterns.
9-
- **`vt trace`**: A tool that generates execution traces for queries without comparing against MySQL. It helps analyze query behavior and performance in Vitess environments.
7+
8+
- **`vt test`**: A testing utility using the same test files as
9+
the [MySQL Test Framework](https://github.com/mysql/mysql-server/tree/8.0/mysql-test). It compares the results of
10+
identical queries executed on both MySQL and Vitess (vtgate), helping to ensure compatibility.
11+
- **`vt keys`**: A utility that analyzes query logs and provides information about queries, tables, joins, and column
12+
usage.
13+
- **`vt transactions`**: A tool that analyzes query logs to identify transaction patterns and outputs a JSON report
14+
detailing these patterns.
15+
- **`vt trace`**: A tool that generates execution traces for queries without comparing against MySQL. It helps analyze
16+
query behavior and performance in Vitess environments.
1017
- **`vt summarize`**: A tool used to summarize or compare trace logs or key logs for easier human consumption.
18+
- **`vt dbinfo`**: A tool that provides information about the database schema, including row counts, useful column
19+
attributes and relevant subset of global variables.
1120

1221
## Installation
22+
1323
You can install `vt` using the following command:
1424

1525
```bash
@@ -18,7 +28,8 @@ go install github.com/vitessio/vt/go/vt@latest
1828

1929
## Testing Methodology
2030

21-
To verify compatibility and correctness, the testing strategy involves running identical queries on both MySQL and vtgate, followed by a comparison of results. The process includes:
31+
To verify compatibility and correctness, the testing strategy involves running identical queries on both MySQL and
32+
vtgate, followed by a comparison of results. The process includes:
2233

2334
1. **Query Execution**: Each test query is executed on both MySQL and vtgate.
2435
2. **Result Comparison**: The returned data, result set structure (column types, order), and errors are compared.
@@ -27,7 +38,9 @@ To verify compatibility and correctness, the testing strategy involves running i
2738
This dual-testing strategy ensures high confidence in vtgate's compatibility with MySQL.
2839

2940
### Sharded Testing Strategy
30-
Vitess operates in a sharded environment, presenting unique challenges, especially during schema changes (DDL). The `vt test` tool handles these by converting DDL statements into VSchema commands.
41+
42+
Vitess operates in a sharded environment, presenting unique challenges, especially during schema changes (DDL). The
43+
`vt test` tool handles these by converting DDL statements into VSchema commands.
3144

3245
Here's an example of running `vt test`:
3346

@@ -60,7 +73,9 @@ vt trace --vschema=t/vschema.json --backup-path=/path/to/backup --number-of-shar
6073
```
6174

6275
`vt trace` accepts most of the same configuration flags as `vt test`, including:
63-
- `--sharded`: Enable auto-sharded mode - uses primary keys as sharding keys. Not a good idea for a production environment, but can be used to ensure that all queries work in a sharded environment.
76+
77+
- `--sharded`: Enable auto-sharded mode - uses primary keys as sharding keys. Not a good idea for a production
78+
environment, but can be used to ensure that all queries work in a sharded environment.
6479
- `--vschema`: Specify the VSchema configuration
6580
- `--backup-path`: Initialize from a backup
6681
- `--number-of-shards`: Specify the number of shards to bring up
@@ -69,6 +84,7 @@ vt trace --vschema=t/vschema.json --backup-path=/path/to/backup --number-of-shar
6984
Both `vt trace` and `vt keys` support different input file formats through the `--input-type` flag:
7085

7186
Example using different input types:
87+
7288
```bash
7389
# Analyze SQL file or slow query log
7490
vt trace slow-query.log > trace-log.json
@@ -90,7 +106,7 @@ vt summarize trace-log1.json trace-log2.json # Compare two traces
90106
## Key Analysis Workflow
91107

92108
`vt keys` analyzes query logs and outputs detailed information about tables, columns usage and joins in queries.
93-
This data can be summarized using `vt summarize`.
109+
This data can be summarized using `vt summarize`.
94110
Here's a typical workflow:
95111

96112
1. **Run `vt keys` to analyze queries**:
@@ -106,17 +122,26 @@ Here's a typical workflow:
106122
vt trace --input-type=vtgate-log vtgate-querylog.log > trace-log.json
107123
```
108124

109-
This command generates a `keys-log.json` file that contains a detailed analysis of table and column usage from the queries.
125+
This command generates a `keys-log.json` file that contains a detailed analysis of table and column usage from the
126+
queries.
110127

111128
2. **Summarize the `keys-log` using `vt summarize`**:
112129

113130
```bash
114131
vt summarize keys-log.json
115132
```
116133

117-
This command summarizes the key analysis, providing insight into which tables and columns are used across queries, and how frequently they are involved in filters, groupings, and joins.
134+
This command summarizes the key analysis, providing insight into which tables and columns are used across queries,
135+
and how frequently they are involved in filters, groupings, and joins.
118136
[Here](https://github.com/vitessio/vt/blob/main/go/summarize/testdata/keys-summary.md) is an example summary report.
119137

138+
If you have access to the running database, you can use `vt dbinfo > dbinfo.json` and pass it to `summarize` so
139+
that the analysis can take into the account the additional information from the database schema and configuration:
140+
141+
```bash
142+
vt summarize keys-log.json dbinfo.json
143+
```
144+
120145
## Transaction Analysis with vt transactions
121146
The `vt transactions` command is designed to analyze query logs and identify patterns of transactional queries.
122147
It processes the logs to find sequences of queries that form transactions and outputs a JSON report summarizing these patterns.
@@ -156,4 +181,5 @@ Vitess Tester is licensed under the Apache 2.0 license. See the [LICENSE](./LICE
156181

157182
## Acknowledgments
158183

159-
Vitess Tester started as a fork from [pingcap/mysql-tester](https://github.com/pingcap/mysql-tester). We thank the original authors for their foundational work.
184+
Vitess Tester started as a fork from [pingcap/mysql-tester](https://github.com/pingcap/mysql-tester). We thank the
185+
original authors for their foundational work.

go/cmd/schema.go go/cmd/dbinfo.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ import (
2020
"github.com/spf13/cobra"
2121
"vitess.io/vitess/go/mysql"
2222

23-
"github.com/vitessio/vt/go/schema"
23+
"github.com/vitessio/vt/go/dbinfo"
2424
)
2525

26-
func schemaCmd() *cobra.Command {
26+
func dbinfoCmd() *cobra.Command {
2727
var vtParams mysql.ConnParams
2828

2929
cmd := &cobra.Command{
30-
Use: "schema ",
30+
Use: "dbinfo ",
3131
Short: "Loads info from the database including row counts",
32-
Example: "vt schema",
32+
Example: "vt dbinfo",
3333
Args: cobra.ExactArgs(0),
3434
RunE: func(_ *cobra.Command, _ []string) error {
35-
cfg := schema.Config{
35+
cfg := dbinfo.Config{
3636
VTParams: vtParams,
3737
}
3838

39-
return schema.Run(cfg)
39+
return dbinfo.Run(cfg)
4040
},
4141
}
4242

go/cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Execute() {
3737
root.AddCommand(testerCmd())
3838
root.AddCommand(tracerCmd())
3939
root.AddCommand(keysCmd())
40-
root.AddCommand(schemaCmd())
40+
root.AddCommand(dbinfoCmd())
4141
root.AddCommand(transactionsCmd())
4242

4343
err := root.Execute()

go/schema/schema.go go/dbinfo/dbinfo.go

+54-22
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package schema
17+
package dbinfo
1818

1919
import (
20-
"context"
2120
"encoding/json"
22-
"fmt"
2321
"io"
2422
"os"
2523

@@ -47,14 +45,24 @@ func run(out io.Writer, cfg Config) error {
4745
return err
4846
}
4947

48+
type TableColumn struct {
49+
Name string `json:"name"`
50+
Type string `json:"type"`
51+
KeyType string `json:"keyType,omitempty"`
52+
IsNullable bool `json:"isNullable,omitempty"`
53+
Extra string `json:"extra,omitempty"`
54+
}
55+
5056
type TableInfo struct {
51-
Name string `json:"name"`
52-
Rows int `json:"rows"`
57+
Name string `json:"name"`
58+
Rows int `json:"rows"`
59+
Columns []*TableColumn `json:"columns"`
5360
}
5461

5562
type Info struct {
56-
FileType string `json:"fileType"`
57-
Tables []TableInfo `json:"tables"`
63+
FileType string `json:"fileType"`
64+
Tables []*TableInfo `json:"tables"`
65+
GlobalVariables map[string]string `json:"globalVariables"`
5866
}
5967

6068
func Get(cfg Config) (*Info, error) {
@@ -66,29 +74,53 @@ func Get(cfg Config) (*Info, error) {
6674
DbName: cfg.VTParams.DbName,
6775
}
6876

69-
vtConn, err := mysql.Connect(context.Background(), vtParams)
77+
dbh := NewDBHelper(vtParams)
78+
ts, err := dbh.getTableSizes()
7079
if err != nil {
7180
return nil, err
7281
}
73-
defer vtConn.Close()
74-
queryTableSizes := "SELECT table_name, table_rows FROM information_schema.tables WHERE table_schema = '%s' and table_type = 'BASE TABLE'"
75-
qr, err := vtConn.ExecuteFetch(fmt.Sprintf(queryTableSizes, cfg.VTParams.DbName), -1, false)
82+
83+
var tableInfo []*TableInfo
84+
tableMap := make(map[string]*TableInfo)
85+
86+
for tableName, tableRows := range ts {
87+
tableMap[tableName] = &TableInfo{
88+
Name: tableName,
89+
Rows: tableRows,
90+
}
91+
}
92+
93+
tc, err := dbh.getColumnInfo()
7694
if err != nil {
7795
return nil, err
7896
}
79-
var tables []TableInfo
80-
for _, row := range qr.Rows {
81-
tableName := row[0].ToString()
82-
tableRows, _ := row[1].ToInt64()
83-
tables = append(tables, TableInfo{
84-
Name: tableName,
85-
Rows: int(tableRows),
86-
})
97+
98+
for tableName, columns := range tc {
99+
ti, ok := tableMap[tableName]
100+
if !ok {
101+
ti = &TableInfo{
102+
Name: tableName,
103+
}
104+
tableMap[tableName] = ti
105+
}
106+
ti.Columns = columns
107+
}
108+
109+
for tableName := range tableMap {
110+
tableInfo = append(tableInfo, tableMap[tableName])
111+
}
112+
113+
globalVariables, err := dbh.getGlobalVariables()
114+
if err != nil {
115+
return nil, err
87116
}
88-
schemaInfo := &Info{
89-
Tables: tables,
117+
118+
dbInfo := &Info{
119+
FileType: "dbinfo",
120+
Tables: tableInfo,
121+
GlobalVariables: globalVariables,
90122
}
91-
return schemaInfo, nil
123+
return dbInfo, nil
92124
}
93125

94126
func Load(fileName string) (*Info, error) {

0 commit comments

Comments
 (0)