MySQL Per-Connection Buffer & Memory-Blowup Tuning Prompt
Size per-connection buffers (sort/join/read/tmp) to stop OOM kills and RSS growth under connection load.
- Target user
- DBAs and SREs debugging MySQL memory growth and OOM-killed mysqld
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior MySQL/MariaDB DBA who diagnoses server memory growth and OOM kills. You understand the distinction between global buffers (allocated once — innodb_buffer_pool_size) and per-session buffers allocated per connection and sometimes multiple times per query (sort_buffer_size, join_buffer_size, read_buffer_size, read_rnd_buffer_size, tmp_table_size/max_heap_table_size, and binlog_cache_size). You know why a "safe on paper" my.cnf can still OOM when max_connections is high, and how to reason about worst-case memory instead of typical memory. I will provide: - The version and total server RAM (and any cgroup/container memory limit): [DESCRIBE] - The relevant variables: [PASTE `SELECT @@max_connections, @@innodb_buffer_pool_size, @@sort_buffer_size, @@join_buffer_size, @@read_buffer_size, @@read_rnd_buffer_size, @@tmp_table_size, @@max_heap_table_size, @@binlog_cache_size, @@thread_stack, @@table_open_cache;`] - The symptom: [DESCRIBE — mysqld OOM-killed, steady RSS growth, or swapping under load] - Concurrency data: `SHOW STATUS LIKE 'Threads_connected'` / `Max_used_connections`, and `Created_tmp_disk_tables` vs `Created_tmp_tables`. - Query characteristics if known: big sorts, multi-table joins without indexes, large result scans. Work through this: 1. **Separate global from per-connection memory.** Compute the fixed global footprint (buffer pool + log buffer + other global caches). Then build a realistic worst-case per-connection estimate — being explicit that some buffers (join_buffer, sort_buffer) can be allocated more than once per query for multi-join or multi-sort statements. 2. **Model the worst case.** Estimate peak memory as global + (per-connection worst case x a realistic concurrent-query count, informed by Max_used_connections). Compare against RAM minus OS/filesystem headroom and any cgroup limit. Show the arithmetic so the risk is legible. 3. **Explain the observed symptom.** Tie OOM/RSS growth to specific oversized session buffers or to max_connections being multiplied against them — the classic anti-pattern of a large sort_buffer_size set globally. 4. **Recommend right-sizing.** Keep global session-buffer defaults conservative and raise buffers only per-session for the specific heavy queries that need them (SET SESSION). Advise on tmp_table_size/max_heap_table_size given Created_tmp_disk_tables, and on lowering max_connections plus using a pooler if concurrency is the multiplier. 5. **Attack the root query cause.** Note that large sort/join buffers are compensating for missing indexes and filesorts; indexing the offending queries reduces the need for big buffers at all. Output: (a) Global vs worst-case per-connection memory breakdown with arithmetic, (b) Which setting is causing the blowup, (c) Concrete right-sized values (global conservative + per-session overrides), (d) max_connections / pooling guidance, (e) Query/index fixes that reduce buffer pressure, (f) Risks and validation plan. Guardrails: never set large per-session buffers (sort_buffer_size, join_buffer_size, etc.) globally as a "performance boost" — they multiply by connections and cause OOM. Leave headroom below total RAM (and below any cgroup limit) for the OS page cache and mysqld overhead; do not size global + worst-case to 100% of RAM. Validate changes under representative load on staging, and prefer fixing the query/index over inflating buffers. Do not disable the OOM killer as a fix.
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
Most MySQL OOM incidents come from a my.cnf that looks reasonable line by line but is catastrophic in aggregate, because per-connection buffers are invisible until concurrency multiplies them. This prompt makes the model do the arithmetic engineers usually skip: separate the fixed global footprint from the per-session buffers, and then model the worst case as global plus per-connection-peak times a realistic concurrent-query count. Showing the multiplication is what turns “mysqld got OOM-killed again” into a specific, indefensible setting.
It captures the subtlety that trips up even experienced operators — that buffers like join_buffer_size and sort_buffer_size can be allocated more than once within a single query, and that setting them large globally is not a tuning win but a loaded gun. By steering toward conservative global defaults with per-session overrides for the few heavy queries that need them, the prompt fixes the blowup without starving normal workloads.
The guardrails push toward the durable answer rather than the knob-twiddling one: big sort and join buffers are usually compensating for missing indexes and filesorts, so indexing the offending queries reduces buffer pressure at the source. Insisting on RAM and cgroup headroom, staging validation, and never disabling the OOM killer keeps the engineer solving the real capacity problem instead of hiding it.
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 Redo Log & Checkpoint Stall Tuning Prompt
Diagnose write stalls and checkpoint bursts caused by an undersized or misconfigured InnoDB redo log, and size redo capacity and flushing for the workload.
-
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.
-
MySQL Too Many Connections & Pooling Prompt
Diagnose 'ERROR 1040: Too many connections' and connection exhaustion, then right-size max_connections, kill runaway sessions safely, and design application-side pooling so the database stops falling over under load.
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.