If you are using InnoDB
with INSERT IGNORE
, you may want to read this article that talks about this issue:
Avoiding auto-increment holes on InnoDB with INSERT IGNORE
Why do we have gaps?
InnoDB checks an auto_increment counter on the table and if a new value is needed, increments that counter and assigns the new value to the column. Prior to MySQL 5.1.22 InnoDB used a method to access that counter values called “Traditional“. This one uses a special table lock called AUTO-INC that remains until the end of the query or transaction. Because of this, two queries can’t have the AUTO-INC lock at the same time, so we lose concurrency and performance. The problems are even worse with long running queries like INSERT INTO table1 … SELECT … FROM table2.
When using a simple insert from select, there's a known bug about this
In conclusion, i wouldn't say this is a problem with your query, but more related to the way the engine deals with the insert from select blocks.