N+1 detection tools help find slow database queries caused by loading data inefficiently. They show where your app asks the database too many times instead of once.
N+1 detection tools in Ruby on Rails
gem 'bullet' # In config/environments/development.rb Rails.application.configure do config.after_initialize do Bullet.enable = true Bullet.alert = true Bullet.bullet_logger = true Bullet.console = true Bullet.rails_logger = true end end
Bullet is a popular gem to detect N+1 queries in Rails apps.
Enable Bullet only in development to avoid slowing production.
Bullet.enable = true Bullet.alert = true
Bullet.bullet_logger = true Bullet.console = true
Bullet.rails_logger = true
This setup uses Bullet to detect N+1 queries when loading posts and their comments without eager loading.
Bullet will alert you in the browser and logs about the N+1 problem caused by calling post.comments.count inside the loop.
# Gemfile gem 'bullet' # config/environments/development.rb Rails.application.configure do config.after_initialize do Bullet.enable = true Bullet.alert = true Bullet.bullet_logger = true Bullet.console = true Bullet.rails_logger = true end end # app/controllers/posts_controller.rb class PostsController < ApplicationController def index @posts = Post.all end end # app/views/posts/index.html.erb <% @posts.each do |post| %> <p><%= post.title %></p> <p><%= post.comments.count %></p> <!-- This triggers N+1 if comments not eager loaded --> <% end %>
Always fix N+1 warnings by eager loading related data using includes or preload.
Bullet can also detect unused eager loading to keep queries efficient.
Use Bullet only in development to avoid performance impact in production.
N+1 detection tools find repeated database queries that slow your app.
Bullet gem is a common tool in Rails to catch these problems early.
Fixing N+1 issues improves app speed and user experience.