我有一个查询要select
和insert
表中的行,自动递增col 作为键。
insert into table a select * from table B
我发现执行查询后,表B
中的自增列存在序列间隙
这可能吗? 或者我的 select 语句未能插入一些记录??
请帮忙。
我无法重现这种情况,因为这是一个生产服务器,而且我在执行查询之前没有备份。
非常感谢。
最佳答案
如果您将 InnoDB
与 INSERT IGNORE
一起使用,您可能需要阅读讨论此问题的这篇文章:
Avoiding auto-increment holes on InnoDB with INSERT IGNORE
为什么我们会有差距?
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.
当使用来自 select 的简单插入时,有一个 known bug about this
总而言之,我不会说这是您的查询的问题,而是与引擎处理来自选择 block 的插入的方式有关。
关于mysql查询批量插入导致序列间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18130920/