Rails跳过ID,可能存在数据库MySQL事务问题?

Rails跳过ID,可能存在数据库MySQL事务问题?,mysql,ruby-on-rails-3,transactions,Mysql,Ruby On Rails 3,Transactions,我有两个运行RubyonRails的应用服务器,使用InnoDB引擎共享同一个MySQL数据库服务器 有时在重载情况下,我发现表Users、X和Y中的记录ID被跳过,并且不是顺序的 这是伪代码: user = Create user x in table Users user_x = Find user x Insert a tuple into an auxiliary table X Insert a tuple into an auxiliary table Y Update th

我有两个运行RubyonRails的应用服务器,使用InnoDB引擎共享同一个MySQL数据库服务器

有时在重载情况下,我发现表Users、X和Y中的记录ID被跳过,并且不是顺序的

这是伪代码:

user = Create user x in table Users

user_x = Find user x

Insert a tuple into an auxiliary table X

Insert a tuple into an auxiliary table Y

Update the tuple that was inserted in table X

Insert a tuple into an auxiliary table Z
表X、Y、Z都以某种方式相关,我不会删除任何记录

然而,我发现有时记录ID会在重载期间跳过一到两个

交易对这个案子有帮助吗

Start transaction

  user = Create user x

  user_x = Find user x

  Insert a tuple into an auxiliary table X

  Insert a tuple into an auxiliary table Y

  Update the tuple that was inserted in table X

  Insert a tuple into an auxiliary table Z

Commit
Noobie的问题是,这段代码在没有加载的情况下运行良好,但在有加载的情况下运行一次。ie多个用户在同一时间访问同一资源,然后发生上述情况。交易会有帮助吗


将所有内容包装在事务块中会有什么缺点?

我不确定MYSQL,但在Postgres中,ID只从自动递增序列中分配一次,如果事务回滚或中止,ID将不被使用

您最好看看为什么跳过某些ID是一个问题。任何依赖于它们的顺序的东西都是可疑的


您是否在用户和表X、Y和Z之间使用外键?或者关系是否依赖于并行递增的ID?

应用程序不需要按顺序递增ID。我想我只是想看看为什么它会跳过,因为我没有删除行或更新ID。不,我没有使用外键,Rails应用程序在其数据库架构迁移中不使用外键。@Bill为了使序列递增具有低开销和非阻塞性,大多数数据库引擎会将值块分配给连接。如果发生错误、断开连接或服务器重新启动,将跳过这些操作,并且不会回滚自动递增的值。非常感谢,问题中的注释回答了我的问题。另外,我认为在复制应用服务器的环境中,跳过ID是很常见的。@Bill是的,插入的连接越多,跳过的ID就越多。此外,我相信任何提供id字段的手动插入,而不是从自动递增值中提取,都会将自动递增重置为MAX(id)+1。添加事务对自动递增值的跳过几乎没有影响。但是,如果您需要将要插入到用户X、Y和Z中的记录全部存在或全部失败,则需要一个事务。