# How to Diagnose Ruby on Rails N + 1 Query Problems

DevFeed: [How to Diagnose Ruby on Rails N + 1 Query Problems](<https://devfeed.tech/articles/how-to-diagnose-ruby-on-rails-n-1-query-problems-21072.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2016/04/17/how-to-diagnose-ruby-on-rails-n-plus-1-query-problems/>)

Published: 2016-04-17T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Query (disambiguation)](<https://devfeed.tech/topics/query.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Database](<https://devfeed.tech/topics/database.md>), [Heroku](<https://devfeed.tech/topics/heroku.md>), [Development](<https://devfeed.tech/topics/development.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [analysis](<https://devfeed.tech/tags/analysis.md>), [database](<https://devfeed.tech/tags/database.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [performance](<https://devfeed.tech/tags/performance.md>), [production](<https://devfeed.tech/tags/production.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-on-rails](<https://devfeed.tech/tags/ruby-on-rails.md>)

## AI overview

This tutorial explains how to diagnose Ruby on Rails N + 1 query problems that may be hidden in development but become visible under production traffic. It uses New Relic, including its Heroku add-on, as the primary diagnostic tool and also discusses analyzing application log files. The example demonstrates an application whose controller and view access users, posts, and themes through ActiveRecord, producing more database queries than necessary.

## Source excerpt

Diagnosing performance problems in a production Ruby on Rails application can be deceptively complex. When constructing a new application or expanding the features of an existing one, development environments that are not subject to typical production web traffic may not make performance issues evident. In those instances, the use of some simple (and mostly free) tools can help diagnose performance issues in production. If an application is hosted on Heroku, the New Relic add-on can be added to the application for free. Despite it having a few limitations, the free version of New Relic can be extremely valuable for diagnosing some common performance issues. New Relic will be the primary tool used in this post for analysis, but an application's log files can prove to be just as valuable when analyzed correctly. Setup The example application has the following database structure and respective models: create_table :users do |t| t.string :email t.string :first_name t.string :last_name t.timestamps(null: false) end create_table :posts do |t| t.integer :user_id t.integer :theme_id t.string :content t.timestamps(null: false) end create_table :themes do |t| t.string :name t.timestamps(null: false) end class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user belongs_to :theme end class Theme < ActiveRecord::Base end An example controller, home_controller.rb has a single action: class HomeController < ApplicationController def show @user = User.find(params[:user_id]) end end The above action renders a single simple view, show.html.haml: %h3= "Posts from #{ @user.first_name } #{ @user.last_name }" - @user.posts.each do |post| .theme= "Theme #{ post.theme.name }" .content= post.content Additionally in the application's Gemfile, the New Relic Ruby gem has been added: source 'https://rubygems.org' # ... gem 'newrelic_rpm' On a development machine under no contention, this action will quickly render a page consisting of a User's name inf