n-plus-one-killer

Detect and eliminate N+1 queries in Ruby on Rails applications.

21|2|Updated May 24, 2026
One-click install
npx skills add https://github.com/sandeepmvl/rails-skills --skill n-plus-one-killer
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: n-plus-one-killer
Source: https://github.com/sandeepmvl/rails-skills/tree/main/skills/02-n-plus-one-killer
Command: npx skills add https://github.com/sandeepmvl/rails-skills --skill n-plus-one-killer

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires bullet, and includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill solves the pervasive Rails performance issue of undetected N+1 queries, which AI coding agents frequently introduce when generating controller and view code, leading to excessive database load and slow endpoint response times.

Core Features & Use Cases

  • N+1 Detection: Uses Bullet for association-aware detection in development and test, and Prosopite to catch raw query pattern N+1s that Bullet misses.
  • Optimization Patterns: Provides step-by-step fixes including eager loading with includes/preload/eager_load, counter cache implementation for count N+1s, and denormalization guidance for complex query scenarios.
  • Production Diagnosis: Includes workflows for identifying production-only N+1s via query log tags, APM tools, and pg_stat_statements, plus an EXPLAIN ANALYZE reference for deep SQL analysis.
  • Use Case: For a slow /posts index that loads post authors and comment counts per row, the skill identifies both N+1s, adds the correct eager loading, recommends a counter cache for comment counts, and wires up Bullet to catch future regressions in test suites.

Quick Start

Use the n-plus-one-killer skill to diagnose and fix the slow /posts endpoint that loads post authors and comment counts per row.

Frequently Asked Questions about n-plus-one-killer

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I detect and fix N+1 queries in Rails?

To fix Rails N+1 queries, this skill uses Bullet and Prosopite to detect unnecessary database loads, then applies eager loading with includes or preload to optimize slow endpoints.

What is the difference between Bullet and Prosopite for N+1 detection?

Bullet provides association-aware N+1 detection in development and test environments, while Prosopite catches raw query pattern N+1s that Bullet misses. Combining both ensures comprehensive detection of slow query patterns in Rails applications.

How do I find production-only N+1 queries in a Rails application?

Finding production-only N+1 queries requires analyzing query log tags, APM tools, and pg_stat_statements. This skill includes workflows for identifying these slow query patterns and using EXPLAIN ANALYZE for deep SQL analysis in production environments.

When should I use a counter cache instead of eager loading for count N+1s?

A counter cache should be used for count N+1s when you are repeatedly counting associations per row, such as comment counts on posts. This skill provides counter cache implementation patterns to replace inefficient count queries with cached columns.

Does this N+1 detection skill require the Bullet gem?

Yes, this N+1 detection skill depends on the Bullet gem for association-aware detection in development and test environments, while also integrating Prosopite to catch raw query pattern N+1s that Bullet might miss.

What is the best way to eager load associations in Rails to prevent N+1 queries?

The best way to eager load associations in Rails is using includes, preload, or eager_load depending on the query structure. This skill provides guidance on selecting the correct method and wiring up Bullet to catch future regressions.