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.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
MySQL Durability & Flush Settings Tuning Prompt
Balance InnoDB durability against throughput by reviewing flush, sync, and doublewrite settings for a given workload and hardware.
-
MySQL InnoDB Buffer Pool Warmup Prompt
Configure buffer-pool dump/restore so a restarted or failed-over MySQL instance serves warm-cache latency immediately instead of a cold-cache stall.
-
MySQL Unused & Redundant Index Audit Prompt
Find indexes that are never used or are duplicated/prefix-redundant, and decide which are safe to drop to cut write and storage overhead.
-
MySQL InnoDB I/O Capacity & Flush Tuning Prompt
Right-size innodb_io_capacity, io_capacity_max, and page-flushing to match real storage IOPS and stop dirty-page or checkpoint stalls.
More MySQL prompts & error guides
Browse every MySQL prompt and troubleshooting guide in one place.
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.