在 ruby 中查找每一个提高查询速度

find each in ruby increase the speed of query

我想每天从我们的数据库中删除大约 500 万条或更多实际上是过期用户的记录。

我已经用"find_each"批处理删除了这些记录,但是只需要1天就可以删除100万条记录,所以删除500万条记录需要5天:(这太费时间了消费。

有没有什么快速的方法可以安全地从 ruby.

的数据库中删除数百万条记录

这是我的代码:

now = Date.today
@expired_users = ExportUser.where("status != ? and DTSysModified >= ? and DTSysModified <= ?", "Active", Date.new(now.year, 04, 1),  Date.new(now.year, 04, -1))

@expired_users.find_each(batch_size: 10000) do |user|
  user.destroy!
end

delete_all 会比你想象的更快地完成工作:

@expired_users.delete_all

仅当您想要在删除对象时执行所有关联的回调时才使用destroy