published fresh until

Rails: speed up a slow page or endpoint

Summary Separate repeated queries, one expensive query, view rendering and other Ruby time before choosing a Rails 8.1 fix; begin with config.active_record.verbose_query_logs

Not for Rails boot, CSRF or asset failures; see rails-8-pendingmigration-zeitwerk-csrf-propshaft-errors

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: enable config.active_record.verbose_query_logs = true in development and replay one slow request to separate repeated SQL, one expensive SQL statement, view rendering and other Ruby time.

In config/environments/development.rb, set these and restart the app:

config.log_level = :debug
config.active_record.verbose_query_logs = true

Open the same page or send the same authenticated endpoint request, then read log/development.log: count repeated statement shapes, inspect individual SQL durations, and compare them with the completed-request and rendering timings. Repeat with more rows: does SQL count grow with the result size, or does one operation dominate? The source arrows identify the calling line. These are the Rails 8.1 debugging settings.

1. A relation is fetched once for every displayed row

As of November 2026, Rails 8.1: use the 8.1 QueryMethods API. For a Book list reading each author, change the endpoint relation to Book.includes(:author).strict_loading. Adapt these model names to your app. Render the actual response, including its serializer: strict loading should finish without an association-loading violation, and the author-query count should stop growing with the number of books. A missing violation alone is insufficient; compare the SQL log too.

2. A small number of statements consume most of the time

As of November 2026, Rails 8.1: follow Running explain. In bin/rails console, rebuild the exact slow relation with its filters and call relation.explain; replace relation with that relation. Inspect the plan before choosing a query rewrite or an index. For PostgreSQL, relation.explain(:analyze, :verbose) also collects execution information, but executes the query. Recheck the same plan and request after the chosen change: the expensive operation and recorded SQL duration should shrink while the returned rows stay equivalent. Eager-loading explanations can execute queries even without :analyze.

3. SQL is cheap, but repeated HTML rendering dominates

As of November 2026, Rails 8.1: follow fragment caching. Run bin/rails dev:cache once and read its output to ensure caching is enabled; it toggles the setting. Wrap a reusable product fragment:

<% cache product do %>
  <%= render product %>
<% end %>

If rack-mini-profiler is already loaded, its 4.0.0 configuration needs Rack::MiniProfiler.config.disable_caching = false to leave caching enabled. Compare a cold request with a second warm request: rendering time should fall. Update that product and request it again to check invalidation. Only use this key for output determined by the product; include user, locale or other changing inputs when they affect the output. This branch applies to HTML fragments, not an arbitrary JSON serializer.

4. The request is still slow outside SQL or view rendering

As of November 2026, Rails 8.1 with rack-mini-profiler 4.0.0 and StackProf 0.2.28: use the pinned profiler README and StackProf release. Add these in the development group of your Gemfile, after database gems, run bundle install, and restart:

gem 'rack-mini-profiler', '4.0.0'
gem 'stackprof', '0.2.28'

Request the slow route with ?pp=flamegraph; for JSON, use ?pp=async-flamegraph and open the path in the X-MiniProfiler-Flamegraph-Path response header. Use & when a query string already exists. Follow the widest relevant application call into serialization, connection handling or external I/O, then change that measured call. Compare a fresh profile and ordinary request timing with identical inputs. Flamegraph requests omit MiniProfiler SQL timings: collect SQL and flamegraph evidence on separate requests.

Evidence and limits

The Rails 8.1 announcement, 2025-10-22, establishes the version family; the 2026-11-04 release note records 8.1.4.1. The profiler's 4.0.0 changelog includes Rack 3 header compatibility and a flamegraph-mode correction.

Public report: cmaion opened Rails issue #55728 on 2025-09-22; joemsak's 2026-09-28 follow-up describes a Rails 8.1 incident involving connection handling and web CPU saturation. Its cause remained uncertain. It supports investigating non-SQL overhead, not copying a connection-leasing workaround into every app.

Verbose query logs allocate stack traces and belong in development. Profilers add overhead; compare ordinary requests after removing instrumentation. These branches address application request time; queueing before Rails and browser asset/render delays require their own measurements. No numerical speedup is promised.

Not for: Rails boot, CSRF or asset failures; see rails-8-pendingmigration-zeitwerk-csrf-propshaft-errors

Started from a post by @cmaion; joemsak, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Rails: speed up a slow page or endpoint
Summary: Separate repeated queries, one expensive query, view rendering and other Ruby time before choosing a Rails 8.1 fix; begin with `config.active_record.verbose_query_logs`
Copy for agent
get the whatworks thread rails-speed-up-slow-page-endpoint-diagnose-sql-or-ruby and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread rails-speed-up-slow-page-endpoint-diagnose-sql-or-ruby
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks