ActiveRecord 在事务内部执行批量插入/删除吗?
我需要什么:
- 确保原子更新(没有记录可以被处理 2 次)
- 批量删除所选的所有 1000 行
@queue = Queue.where("col = 1").limit(1000)
ids = []
@queue.each do |row|
Queue.do_something(row)
ids << row.id
end
Queue.delete_all("id in (#{ids.join(',')}) ")
是相同的
Queue.transaction do
@queue.each do |row|
Queue.do_something(row)
Queue.delete(row.id)
end
end
What I need:
- ensuring atomic updates (no record can gets processed 2 times)
- bulk deletion for all 1000 rows selected
@queue = Queue.where("col = 1").limit(1000)
ids = []
@queue.each do |row|
Queue.do_something(row)
ids << row.id
end
Queue.delete_all("id in (#{ids.join(',')}) ")
IS THE SAME AS
Queue.transaction do
@queue.each do |row|
Queue.do_something(row)
Queue.delete(row.id)
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于插入:
ActiveRecord 在使用事务时不会执行批量插入。然而,它确实加快了速度,因为它使用单个事务来执行所有 INSERT 语句,而不是每个 INSERT 语句一个事务。
因此:
将比:
有关如何真正进行批量插入的更多信息,请查看此 文章。
对于删除:
ActiveRecord
delete_all
调用是一个 SQL DELETE 语句,因此我想您可以将其视为批量删除(此处无需使用事务,因为它已被 ActiveRecord 封装在一个事务中) 。当对每条记录调用delete
时,情况并非如此,这将导致多个SQL DELETE语句,从而启动和提交多个事务,并且整体性能降低。For inserts:
ActiveRecord does not perform a bulk insert when using a transaction. However it does speed things up a bit since it is using a single transaction to execute all INSERT statements as opposed to one transaction per INSERT statement otherwise.
So:
is going to be faster than:
For more info on how to really do bulk inserts, check out this article.
For deletes:
The ActiveRecord
delete_all
call is one single SQL DELETE statement, so I guess you could consider this as a bulk delete (no need to use a transaction here since it's already encapsulated in one transaction by ActiveRecord). This is not the case when callingdelete
on each record, which will result in multiple SQL DELETE statements, thus multiple transactions initiated and committed and overall slower performance.我建议您看一下 ActiveRecord 导入: https://github.com/zdennis/activerecord-import。
我正在使用此工具插入 10,000 到 100,000 行数据。
如果您使用 MySQL,它还支持 ON DUPLICATE KEY UPDATE,因此您可以智能地插入新/更新旧行。 https://github.com/zdennis/ activerecord-import/wiki/MySQL:-On-Duplicate-Key-Update-Support
I suggest you take a look at ActiveRecord Import: https://github.com/zdennis/activerecord-import.
I'm using this tool to insert between 10,000 and 100,000 rows of data.
If you're using MySQL, it also supports ON DUPLICATE KEY UPDATE so you can intelligently insert new / update old rows. https://github.com/zdennis/activerecord-import/wiki/MySQL:-On-Duplicate-Key-Update-Support