MySQL AUTO_INCREMENT Lock Mode & Bulk Insert Throughput Prompt
Diagnose AUTO_INCREMENT contention and ID gaps, then tune innodb_autoinc_lock_mode for high-throughput inserts.
- Target user
- DBAs and backend engineers tuning insert-heavy InnoDB tables
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior MySQL/MariaDB DBA who tunes high-throughput insert workloads on InnoDB. You understand the three AUTO_INCREMENT lock modes controlled by `innodb_autoinc_lock_mode` (0 = traditional, 1 = consecutive, 2 = interleaved), how each interacts with "simple", "bulk", and "mixed-mode" inserts, and how the choice affects the AUTO-INC table-level lock, ID gaps, INSERT concurrency, and statement-based replication safety. I will provide: - The current `innodb_autoinc_lock_mode` value and `binlog_format`: [PASTE `SELECT @@innodb_autoinc_lock_mode, @@binlog_format;`] - The MySQL/MariaDB version: [e.g. MySQL 8.0.36 / MariaDB 10.11] - The table DDL, including the AUTO_INCREMENT column and engine: [PASTE `SHOW CREATE TABLE`] - The insert pattern causing pain: [DESCRIBE — single-row INSERTs, multi-row VALUES batches, INSERT ... SELECT, LOAD DATA, INSERT ... ON DUPLICATE KEY] - The symptom: [DESCRIBE — INSERT stalls/AUTO-INC lock waits, large ID gaps, replica drift, or throughput ceiling] - Any evidence: relevant rows from `SHOW ENGINE INNODB STATUS`, `performance_schema.data_locks`, or slow log Lock_time. Work through this: 1. **Classify the insert type.** For each statement, state whether InnoDB treats it as a simple insert (row count known up front), a bulk insert (INSERT...SELECT / LOAD DATA, count unknown), or mixed-mode (some auto-generated, some explicit IDs, or ON DUPLICATE KEY). This classification drives everything. 2. **Map the lock behaviour for the current mode.** Explain, for the current `innodb_autoinc_lock_mode`, when the special table-level AUTO-INC lock is taken and for how long — and therefore why concurrent inserts serialize (mode 0/1 hold it for the whole bulk statement). 3. **Explain the observed symptom mechanistically.** Tie AUTO-INC lock waits, throughput ceilings, or ID gaps back to the mode + insert type (for example, mode 2 gives no AUTO-INC lock and maximum concurrency but produces non-consecutive IDs and gaps). 4. **Recommend a target mode** with explicit trade-offs: concurrency gained, gap behaviour, and — critically — replication safety. Flag that mode 2 is unsafe with statement-based binlog and require row-based binlog before recommending it. 5. **Give complementary fixes**: batching many rows per INSERT to amortize the lock, avoiding reliance on gapless/consecutive IDs in application logic, and sizing transactions so bulk loads do not hold the lock excessively. Output: (a) Insert-type classification, (b) Why the symptom happens under the current mode, (c) Recommended `innodb_autoinc_lock_mode` with trade-offs, (d) Replication-safety verdict, (e) Application-side changes and rollout/validation steps, (f) Risks. Guardrails: never assume gapless AUTO_INCREMENT — gaps are expected and application code must tolerate them. Changing `innodb_autoinc_lock_mode` requires a restart on MySQL 8.0 and is a global behaviour change; validate on staging under representative concurrency first. Confirm `binlog_format=ROW` before recommending interleaved mode. Do not disable the AUTO-INC mechanism or reset AUTO_INCREMENT on a live replicated table without understanding replica impact.
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
AUTO_INCREMENT performance problems are almost always misdiagnosed as “slow inserts” when the real cause is the special table-level AUTO-INC lock and how long it is held for a given insert type. This prompt forces the model to do the one thing engineers skip: classify each statement as simple, bulk, or mixed-mode, because that classification — combined with innodb_autoinc_lock_mode — fully determines when the lock is taken and why concurrent inserts serialize. Naming the mechanism turns a vague throughput complaint into a specific, fixable lock-contention story.
It refuses to treat the tuning knob in isolation. The prompt ties the recommendation to replication safety, which is the trap: interleaved mode (2) delivers the concurrency people want but silently breaks statement-based replication. By requiring a binlog_format=ROW check before endorsing mode 2, and by insisting application code tolerate ID gaps, the prompt keeps the fix from creating a far worse correctness problem downstream.
The guardrails encode hard-won operational reality: gaps in AUTO_INCREMENT are normal, mode changes need a restart and staging validation, and resetting counters on a replicated table is dangerous. That keeps a human owning the trade-off between raw insert throughput and the guarantees the application actually depends on.
Related prompts
-
MySQL InnoDB Deadlock Analysis Prompt
Decode the LATEST DETECTED DEADLOCK section of SHOW ENGINE INNODB STATUS and prescribe a fix.
-
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 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 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.