namespace :perf do
desc 'Explain a hot query to verify it uses indexes'
task explain_hot_query: :environment do
sql = Snip.where('score > ?', 100).order(active_at: :desc).limit(20).to_sql
plan = ApplicationRecord.connection.execute("EXPLAIN #{sql}").values.join("
")
puts plan
end
end
When adding a new query path, run EXPLAIN in CI or a smoke task to catch missing indexes before production. You don’t need full query plans everywhere—just guard the hot paths.