Faster eager loading and funky joins
Published almost 7 years ago

I was able to spend some time on a flight and at home to work on a very annoying performance pit associated with eager loading association’s instantiation code. So, with changeset 8051, hopefully you should see some performance improvement with eagerloading associations with large data sets ( even 100 rows should be good enough to notice the difference – remember, rails does a big fat assed cartesian join when you eager load multiple associations ) – at the cost of a little extra bit of memory. You can catch the mailing list discussion here.

On a very related side note, please take some time to check changeset 8054 as well. As the changeset contains really well written documentation, I wouldn’t reinvent the wheel here. This would make life a lot easier for those of you who are shit scared of sql and use eager loading in some of the worst ways possible because of that.

In any cases, I’d really encourage you to benchmark your code before choosing any solution. If you have never done it before, and you got scared by looking at the generated output, it’s time to try it again. I’d suggest you start with ruby-prof HTML call graphs which are explained very well here

The sample performance script can look as simple as :

require 'ruby-prof'
puts "Sanity check..."
puts Person.find(:all, :include => :items).inspect
results = RubyProf.profile { Person.find(:all, :include => :items) }
File.open "#{RAILS_ROOT}/tmp/profile-graph.html", 'w' do |file|
  RubyProf::GraphHtmlPrinter.new(results).print(file)
  `open #{file.path}`
end

And just run the script with script/runner of your rails application. For changeset 8051, you can see my before and after graphs to get a basic idea.