| name | description |
|---|---|
analyzing-rails-performance |
Analyze Ruby on Rails applications for performance issues. Use this skill when asked to find performance problems, optimize Rails apps, check for N+1 queries, investigate slow endpoints, or understand why something is slow. Covers N+1 query detection, missing indexes, inefficient ActiveRecord patterns, and memory bloat.
|
Analyze Rails applications for common performance issues using pattern matching, code analysis, and Rails-specific heuristics.
Search for patterns that commonly cause N+1 queries:
# Find iterations over associations without eager loading
ast-grep --lang ruby -p '$COLLECTION.each { |$VAR| $VAR.$ASSOC }'
# Find .map calls accessing associations
ast-grep --lang ruby -p '$COLLECTION.map { |$VAR| $VAR.$ASSOC }'Red flags to identify:
- Loops iterating over records and accessing associations
- Views rendering partials with
collection.eachthat access related records - Serializers/presenters accessing associations without preloading
See ./n-plus-one-patterns.md for comprehensive detection patterns.
Missing indexes: Check for columns used in where, find_by, order without indexes:
# Find where clauses to cross-reference with schema
ast-grep --lang ruby -p 'where($COLUMN: $$$)'
ast-grep --lang ruby -p 'find_by($COLUMN: $$$)'
ast-grep --lang ruby -p 'order($COLUMN)'Inefficient patterns:
Model.allfollowed by Ruby filtering instead of SQL- Multiple queries that could be combined with joins
pluckon large result sets without limits
See ./query-optimization.md for index analysis and query improvements.
Common memory issues:
- Loading entire tables with
Model.allinstead of batching - String concatenation in loops (use arrays + join)
- Unnecessary object creation in hot paths
# Find potential memory hogs
ast-grep --lang ruby -p 'Model.all'
ast-grep --lang ruby -p '$$.each { |$VAR| $STR + $$$}'See ./memory-patterns.md for memory optimization guidance.
- Identify the scope - specific controller, model, or entire app?
- Check for N+1 patterns - most common Rails performance issue
- Review database queries - missing indexes, inefficient scopes
- Examine memory patterns - especially in background jobs and batch operations
- Check eager loading - verify
includes,preload,eager_loadusage
app/controllers/- N+1 in controller actionsapp/models/- scope definitions, callbacks, associationsapp/views/- partials iterating over collectionsapp/serializers/- association access patternsdb/schema.rb- existing indexesconfig/database.yml- connection pool settings
# Find all includes/preload/eager_load usage
rg 'includes\(|preload\(|eager_load\(' app/
# Find potential N+1 in views
rg '\.each.*\.' app/views/
# Check for missing counter_cache opportunities
ast-grep --lang ruby -p '$$.count'When reporting findings, include:
- Location: File path and line number
- Issue: What the performance problem is
- Impact: Why it matters (query count, memory, response time)
- Fix: Specific code change to resolve it