This tutorial aims to improve your understanding and skills in optimizing database queries, specifically in the context of a Rails application. We will focus primarily on how to write efficient queries and use Active Record effectively.
By the end of the tutorial, you'll learn:
Prerequisites:
- Basic understanding of Ruby on Rails
- Familiarity with Active Record
- Basic knowledge of SQL
N+1 query problem is a common issue that results in performance degradation. It occurs when the code needs to load the children of a parent-child relationship, but it does it in an inefficient way.
Here's an example:
# N+1 problem
authors = Author.all
authors.each do |author|
puts author.books.count
end
For each author, the database is hit again to find the books. It's inefficient.
Active Record provides the includes
method to help solve this problem. It works by loading all the data that's going to be needed upfront.
# Using includes
authors = Author.includes(:books)
authors.each do |author|
puts author.books.count
end
This time, the database is hit twice only, irrespective of the number of authors.
When you only need specific columns from a table, use select
and pluck
to avoid fetching unnecessary data.
# Using select and pluck
titles = Book.pluck(:title)
find_each
for batch processingWhen dealing with large amounts of data, find_each
method fetches data in batches, reducing memory usage.
# Using find_each
Book.find_each(batch_size: 50) do |book|
puts book.title
end
This will fetch and process books in batches of 50, reducing memory footprint.
counter_cache
to speed up countingThe counter_cache
option can be used to automatically keep a count of a model's has_many association.
# Using counter_cache
class Book < ActiveRecord::Base
belongs_to :author, counter_cache: true
end
This will add a books_count
column on the authors table, and Rails will automatically update this counter whenever a book is added or removed.
In this tutorial, we've learned:
includes
select
and pluck
to fetch only necessary datafind_each
to process large amounts of datacounter_cache
For further learning, look into database indexing, and more advanced Active Record techniques.
Book.all.each do |book|
puts "#{book.author.name}: #{book.title}"
end
Solutions:
books = Book.where(author_id: author_ids)
counter_cache: true
to the belongs_to
association in the Book model, then use author.books.size
.includes
to load authors upfront: Book.includes(:author).each { |book| puts "#{book.author.name}: #{book.title}" }
.