mysql查询批量插入导致序列间隙

标签 mysql insert sequence auto-increment bulk

我有一个查询要selectinsert 表中的行,自动递增col 作为键。

insert into table a select * from table B

我发现执行查询后,表B中的自增列存在序列间隙

这可能吗? 或者我的 select 语句未能插入一些记录??

请帮忙。

我无法重现这种情况,因为这是一个生产服务器,而且我在执行查询之前没有备份。

非常感谢。

最佳答案

如果您将 InnoDBINSERT 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/

相关文章:

mysql - 基数违规 : 1242 Subquery returns more than 1 row

MySQL:在多边形内获取记录

php - 我怎么知道我的 mysql_query 更新是否实际上没有更新任何东西?

node.js - 顺序递增跳数

swift - 协议(protocol)符合Sequence协议(protocol),同时消除通用约束问题

mysql - 使用mysql子查询更新错误: Subquery returns more than 1 row

java - 从 Java 插入 MySql 时出现语法错误

arrays - 具有递增重复项的独特数组算法

clojure - 为什么 Clojure 将字符串强制转换为带有 cons 的字符

php - MySQL/PHP 数据库连接以从 Web 表单输入文本