| name | tidb-cluster-inspector |
| version | 1.1.0 |
| description | Comprehensive TiDB cluster inspection. Checks cluster topology, node health, slow queries, hot regions, TiKV storage status, PD scheduling, leader skew, and TopSQL drain (latency / execution count / memory). Outputs a full inspection report with prioritized findings. Compatible with TiDB v4.0+.
|
| allowed-tools | db_query, db_get_status, db_get_variable |
TiDB Cluster Inspector
Overview
TiDB clusters have a complex distributed architecture (TiDB + PD + TiKV) where issues in any component can cascade. This skill performs a systematic inspection across all layers:
- Topology โ Map all nodes, versions, uptime
- Node Health โ Check each TiDB/PD/TiKV node status
- Slow Queries โ Identify TOP N slow queries
- Hot Regions โ Detect read/write hotspots (hot-degree ranked)
- TiKV Storage โ Capacity and region distribution
- PD Scheduling โ Leader/region balance check
- Leader Skew โ Detect leader concentration imbalance across TiKV stores
- TopSQL โ Rank SQL by total latency, execution count, and memory
When to Use
- Periodic cluster health inspection (daily/weekly)
- After TiDB version upgrades
- Performance degradation investigation
- Capacity planning and scaling decisions
- Pre-maintenance readiness check
Required MCP Server
| Tool | Purpose |
|---|
db_query | Query TiDB system tables |
db_get_status | Get server metrics |
db_get_variable | Get configuration |
Required Privileges
SELECT on information_schema.*
Diagnostic Workflow
Step 1: Cluster Topology
SELECT
TYPE AS role, INSTANCE AS address,
VERSION AS version, STATUS AS status,
START_TIME AS start_time
FROM information_schema.cluster_info
ORDER BY TYPE, INSTANCE;
Verify:
- All expected nodes present
- Version consistency across nodes
- No recently restarted nodes (uptime check)
Step 2: Node Health
SELECT
TYPE, INSTANCE, VERSION,
TIMESTAMPDIFF(HOUR, START_TIME, NOW()) AS uptime_hours
FROM information_schema.cluster_info
WHERE STATUS != 'Up' OR STATUS IS NULL;
Alert conditions:
- Any node
STATUS != 'Up' โ critical
- Uptime < 24 hours โ recently restarted, investigate
- Version mismatch โ upgrade incomplete
Step 3: Slow Query Analysis
SELECT
Txn_start, Query_time, Parse_time, Compile_time,
Process_time, Wait_time, Backoff_time,
Request_count, Coprocessor_involved_count,
LEFT(Query, 300) AS sql_fragment
FROM information_schema.slow_query
WHERE Time > DATE_SUB(NOW(), INTERVAL 1 HOUR)
ORDER BY Query_time DESC
LIMIT 20;
SELECT * FROM information_schema.cluster_slow_query
WHERE Time > DATE_SUB(NOW(), INTERVAL 1 HOUR)
ORDER BY Query_time DESC LIMIT 20;
Step 4: Hot Region Detection
SELECT
DB_NAME,
TABLE_NAME,
INDEX_NAME,
TYPE AS hot_type,
MAX_HOT_DEGREE AS hot_degree,
FLOW_BYTES,
REGION_COUNT
FROM information_schema.TIDB_HOT_REGIONS
ORDER BY MAX_HOT_DEGREE DESC, FLOW_BYTES DESC
LIMIT 20;
Interpretation:
MAX_HOT_DEGREE > 0 โ region is a hotspot; the higher the degree the hotter
TYPE = 'read' โ read hotspot: add a covering/composite index or scatter the region to reduce point reads
TYPE = 'write' โ write hotspot: usually auto-increment or sequential inserts; consider region pre-splitting
- Pre-splitting is a write operation (run via
pd-ctl or a dedicated tool such as tidba split); it is out of scope for this read-only skill โ only recommend it
Step 5: TiKV Storage Status
SELECT
STORE_ID,
ADDRESS,
STORE_STATE_NAME AS state,
CAPACITY,
AVAILABLE,
USED_SIZE,
ROUND(USED_SIZE / CAPACITY * 100, 2) AS usage_pct,
REGION_COUNT,
LEADER_COUNT
FROM information_schema.tikv_store_status
ORDER BY STORE_ID;
Alert conditions:
- Usage > 80% โ plan expansion
- Leader count heavily imbalanced โ PD scheduling issue
- Any store
state != 'Up' โ critical
Step 6: PD Scheduling
SHOW CONFIG WHERE TYPE = 'pd' AND NAME LIKE '%schedule%';
SHOW CONFIG WHERE TYPE = 'pd' AND NAME LIKE '%replicate%';
Key configs:
schedule.leader-schedule-limit โ Leader balance speed
schedule.region-schedule-limit โ Region balance speed
schedule.enable-cross-table-merge โ Region merge optimization
Step 7: Region Leader Skew
SELECT
STORE_ID,
ADDRESS,
REGION_COUNT,
LEADER_COUNT,
ROUND(LEADER_COUNT / NULLIF(REGION_COUNT, 0) * 100, 2) AS leader_pct_of_store,
ROUND(
LEADER_COUNT /
(SELECT SUM(LEADER_COUNT) FROM information_schema.TIKV_STORE_STATUS) * 100,
2
) AS leader_share_pct
FROM information_schema.TIKV_STORE_STATUS
ORDER BY leader_share_pct DESC;
Alert conditions:
- One store's
leader_share_pct far exceeds the others โ PD leader scheduling skew
- Check
schedule.leader-schedule-limit and each store's weight / labels config
- Per-table leader drill-down is possible via
TIKV_REGION_STATUS (one row per region โ expensive on large clusters; always scope with WHERE DB_NAME = '...' AND TABLE_NAME = '...')
Step 8: TopSQL Diagnosis
Rank SQL by drain across the cluster using the statement summary. All latency
fields are in nanoseconds (SUM_LATENCY / 1e9 โ seconds, / 1e6 โ ms).
8a โ Top 10 by total latency (which SQL drains the most cluster time):
SELECT
DIGEST_TEXT,
SCHEMA_NAME,
STMT_TYPE,
EXEC_COUNT,
ROUND(SUM_LATENCY / 1e9, 3) AS total_latency_s,
ROUND(AVG_LATENCY / 1e6, 2) AS avg_latency_ms,
ROUND(MAX_LATENCY / 1e6, 2) AS max_latency_ms
FROM information_schema.CLUSTER_STATEMENTS_SUMMARY
WHERE EXEC_COUNT > 0
ORDER BY SUM_LATENCY DESC
LIMIT 10;
8b โ Top 10 by execution count (high-frequency queries):
SELECT
DIGEST_TEXT,
SCHEMA_NAME,
EXEC_COUNT,
ROUND(AVG_LATENCY / 1e6, 2) AS avg_latency_ms
FROM information_schema.CLUSTER_STATEMENTS_SUMMARY
WHERE EXEC_COUNT > 0
ORDER BY EXEC_COUNT DESC
LIMIT 10;
8c โ Top 10 by memory (OOM risk):
SELECT
DIGEST_TEXT,
SCHEMA_NAME,
EXEC_COUNT,
SUM_MEMORY,
ROUND(AVG_MEM / 1024 / 1024, 2) AS avg_mem_mb
FROM information_schema.CLUSTER_STATEMENTS_SUMMARY
WHERE EXEC_COUNT > 0
ORDER BY SUM_MEMORY DESC
LIMIT 10;
8d โ Diagnostic TOP 5 (the single biggest combined drain):
SELECT
DIGEST_TEXT,
SCHEMA_NAME,
STMT_TYPE,
EXEC_COUNT,
ROUND(SUM_LATENCY / 1e9, 3) AS total_latency_s,
ROUND(AVG_LATENCY / 1e6, 2) AS avg_latency_ms,
ROUND(SUM_MEMORY / 1024 / 1024, 2) AS total_mem_mb
FROM information_schema.CLUSTER_STATEMENTS_SUMMARY
WHERE EXEC_COUNT > 0
ORDER BY SUM_LATENCY DESC
LIMIT 5;
Empty result guard โ if the queries above return no rows, statement summary
is disabled, so recommend enabling it:
SELECT @@tidb_enable_stmt_summary AS enabled;
Scope & Limitations
This skill is a read-only SQL inspection. Some TiDB operational capabilities
are not reachable via SQL โ they require the PD HTTP API, write access, or a
dedicated feature. For those, point the DBA to the right tool instead of
attempting them here:
| Capability | Why not in this skill | Where to do it |
|---|
Region pre-splitting (tidba split) | Write operation via PD | pd-ctl / SPLIT TABLE DDL / tidba split |
Runaway SQL throttle/kill (tidba runaway) | Resource-group write op | RESOURCE GROUP + runaway watches |
Majority-DOWN region detection (tidba region replica) | Needs PD region-peer API | pd-ctl / tidba region replica |
Native CPU-ranked TopSQL (tidba topsql cpu) | Needs TiDB TopSQL feature (tidb_enable_top_sql) | TopSQL / TiDB Dashboard |
The four enhanced steps above (hot region, leader skew, TopSQL latency/exec/memory)
cover the read-only diagnosis surface. This skill approximates "CPU drain"
via total latency, which is the standard proxy when the TopSQL feature is not
enabled.
Output Template
๐ TiDB Cluster Inspection Report
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Cluster Overview
Topology: {tidb_count} TiDB + {pd_count} PD + {tikv_count} TiKV
TiDB Version: {version}
Inspection Time: {timestamp}
โ
Node Health
| Role | Address | Version | Status | Uptime |
|------|---------|---------|--------|--------|
| TiDB | ... | 4.0.11 | Up | 187d |
| PD | ... | 4.0.11 | Up | 327d |
| TiKV | ... | 4.0.11 | Up | 4d |
โ ๏ธ Slow Queries TOP 5
| Duration | Scan Rows | SQL |
|----------|-----------|-----|
โก TopSQL TOP 5 (by total latency)
| SQL (digest) | Schema | Execs | Total Latency | Avg Latency |
|--------------|--------|-------|---------------|-------------|
๐ฅ Hot Regions
| DB | Table | Type | Flow Bytes |
|----|-------|------|------------|
โ๏ธ Leader Skew (per TiKV store)
| Store | Leader Share% | Note |
|-------|---------------|------|
๐พ TiKV Storage
| Store | Capacity | Used | Usage% | Regions | Leaders |
|-------|----------|------|--------|---------|---------|
โ๏ธ PD Config
Leader Schedule: {enabled}
Region Schedule: {enabled}
๐ก Findings & Recommendations
{Prioritized action items}
Example Usage
User: "Inspect my TiDB cluster"
AI Response:
๐ TiDB Cluster Inspection
Topology: 3 TiDB + 3 PD + 3 TiKV = 9 nodes
Version: v4.0.11 โ
(consistent)
โ
All 9 nodes Up
๐ฅ Hot Regions: 5 found
order_service.order_detail (read, 9.5MB flow)
โ Check index on order_detail
๐พ TiKV Storage: 3 stores
All < 50% usage โ
Leaders balanced โ
๐ก Recommendation: Investigate read hotspot on order_service
order_detail table โ consider adding composite index.
Best Practices
Inspection Schedule
| Frequency | Scope | Focus |
|---|
| Daily | Slow queries + hot regions | Performance |
| Weekly | Full 8-step inspection | Health check |
| Monthly | Storage trends + capacity | Planning |
TiDB-Specific Tuning
SET GLOBAL tidb_enable_slow_log = ON;
SET GLOBAL tidb_slow_log_threshold = 200;
SET GLOBAL tidb_merge_partial_ddl = ON;
Quick Reference
| Action | Query |
|---|
| Cluster info | SELECT * FROM information_schema.cluster_info |
| Slow queries | SELECT * FROM information_schema.slow_query ORDER BY Query_time DESC |
| Hot regions | SELECT * FROM information_schema.TIDB_HOT_REGIONS |
| TiKV stores | SELECT * FROM information_schema.tikv_store_status |
| PD config | SHOW CONFIG WHERE TYPE='pd' |
| TopSQL drain | SELECT DIGEST_TEXT, EXEC_COUNT, SUM_LATENCY FROM information_schema.CLUSTER_STATEMENTS_SUMMARY ORDER BY SUM_LATENCY DESC LIMIT 10 |
| Table stats | SHOW STATS_META WHERE table_name='...' |
Use this skill for systematic TiDB cluster health inspection across all components.