Bug #70692 | auto_increment wrong value when using insert into... select from | ||
---|---|---|---|
Submitted: | 23 Oct 2013 3:46 | Modified: | 23 Oct 2013 6:21 |
Reporter: | ricky leung | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: DML | Severity: | S2 (Serious) |
Version: | 5.6.12,5.6.14,5.5.34,5.1.72 | OS: | Linux |
Assigned to: | CPU Architecture: | Any |
[23 Oct 2013 3:46]
ricky leung
[23 Oct 2013 6:21]
MySQL Verification Team
Hello ricky, Thank you for the bug report. Verified as described. Thanks, Umesh
[23 Oct 2013 9:25]
Peter Laursen
There have been lots of similar discussion here. In my understanding this is expected behavior with innodb_autoinc_lock_mode variable set to "1". Peter (not a MySQL/Oracle person)
[1 May 2023 15:48]
Oleksandr Peresypkin
According to the source code https://github.com/mysql/mysql-server/blob/8.0/sql/handler.cc#L3820, for statements like INSERT INTO ... SELECT, where the exact number of inserted rows is unknown in advance, the SQL handler reserves intervals of auto-increment values while processing the records, every time reserving twice as larger an interval as the previous one as soon as the number of rows reaches the highest value of the previous interval. For example, when inserting 4 records, first, an interval of 1 gets reserved, afterwards an interval of 2, then 4, and in the end 8. In this particular case, the next auto-increment value is going to be 8 instead of 5, and as a result, a gap between 4 and 8 appears. With larger numbers, the gap gets much more significant, for instance, while processing data in bulk say by 5000, the next auto-increment will be the current column value + 8192 instead of the current column value + 5000 + 1. As a workaround to this, data could be processed in batches equal to the value of 2^n-1. However, even with this, gaps can't be avoided at all but at least could be reduced.