Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for MySQL Difficulty: Intermediate ClaudeChatGPT

MySQL Temp Table & Filesort Spill Tuning Prompt

Diagnose queries spilling to on-disk temporary tables and filesorts, tune tmp_table_size/max_heap_table_size and sort/join buffers, and rewrite the offending SQL so sorts and GROUP BYs stay in memory.

Target user
DBAs and performance-focused backend engineers
Difficulty
Intermediate
Tools
Claude, ChatGPT

The prompt

You are a senior MySQL performance engineer helping reduce I/O and latency caused by on-disk temporary tables and filesorts.

I will provide:
- `SHOW GLOBAL STATUS LIKE 'Created_tmp%';` (Created_tmp_tables vs Created_tmp_disk_tables) and `Sort_merge_passes`
- `EXPLAIN FORMAT=JSON` for a slow query (look for "Using temporary", "Using filesort")
- `SHOW VARIABLES LIKE 'tmp_table_size';`, `max_heap_table_size`, `sort_buffer_size`, `join_buffer_size`, `internal_tmp_mem_storage_engine`
- The query text + relevant table/index definitions (`SHOW CREATE TABLE`)
- Server RAM and typical concurrency

Your job:

1. **Quantify the spill** — compute the Created_tmp_disk_tables ratio and Sort_merge_passes trend, and explain what fraction of temp tables are going to disk and why that hurts.
2. **Pin the cause per query** — read the EXPLAIN to see whether the temp table is from GROUP BY/DISTINCT/UNION/ORDER BY on a non-indexed expression, a large BLOB/TEXT forcing on-disk format, or a missing covering index.
3. **Tune buffers carefully** — recommend tmp_table_size and max_heap_table_size together (the lower of the two applies), and right-size sort_buffer_size/join_buffer_size as SESSION values for heavy queries rather than bloating them globally (they are per-connection).
4. **Fix the query/schema** — propose an index or rewrite (ordering by an indexed column, avoiding SELECT * with TEXT columns, replacing UNION with UNION ALL where valid) so the temp table or filesort disappears entirely.
5. **Validate** — re-run EXPLAIN and check Created_tmp_disk_tables stops climbing.

Output as: (a) spill diagnosis with numbers, (b) per-query cause, (c) buffer recommendations with global-vs-session warning, (d) query/index fix, (e) verification plan.

Advisory only: prefer schema/query fixes over inflating per-connection buffers, since large global buffer sizes multiplied by connection count can exhaust RAM.
Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 2,104 DevOps AI prompts
  • One practical workflow email per week