Benito Serna Tips and tools for Ruby on Rails developers

What is an n+1 queries problem?

An n+1 queries problem means that a query is executed for every result of a previous query.

For example, with this records…

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

If you execute the next code…

Post.all.each do |post|
  post.comments.each do |comment|
    puts comment.body
  end
end

You will execute…

If there are…

Related articles

Do you feel bad for asking for help to fix an n+1 queries problem?

Making things work isn't enough for you any more? Now you need to consider performance and scalability?

... But you normally have troubles fixing n+1 queries and trying to find why active record is ignoring your "includes"?

Are you are worried because you feel unqualified to tackle tasks with complex data models?

Sign up to learn how to fix n+1 queries on Rails