AUTO_INCREMENT ids jump after INSERT ... ON DUPLICATE KEY UPDATE
Fix it fast
Most common cause, 1 of 2The upsert updated a row but still took an id
Treat ids as identifiers, not as a row count.
ALTER TABLE tags MODIFY id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;
-- update first, insert only when no row matched
UPDATE tags SET hits = hits + 1 WHERE name = 'red';
-- if the affected-row count was 0:
INSERT INTO tags (name, hits) VALUES ('red', 1);Short answer
InnoDB counts INSERT ... ON DUPLICATE KEY UPDATE as a mixed-mode insert: it can allocate an AUTO_INCREMENT value before it finds the duplicate, and that value is never reused, even when the row is only updated or the transaction rolls back. The gaps are by design. Widen the id column, or UPDATE first and INSERT only new rows. MySQL 8.4.
Which cause do you have?
Note the highest id, run the upsert for a key that already exists, then add one genuinely new row and compare its id with the highest id from before.
SELECT MAX(id) FROM tags;
INSERT INTO tags (name, hits) VALUES ('red', 1)
ON DUPLICATE KEY UPDATE hits = hits + 1;
INSERT INTO tags (name, hits) VALUES ('probe-new-row', 1);
SELECT LAST_INSERT_ID();Then find the line you see in this list and open its fix:
- 1The upsert updated a row but still took an id
The client reports 2 rows affected for the upsert, and the new row's id is higher than MAX(id) + 1 - 2The upsert ran in a transaction that rolled back, or in a statement that failed
Gaps line up with batches that the application log shows as failed or rolled back, and the Com_rollback counter keeps growing
What does the error look like?
Ids go 1, 2, 7, 8 although no rows were deletedQuery OK, 2 rows affectedThe AUTO_INCREMENT counter in SHOW CREATE TABLE climbs after upserts that only updated existing rows
Cause 1 of 2
The upsert updated a row but still took an id
You seeThe client reports 2 rows affected for the upsert, and the new row's id is higher than MAX(id) + 1
Check
The gaps come from how InnoDB handles this statement. The manual lists INSERT ... ON DUPLICATE KEY UPDATE as a mixed-mode insert that in the worst case acts as an INSERT followed by an UPDATE, where the allocated AUTO_INCREMENT value may or may not be used during the update. A value, once generated, is not handed out again, so each upsert that ends as an update can leave a hole. Interleaved lock mode 2 is the default, and the mode is configured at server startup.
SELECT @@GLOBAL.innodb_autoinc_lock_mode;Fix
Treat ids as identifiers, not as a row count. Give upsert-heavy tables a BIGINT UNSIGNED id so the extra consumption never reaches the type's maximum. When most calls hit existing rows, run the UPDATE first and insert only if it matched nothing: an UPDATE adds no row, so it needs no new AUTO_INCREMENT value. If two sessions race, the second INSERT fails with a duplicate-key error; catch it and repeat the UPDATE.
ALTER TABLE tags MODIFY id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;
-- update first, insert only when no row matched
UPDATE tags SET hits = hits + 1 WHERE name = 'red';
-- if the affected-row count was 0:
INSERT INTO tags (name, hits) VALUES ('red', 1);Verify
A repeat of the first step shows the new row taking MAX(id) + 1 when the update-first path is used.
SELECT MAX(id) FROM tags;Confirmed by dev.mysql.com
Cause 2 of 2
The upsert ran in a transaction that rolled back, or in a statement that failed
You seeGaps line up with batches that the application log shows as failed or rolled back, and the Com_rollback counter keeps growing
Check
In all three lock modes, values generated by a statement are lost when its transaction rolls back, and they are lost whether or not the statement completed. A batch of upserts that fails halfway still uses up the ids it reserved.
SHOW GLOBAL STATUS LIKE 'Com_rollback';Fix
Nothing in MySQL needs repair. If a gap-free number matters to the business, such as an invoice number, keep it in its own column filled by the application, and leave the AUTO_INCREMENT id as an internal key.
ALTER TABLE invoices ADD COLUMN invoice_no INT UNSIGNED NULL,
ADD UNIQUE KEY uq_invoice_no (invoice_no);Verify
Reports and users see the business number, and gaps in the id column no longer matter to anyone.
SELECT id, invoice_no FROM invoices ORDER BY invoice_no DESC LIMIT 5;Confirmed by dev.mysql.com
How do I stop it coming back?
- Pick BIGINT UNSIGNED for the id of any table written mostly by upserts.
- Never show AUTO_INCREMENT ids to users as a count or a gap-free sequence.
- Avoid ON DUPLICATE KEY UPDATE on tables with more than one unique index, as the manual advises.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, ShopGo.