rails-query-optimization
Installation
SKILL.md
Rails Query Optimization
Skill Resources
- Activation and cross-agent notes: references/activation.md
- Use
ultrathinkor the deepest available reasoning mode before changing architecture, security, migration, or performance-critical paths.
includes solves the obvious N+1. The queries that bring down production are the ones that pass review because they look fine on a small dataset. This skill covers what happens after includes.
Reading EXPLAIN ANALYZE Output
Before optimizing any query, run EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) and read it correctly. Never guess; measure.
# In Rails console
sql = User.joins(:orders).where(orders: { status: "pending" }).to_sql
puts ActiveRecord::Base.connection.execute("EXPLAIN (ANALYZE, BUFFERS) #{sql}").to_a.map { |r| r["QUERY PLAN"] }.join("\n")