mcpField
P
performance-optimization

performance-optimization

github.com/thibautbaissacDevelopment67258 installsUpdated 22w ago
Repository

About performance-optimization

A performance optimization guide for Rails 8 developers, covering database queries, memory management, and response-time analysis. It presents optimization patterns within a detect, analyze, fix, and verify workflow.

Key features

  1. 01Detect and prevent N+1 queries
  2. 02Optimize queries and database indexes
  3. 03Analyze query plans and slow queries
  4. 04Profile memory use and application performance
  5. 05Process large datasets in batches

Use cases

  1. 01Investigate N+1 queries in page requests
  2. 02Optimize processing of large datasets
  3. 03Analyze slow queries and indexing needs
  4. 04Find memory issues in an application

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/thibautbaissac/rails_ai_agents --skill performance-optimization

After installing, type/performance-optimizationin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
memory-and-profiling.md
n-plus-one.md
query-optimization.md

Details

From the project README

Performance Optimization for Rails 8

Overview

Performance optimization focuses on:

  • N+1 query detection and prevention
  • Query optimization
  • Memory management
  • Response time improvements
  • Database indexing

Quick Start

# Gemfile
group :development, :test do
  gem 'bullet'           # N+1 detection
  gem 'rack-mini-profiler' # Request profiling
  gem 'memory_profiler'  # Memory analysis
end

N+1 Query Detection and Prevention

N+1 queries occur when code loads a collection then makes a separate query for each associated record. The Bullet gem detects these automatically. Fix them with eager loading via includes, preload, or eager_load.

Eager Loading Decision Table
MethodUse When
includesMost cases (Rails chooses best strategy)
preloadForcing separate queries, large datasets
eager_loadFiltering on association, need single query
joinsOnly need to filter, don't need association data

Key patterns: Bullet configuration, eager loading methods, scoped eager loading, counter caches, N+1 specs with query count assertions.

See references/n-plus-one.md for all code examples and patterns.

Query Optimization

Optimize queries by selecting only needed columns, using batch processing for large datasets, and choosing efficient existence checks.

Key Patterns
PatternBadGood
Column selectionUser.all.map(&:name)User.pluck(:name)
Large iterationsEvent.all.each { ... }Event.find_each { ... }
Existence checks.any? / .present?.exists?
Collection size.length (loads all).size (smart)
Database Indexing

Add indexes for: foreign keys, columns in WHERE/ORDER BY/JOIN clauses, and unique constraints. Use composite indexes for multi-column queries. Use partial indexes for filtered subsets.

Query Analysis

Use Event.where(...).explain(:analyze) to inspect query plans. Set up slow query logging via ActiveSupport::Notifications to catch queries over a threshold.

See references/query-optimization.md for all code examples and patterns.

Memory Management and Profiling

Use memory_profiler to detect memory issues. Prefer pluck over loading full AR objects, use find_each for streaming, and use update_all / in_batches for bulk operations.

Rack Mini Profiler

Provides per-request profiling in development. Shows query count, timing, and flamegraphs (with stackprof gem). Access via the profiler badge or ?pp=flamegraph.

See references/memory-and-profiling.md for all code examples and patterns.

Quick Fixes Reference

ProblemSolution
N+1 on belongs_toincludes(:association)
N+1 on has_manyincludes(:association)
Slow COUNTAdd counter_cache
Loading all columnsUse select or pluck
Large dataset iterationUse find_each
Missing index on FKAdd index on *_id columns
Slow WHERE clauseAdd index on filtered column
Loading unused associationsRemove from includes

Performance Checklist

  • Bullet enabled in development/test
  • No N+1 queries in critical paths
  • Foreign keys have indexes
  • Counter caches for frequent counts
  • Eager loading in controllers
  • Batch processing for large datasets
  • Query analysis for slow endpoints

Workflow

  1. Detect -- Enable Bullet, run specs, check Rack Mini Profiler
  2. Analyze -- Use explain(:analyze), check slow query logs, profile memory
  3. Fix -- Apply the appropriate pattern from the reference files
  4. Verify -- Re-run specs, confirm query counts, check profiler

Reference Files

Comments

Markdown supported