Skip to content Skip to sidebar Skip to footer

Rails Activerecord Eager Load Takes A Long Time

I am using eager loading to join two of my tables Model1.eager_load(:model2) The Model1 table has about 800 rows and has many references to other tables. Whenever that line is cal

Solution 1:

It's likely that this is not due to eager loading per se, but reflects the overhead of instantiating many model objects. ActiveRecord must initialize objects when retrieving them from a database. You can prove this to yourself by adding a callback to the associated model:

after_find :yodeldefyodel
  puts "Yodel-a-e-hoo!"end

This is pretty fast, but it is not instantaneous. When you are getting back many objects, that initialization time will start to affect performance.

The typical solution is to paginate results if possible. Otherwise you may need to re-write the query, using raw SQL if necessary, to be more selective.

Post a Comment for "Rails Activerecord Eager Load Takes A Long Time"