Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for MySQL Difficulty: Advanced ClaudeChatGPTCursor

MySQL Table & Index Fragmentation Reclaim Prompt

Identify bloated, fragmented InnoDB tables where deletes never returned disk space, and plan a safe, low-lock rebuild to reclaim space and restore performance.

Target user
DBAs and SREs reclaiming disk and fixing bloat from heavy DELETE/UPDATE churn
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior MySQL/MariaDB DBA who reclaims space and fixes fragmentation in InnoDB. You understand that DELETEs leave free space inside pages that is not returned to the filesystem, that OPTIMIZE TABLE on InnoDB maps to an online ALTER TABLE ... FORCE (a full table rebuild), the difference between innodb_file_per_table (rebuild frees OS space) and the shared system tablespace (space is only reused internally, never returned), data_free from INFORMATION_SCHEMA, and the locking, disk-headroom, and replication-lag costs of a rebuild.

I will provide:
- Candidate tables with size and free space from INFORMATION_SCHEMA.TABLES (DATA_LENGTH, INDEX_LENGTH, DATA_FREE, TABLE_ROWS, ENGINE, ROW_FORMAT): [PASTE]
- Filesystem headroom for the datadir (`df -h`) and whether innodb_file_per_table is ON: [PASTE]
- The workload on these tables: churn pattern (bulk deletes, archival jobs, hot OLTP), read/write ratio, and the maintenance window available: [DESCRIBE]
- Topology: standalone, source→replica, or a cluster (Galera/Group Replication), and current replica lag: [PASTE]

Work through this:

1. **Quantify the bloat, don't assume it.** Compute the ratio of DATA_FREE to DATA_LENGTH+INDEX_LENGTH per table and rank them. Note that DATA_FREE is approximate and, for the shared tablespace, is not reclaimable to the OS — flag those tables as not worth rebuilding for space.
2. **Decide whether a rebuild is warranted.** Fragmentation only matters when it causes real disk pressure or measurable scan/IO cost; small tables and modest free space are not worth the churn. State a threshold and which tables clear it.
3. **Pick the safe method.** Prefer OPTIMIZE TABLE / ALTER TABLE ... ENGINE=InnoDB (or ... FORCE) with ALGORITHM=INPLACE where supported; for very large hot tables or where INPLACE blocks, recommend gh-ost or pt-online-schema-change to avoid a long metadata lock. Explain the difference.
4. **Check headroom and lag risk.** A rebuild needs free space roughly equal to the table's full size plus the sort buffer; verify `df` has it. On a source→replica topology the rebuild replicates and can spike replica lag — recommend running per-table, off-peak, and watching lag.
5. **Order and stage.** Sequence the tables largest-benefit-first, one at a time, so a failure or lag spike is contained.

Output: (a) Ranked bloat report with the computed ratios and which tables are genuinely reclaimable (file_per_table) vs not (shared tablespace), (b) The exact rebuild command per table with the chosen algorithm/tool and why, (c) Pre-flight checklist (disk headroom, backup, replica lag baseline, maintenance window), (d) Post-run verification (re-check DATA_FREE, df, and query timings).

Guardrails: OPTIMIZE TABLE on InnoDB rebuilds the whole table and needs free disk roughly equal to the table size — verify headroom first or the rebuild fails and can leave a large temp file. It can hold locks and generate replication lag; run per-table, off-peak, and back up first. Tables in the shared system tablespace cannot return space to the OS by rebuilding — do not promise disk reclaim there. On Galera/Group Replication a big rebuild can stall flow control — schedule carefully. Note MariaDB and MySQL differ in OPTIMIZE/ALGORITHM support by version.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

Fragmentation cleanup is where good intentions destroy production: someone sees a table that “should be smaller,” runs OPTIMIZE TABLE on the largest table on the box mid-day, and either fills the disk with the rebuild’s temporary copy or freezes replication for twenty minutes. This prompt makes the model measure the bloat first — the ratio of DATA_FREE to actual data+index size — and rank tables, rather than trusting a hunch that a table is fragmented.

Its most important move is refusing to promise reclaim that cannot happen. Space is only returned to the filesystem when innodb_file_per_table is on; tables living in the shared system tablespace will never give bytes back to the OS no matter how many times you rebuild them. By forcing that distinction up front, the prompt stops teams from burning a maintenance window on a rebuild that reclaims nothing.

The rest is operational safety that experienced DBAs treat as reflex: confirm free disk roughly equal to the table size before starting (an InnoDB rebuild is a full copy), sequence tables one at a time largest-benefit-first, watch replica lag and Galera flow control, and choose gh-ost / pt-online-schema-change for hot tables where an in-place ALGORITHM=INPLACE rebuild would still block. Pairing each recommendation with a pre-flight checklist and post-run verification means a human can approve and confirm the reclaim instead of discovering the damage afterward.

Related prompts

More MySQL prompts & error guides

Browse every MySQL prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.