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
:selectto speed up queries by limiting which columns are selected - use
:includeto eliminate N+1 query problems - use
:joinsto 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