Skip to content

Commit dc2c2b4

Browse files
feat(tools/mysql): add list-table-stats-tool to list table statistics in MySQL and Cloud SQL MySQL source. (#2938)
## Description Add the list_table_stats tool to MySQL to list table statistics. The tool display table level statistics for troubleshooting performance issues, finding hottest tables and monitoring table growth. ## PR Checklist > Thank you for opening a Pull Request! Before submitting your PR, there are a > few things you can do to make sure it goes smoothly: - [x] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md) - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) - [x] Make sure to add `!` if this involve a breaking change 🛠️ Fixes #2484 --------- Co-authored-by: Yuan Teoh <45984206+Yuan325@users.noreply.github.com>
1 parent bd43edb commit dc2c2b4

16 files changed

Lines changed: 533 additions & 2 deletions

File tree

cmd/internal/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ func TestPrebuiltTools(t *testing.T) {
17631763
},
17641764
"monitor": tools.ToolsetConfig{
17651765
Name: "monitor",
1766-
ToolNames: []string{"get_query_plan", "list_active_queries", "get_query_metrics", "get_system_metrics", "list_table_fragmentation", "list_tables_missing_unique_indexes"},
1766+
ToolNames: []string{"get_query_plan", "list_active_queries", "get_query_metrics", "get_system_metrics", "list_table_fragmentation", "list_table_stats", "list_tables_missing_unique_indexes"},
17671767
},
17681768
"lifecycle": tools.ToolsetConfig{
17691769
Name: "lifecycle",
@@ -1847,7 +1847,7 @@ func TestPrebuiltTools(t *testing.T) {
18471847
},
18481848
"monitor": tools.ToolsetConfig{
18491849
Name: "monitor",
1850-
ToolNames: []string{"get_query_plan", "list_active_queries", "list_table_fragmentation", "list_tables_missing_unique_indexes"},
1850+
ToolNames: []string{"get_query_plan", "list_active_queries", "list_table_fragmentation", "list_table_stats", "list_tables_missing_unique_indexes"},
18511851
},
18521852
},
18531853
},

