MySql upsert和自动增量导致间隙
我有一个带有自动增量主键的 MySql 表,似乎所有各种 upsert 方法(INSERT IGNORE 和 ON DUPLICATE KEY UPDATE)都受到自动增量字段增量的影响,甚至如果一行已更新但未插入。这意味着表中引入了间隙,我认为这是不可取的。
所以问题是:如果更新插入实际上只是更新行,是否有任何方法可以在具有自动增量字段的表中更新插入记录而不自动增量该字段。在我看来,这是 upsert 应该表现的方式,但似乎并非如此。
I've got a MySql table with an auto-increment primary key, and it seems that all of the various upsert methods (INSERT IGNORE and ON DUPLICATE KEY UPDATE) suffer from the, uh, feature that the auto-increment field increments, even if a row is updated and not inserted. This means that gaps are introduced into the table, which I find undesirable.
So the question is: is there any way to upsert records in a table with an auto-increment field without auto-incrementing that field, if the upsert in fact merely updates the row. To my mind, this is the way upsert should behave, but it doesn't seem to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个“问题”仅存在于
InnoDB
中。这是设计使然,旨在提高并发性:另一个线程可以使用
AUTO_INCRMENT
而无需等待UPSERT
操作的结果。来自 文档 :
MyISAM
不会表现出这种行为,因为它的AUTO_INCRMENT
算法的实现方式不同(由于其支持并发DML
的能力有限)。This "problem" is only in
InnoDB
.It is by design, and intended to improve concurrency: another thread can use an
AUTO_INCREMENT
without having to wait for the results of anUPSERT
operation.From the docs:
MyISAM
does not exhibit this behavior, since it'sAUTO_INCREMENT
algorithm is implemented differently (due to its limited ability to support concurrentDML
).