detect_fragmented_tables
它能做什么
这项技能查找存在闲置空间的 MySQL 和 MariaDB 表,并估算可以回收的空间。适合希望安全地减少存储占用的数据库管理员。
适用场景
- ✓当你的数据库磁盘快要写满,想知道浪费在哪里时。
- ✓当某张表有大量可回收的空闲空间时。
- ✓当你在规划维护,需要知道哪些表大到足以影响服务器时。
技能链接
https://github.com/major/MySQLTuner-perl/blob/master/.agent/skills/detect-fragmented-tables/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请找出我数据库中最浪费磁盘空间的表,并告诉我哪些可以安全清理。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会读取表的大小信息,计算每张表中空闲空间的占比,并对结果进行排序。它会建议清理命令,并将大表标记为高影响,以便你提前规划。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
AI Skill: Table Fragmentation & Storage Reclaim Diagnostics
🧠 Purpose & Operational Objectives
The detect_fragmented_tables skill enables autonomous DBA agents to identify wasted disk space, high data file fragmentation, and unused allocated extents across InnoDB and MyISAM tables.
📋 Preconditions & Context
- Server running MySQL 5.5+ or MariaDB 10.0+.
- Read access to
information_schema.TABLES. - Evaluates non-system schemas (skips
mysql,information_schema,performance_schema,sys).
🛠️ Input Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
min_fragmentation_pct |
number | No | 20 |
Minimum fragmentation percentage (0-100) to trigger reporting. |
min_table_size_mb |
number | No | 10 |
Minimum table size in MB to filter out trivial tables. |
schema_filter |
string | No | "" |
Optional schema name to restrict the scan. |
📊 Fragmentation Formula & Impact Scoring
$$\text{Total Allocated Space} = \text{DATA_LENGTH} + \text{INDEX_LENGTH} + \text{DATA_FREE}$$ $$\text{Fragmentation Pct} = \left(\frac{\text{DATA_FREE}}{\text{Total Allocated Space}}\right) \times 100$$
- Tables $< 5\text{GB}$: Can be defragmented online during low-traffic windows via
OPTIMIZE TABLE \db`.`table`;`. - Tables $\ge 5\text{GB}$: Flagged as
is_high_impact: true. Advise using online tools likept-online-schema-changeorgh-ostto prevent extended table locks and disk thrashing.
🛡️ Guardrails & Safety
- All schema and table names are safely escaped with backticks to prevent SQL injection.
- Reclaimable disk space is calculated deterministically.
常见问题
它会自行清理表吗?
它只报告并建议命令。在对数据库执行任何操作之前,由你进行审阅。
它会跳过哪些表?
mysql、information_schema、performance_schema 和 sys 等系统数据库不会被扫描。
我可以缩小扫描范围吗?
可以。你可以设置最低碎片百分比和表大小,或将扫描限制在某一个数据库中。
详情
作者的原始描述
Detects tables with high storage fragmentation, calculates reclaimable disk space, evaluates lock impact, and generates safe defragmentation commands.
repo: major/MySQLTuner-perl
热度
- GitHub 星标
- 9.5k
- 排名
- #12278
- 许可证
- GPL-3.0
- 更新时间
- 2026/10/28
评论
- …