cmd/internal/imports.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ import (
182182
_ "github.com/googleapis/mcp-toolbox/internal/tools/mysql/mysqllisttablefragmentation"
183183
_ "github.com/googleapis/mcp-toolbox/internal/tools/mysql/mysqllisttables"
184184
_ "github.com/googleapis/mcp-toolbox/internal/tools/mysql/mysqllisttablesmissinguniqueindexes"
185+
_ "github.com/googleapis/mcp-toolbox/internal/tools/mysql/mysqllisttablestats"
185186
_ "github.com/googleapis/mcp-toolbox/internal/tools/mysql/mysqlsql"
186187
_ "github.com/googleapis/mcp-toolbox/internal/tools/neo4j/neo4jcypher"
187188
_ "github.com/googleapis/mcp-toolbox/internal/tools/neo4j/neo4jexecutecypher"

docs/CLOUDSQLMYSQL_README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ The Cloud SQL for MySQL MCP server provides the following tools:
6161
| `list_tables` | Lists detailed schema information for user-created tables. |
6262
| `list_tables_missing_unique_indexes` | Find tables that do not have primary or unique key constraint. |
6363
| `list_table_fragmentation` | List table fragmentation in MySQL. |
64+
| `list_table_stats` | List table statistics in MySQL. |
6465

6566
## Custom MCP Server Configuration
6667

docs/en/integrations/cloud-sql-mysql/prebuilt-configs/cloud-sql-for-mysql.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ description: "Details of the Cloud SQL for MySQL prebuilt configuration."
3030
* `list_tables_missing_unique_indexes`: Looks for tables that do not have
3131
primary or unique key contraint.
3232
* `list_table_fragmentation`: Displays table fragmentation in MySQL.
33+
* `list_table_stats`: Displays table statistics in MySQL.

docs/en/integrations/mysql/prebuilt-configs/mysql.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ description: "Details of the MySQL prebuilt configuration."
2525
* `list_tables_missing_unique_indexes`: Looks for tables that do not have
2626
primary or unique key contraint.
2727
* `list_table_fragmentation`: Displays table fragmentation in MySQL.
28+
* `list_table_stats`: Displays table statistics in MySQL.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: "mysql-list-table-stats"
3+
type: docs
4+
weight: 1
5+
description: >
6+
A "mysql-list-table-stats" tool report table statistics including table size, total latency, rows read, rows written, read and write latency for entire instance, a specified database, or a specified table.
7+
---
8+
9+
## About
10+
11+
A `mysql-list-table-stats` tool generates table-level performance and resource consumption statistics to facilitate bottleneck identification and workload analysis.
12+
13+
`mysql-list-table-stats` outputs detailed table-level resource consumption including estimated row counts, table size, a complete breakdown of CRUD activity (rows fetched, inserted, updated, and deleted), and IO statistics such as total, read, write and miscellaneous latency. The output is a JSON formatted array of the top 10 MySQL tables ranked by total latency.
14+
15+
Below are some use cases for `mysql-list-table-stats`
16+
- **Finding hottest tables**: Identify tables with highest total latency, read or writes based on the `sort_by` column.
17+
- **Finding tables with most reads**: Identify tables with highest reads by sorting on `rows_fetched`.
18+
- **Monitoring growth**: Track `row_count` and `size_MB` of table over time to estimate growth."
19+
20+
## Compatible Sources
21+
22+
{{< compatible-sources others="integrations/cloud-sql-mysql">}}
23+
24+
## Requirements
25+
26+
- `performance_schema` should be turned ON for this tool to work.
27+
28+
## Parameters
29+
30+
This tool takes 4 optional input parameters:
31+
32+
- `table_schema` (optional): The database where table stats check is to be
33+
executed. Check all tables visible to the current database if not specified.
34+
- `table_name` (optional): Name of the table to be checked. Check all tables
35+
visible to the current user if not specified.
36+
- `sort_by` (optional): The column to sort by. Valid values are `row_count`, `rows_fetched`, `rows_inserted`, `rows_updated`, `rows_deleted`, `total_latency_secs` (defaults to `total_latency_secs`)
37+
- `limit` (optional): Max rows to return, default 10.
38+
39+
## Example
40+
41+
```yaml
42+
kind: tools
43+
name: list_table_stats
44+
type: mysql-list-table-stats
45+
source: my-mysql-instance
46+
description: Display table statistics including table size, total latency, rows read, rows written, read and write latency for entire instance, a specified database, or a specified table. Specifying a database name or table name filters the output to that specific db or table. Results are limited to 10 by default.
47+
```
48+
49+
## Output Format
50+
51+
The response is a json array with the following fields:
52+
```json
53+
[
54+
{
55+
"table_schema": "The schema/database this table belongs to",
56+
"table_name": "Name of this table",
57+
"size_MB": "Size of the table data in MB",
58+
"row_count": "Number of rows in the table",
59+
"total_latency_secs": "total latency in secs",
60+
"rows_fetched": "total number of rows fetched",
61+
"rows_inserted": "total number of rows inserted",
62+
"rows_updated": "total number of rows updated",
63+
"rows_deleted": "total number of rows deleted",
64+
"io_reads": "total number of io read requests",
65+
"io_read_latency": "io read latency in seconds",
66+
"io_write_latency": "io write latency in seconds",
67+
"io_misc_latency": "io misc latency in seconds",
68+
}
69+
]
70+
```
71+
72+
## Reference
73+
74+
| **field** | **type** | **required** | **description** |
75+
|-------------|:--------:|:------------:|----------------------------------------------------|
76+
| type | string | true | Must be "mysql-list-table-stats". |
77+
| source | string | true | Name of the source the SQL should execute on. |
78+
| description | string | true | Description of the tool that is passed to the LLM. |
79+
80+

internal/prebuiltconfigs/tools/cloud-sql-mysql.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ tools:
4444
kind: mysql-list-tables
4545
source: cloud-sql-mysql-source
4646
description: "Lists detailed schema information (object type, columns, constraints, indexes, triggers, comment) as JSON for user-created tables (ordinary or partitioned). Filters by a comma-separated list of names. If names are omitted, lists all tables in user schemas."
47+
list_table_stats:
48+
kind: mysql-list-table-stats
49+
source: cloud-sql-mysql-source
50+
description: "Display table statistics including table size, total latency, rows read, rows written, read and write latency for entire instance, a specified database, or a specified table. Specifying a database name or table name filters the output to that specific db or table. Results are limited to 10 by default."
4751
list_tables_missing_unique_indexes:
4852
kind: mysql-list-tables-missing-unique-indexes
4953
source: cloud-sql-mysql-source
@@ -148,6 +152,7 @@ toolsets:
148152
- get_query_metrics
149153
- get_system_metrics
150154
- list_table_fragmentation
155+
- list_table_stats
151156
- list_tables_missing_unique_indexes
152157
lifecycle:
153158
- create_backup

internal/prebuiltconfigs/tools/mysql.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ tools:
4343
kind: mysql-list-tables
4444
source: mysql-source
4545
description: "Lists detailed schema information (object type, columns, constraints, indexes, triggers, comment) as JSON for user-created tables (ordinary or partitioned). Filters by a comma-separated list of names. If names are omitted, lists all tables in user schemas."
46+
list_table_stats:
47+
kind: mysql-list-table-stats
48+
source: mysql-source
49+
description: "Display table statistics including table size, total latency, rows read, rows written, read and write latency for entire instance, a specified database, or a specified table. Specifying a database name or table name filters the output to that specific db or table. Results are limited to 10 by default."
4650
list_tables_missing_unique_indexes:
4751
kind: mysql-list-tables-missing-unique-indexes
4852
source: mysql-source
@@ -61,4 +65,5 @@ toolsets:
6165
- get_query_plan
6266
- list_active_queries
6367
- list_table_fragmentation
68+
- list_table_stats
6469
- list_tables_missing_unique_indexes

internal/sources/cloudsqlmysql/cloud_sql_mysql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func (s *Source) MySQLPool() *sql.DB {
102102
return s.Pool
103103
}
104104

105+
func (s *Source) MySQLDatabase() string {
106+
return s.Database
107+
}
108+
105109
func (s *Source) RunSQL(ctx context.Context, statement string, params []any) (any, error) {
106110
results, err := s.MySQLPool().QueryContext(ctx, statement, params...)
107111
if err != nil {

internal/sources/mysql/mysql.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func (s *Source) MySQLPool() *sql.DB {
101101
return s.Pool
102102
}
103103

104+
func (s *Source) MySQLDatabase() string {
105+
return s.Database
106+
}
107+
104108
func (s *Source) RunSQL(ctx context.Context, statement string, params []any) (any, error) {
105109
results, err := s.MySQLPool().QueryContext(ctx, statement, params...)
106110
if err != nil {

0 commit comments

Comments
 (0)