How can I run updates in batches in Rails 3/4?

后端 未结 6 1076
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 01:09

I need to mass-update many thousands of records, and I would like to process the updates in batches. First, I tried:

Foo.where(bar: \'bar\').find_in_batches.upda         


        
6条回答
  •  盖世英雄少女心
    2021-02-05 02:06

    This is 2 years late, but the answers here are a) very slow for large data sets and b) ignore the builtin rails capabilities (http://api.rubyonrails.org/classes/ActiveRecord/Batches.html).

    As the offset value increases, depending on your DB server, it will do a sequence scan until it reaches your block, and then fetches the data for processing. As your offset gets into the millions, this will be extremely slow.

    use the "find_each" iterator method:

    Foo.where(a: b).find_each do |bar|
       bar.x = y
       bar.save
    end
    

    This has the added benefit of running the model callbacks with each save. If you don't care for the callbacks, then try:

    Foo.where(a: b).find_in_batches do |array_of_foo|
      ids = array_of_foo.collect &:id
      Foo.where(id: ids).update_all(x: y)
    end
    

提交回复
热议问题