Everyday Active Record Episode 5: Optimizing Queries

Posted in Development, Frameworks, Databases on September 08, 2008

Everyday Active Record Episode 5: Optimizing Queries

Rails makes it easy to fetch records from the database without even writing SQL, but it’s equally easy to wind up with slow database queries. In this episode, we’ll use advanced find options to improve the performance of database queries in the cinemas application. We’ll also measure each change along the way to avoid premature optimization. As a result, you’ll be able to benchmark and improve the performance of your database queries. You’ll learn how to:

  • create a staging environment for performance testing
  • dynamically populate the staging database to simulate the production environment
  • use :select to speed up queries by limiting which columns are selected
  • use :include to eliminate N+1 query problems
  • use :joins to join associations with faster queries and a smaller memory footprint
  • add database indexes and understand how they work
  • benchmark the performance benefits of the various tuning options

Watch Video

Tags: Ruby, Ruby On Rails, Frameworks, Databases, ActiveRecord, ORM, The Pragmatic Programmers, Everyday Active Record