15. We can do better ...
Look at the generated SQL
Create indices
Let AR/SQL do the math (conditions,count,sum,
named_scope, ...)
Paginate, but paginate in SQL not on ruby collections
Only fetch what you really need (:select => :id)
Use :include (n + 1 problem)
16. Check SQL logs
tail -f log/*.log
Log to STDOUT in script/console
Do it once in ~/.irbrc
Use one of the query analyzer plugins
Use profiling tools like New Relic RPM, FiveRuns
TuneUp or Rack::Bug (Check railscast)
17. Where to look
Iterations/calculations in views / partials
Calculations on potentially big collections in ruby that
can be done in SQL
@user.friends.recent.sort_by{ |friend| [ friend.online? ?
1 : 0,friend] }
Pagination on collection instead of SQL
User.all.paginate(:per_page => 1, :page => 1)
18. Where to look
Not eager loaded associations
Post.all.each{|post| puts post.author.name}
Classic n + 1 problem
Post.all(:include => :author)
Ruby is used for tasks that could be done with SQL
Post.all.map(&:id)
Instead of Post.all(:select => :id)
20. Contact info
Available for hire
patrick.huesler@gmail.com
https://www.xing.com/profile/Patrick_Huesler
http://twitter.com/phuesler
http://github.com/phuesler