Active Record No No's

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Active Record No No's - Presentation Transcript

    1. Active Record No Nos Use your common sense
    2. About me Patrick Hüsler Freelance developer Apple enthusiast Surfing and Kung Fu
    3. AR Kool Aid No giant xml mapping file Dynamic finders Associations Inheritance Polymorphism ...
    4. IT’S ALMOST WIZARDRY!!!!
    5. BUT...
    6. SO...
    7. Be reasonable or ...
    8. Code like this @user.friends.map {|friend| friend.id}
    9. Or this @user.friends.sort_by{ |friend| [ friend.online? ? 1 : 0,friend] }
    10. Or this friends.accepted.map{ |friend| friend.logs.visible_for(self) }.flatten
    11. eventually leads to
    12. THIS
    13. 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)
    14. 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)
    15. 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)
    16. 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)
    17. Image ressources http://www.gunaxin.com/wp-content/uploads/2009/05/kool_aid_man_glass.jpg http://www.thinkingfinance.net/blog/wp-content/uploads/2009/04/koolaidman.gif http://tux.crystalxp.net/png/chozo-mj-tux-gandalf-le-gris-1887.png http://icanhascheezburger.files.wordpress.com/2009/01/funny-pictures-cat-does-not-li http://midlifeslices.files.wordpress.com/2009/02/explosion-finger.jpg http://bmorearty.files.wordpress.com/2008/06/test-log-with-titles.png http://www.adoptedthemovie.com/wp-content/uploads/Picture_005.png http://comps.fotosearch.com/comp/TBZ/TBZ175/dynamite-magnifying-glass_~DYN014.jpg
    18. Contact info Available for hire patrick.huesler@gmail.com https://www.xing.com/profile/Patrick_Huesler http://twitter.com/phuesler http://github.com/phuesler
    SlideShare Zeitgeist 2009

    + Patrick HueslerPatrick Huesler Nominate

    custom

    240 views, 1 favs, 1 embeds more stats

    Avoid the most common pitfalls of Active Record and more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 240
      • 235 on SlideShare
      • 5 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 2
    Most viewed embeds
    • 5 views on http://www.huesler-informatik.ch

    more

    All embeds
    • 5 views on http://www.huesler-informatik.ch

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories