gotchasqlModerate
Why does auto increment jumps by more than the number of rows inserted?
Viewed 0 times
rowswhyincrementthenumberautothanmoreinserteddoes
Problem
I am very perturbed by this weird behaviour I am seeing in the
For example, if the
Having check the following,
I have no idea why it is happening. What could be causing the jump in the
auto_increment value recorded in the bidID of a Bids table after performing bulk insertion using a stored procedure:INSERT INTO Bids (itemID, buyerID, bidPrice)
SELECT itemID, rand_id(sellerID, user_last_id), FLOOR((1 + RAND())*askPrice)
FROM Items
WHERE closing BETWEEN NOW() AND NOW() + INTERVAL 1 WEEK ORDER BY RAND() LIMIT total_rows;For example, if the
auto_increment bidID value is 101 at start, and I inserted 100 rows, the ending value becomes 213 instead of 201. However, the bidIDs of those inserted rows runs sequentially to a maximum of 201.Having check the following,
SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+I have no idea why it is happening. What could be causing the jump in the
auto increment value?Solution
This is not unusual and there are a couple of causes. Sometimes it is due to optimisations the query runner makes to reduce contention issues with the counter resource, improving efficiency when there are concurrent updates to the affected table. Sometimes it is due to transactions that got explicitly rolled back (or implicitly rolled back due to encountering an error).
The only guarantees from an
If you need the column's values to have no gaps at all you will need to manage the values yourself, either in another layer of business logic or in the DB via a trigger (be careful of potential performance issues with triggers though), of course if you do roll your own you will have to contend with all the concurrency/rollback/cleanup-after-delete/other issues that the DB engines work around by allowing gaps).
The only guarantees from an
auto_increment column (or IDENTITY in MSSQL, and the other names the concept goes by) is that each value will be unique and never smaller than a previous one: so you can rely on the values for ordering but you can not rely on them not to have gaps.If you need the column's values to have no gaps at all you will need to manage the values yourself, either in another layer of business logic or in the DB via a trigger (be careful of potential performance issues with triggers though), of course if you do roll your own you will have to contend with all the concurrency/rollback/cleanup-after-delete/other issues that the DB engines work around by allowing gaps).
Context
StackExchange Database Administrators Q#60295, answer score: 14
Revisions (0)
No revisions yet.