Rails Relation # update_all consumes more memory than updating Relation #

The document Relation#update(id, attributes) docs mentions that "the resulting object is returned regardless of whether the object was successfully stored in the database or not", while Relation#update_all(updates, conditions = nil, options = {}) declares that no instances, callbacks or checks are performed (direct database request).

By performing a simple performance test and replacing Relation#update with Relation#update_all , it seems that memory consumption has increased.

Any explanation?

Excerpts:

 #Before MyModel.update(my_hash['id'], special_attrs) #After (more memory consumed) MyModel.where(:id => my_hash['id']).update_all(special_attrs) #Update-2 (slightly more memory consumed than with update(id, attrs) MyModel.update_all(special_attrs, {:id => my_hash['id']}) 
  • Windows XP (yeh - I know)
  • JRuby 1.6.8 (1.9)
  • Rails 3.2.8

UPDATE . Some basic numbers for comparison: For about 700 update applications, there is a difference of about 0.6MB (0.8kB per operator)

UPDATE 2 . Removed AREL chain as suggested by @ philip-hallstrom. A slight decrease in memory, but still MyModel.update_all(attrs, conditions) consumes more memory than MyModel.update(id, attrs) .

+4
source share
1 answer

Is there any difference:

MyModel.update_all(special_attrs, {:id => my_hash['id']})

I did not dig too far, but it seems that update_all calls Arel right off the bat. I settled on .update -> .update_attributes -> .assign_attributes and didn’t see Arel at all, although I’m sure that he should enter the game at some point.

If I were to guess, I would say why.

I guess this is somewhere in the loop? Otherwise, why not just do:

MyModel.update_all(special_attrs, {:id => all_my_hash_ids_as_an_array})

0
source

Source: https://habr.com/ru/post/1443499/


All Articles