In a Rails development log, what do Grade Load (12.4ms), CACHE Grade Load (0.0ms) and ActiveRecord: 350.1ms (48 queries, 6 cached) each measure?
answer
- time per statement, seen from Ruby
- CACHE: the query cache answered
- object building is not in it
- queries run while rendering count as ActiveRecord
- the 48 includes the 6 cached
basics
~20 sGrade Load (12.4ms) is the time one SELECT took inside the database adapter, as measured from Ruby. CACHE means Active Record's query cache answered it without the database. ActiveRecord: 350.1ms (48 queries, 6 cached) totals every statement in the action.
solid answer
~50 sEach SQL line is Active Record's log subscriber reporting one `sql.active_record` event: the label is the model and operation (`Grade Load`, `Student Count`, `Grade Update`, or `SQL` for raw statements), and the milliseconds are the wall-clock time spent in the adapter sending the statement and receiving its result, network included, without turning rows into `Grade` objects. A `CACHE` prefix means the request's query cache returned a result already fetched for identical SQL, so no database round trip happened. On the `Completed` line, `ActiveRecord: 350.1ms (48 queries, 6 cached)` sums every statement in the action; the 48 includes the 6 cache hits. Queries fired while a view renders are moved out of `Views:` into the `ActiveRecord:` figure. Whatever is left of the total, beyond views and SQL, is Ruby work. These SQL lines are `debug` output.
code
ruby · 9 linesclass ReportCardsController < ApplicationController
def show
@student = Student.find(params[:id]) # Student Load (0.3ms)
@grades = @student.grades.to_a # Grade Load (12.4ms)
@count = @student.grades.count # Grade Count (1.1ms)
@again = Student.find(params[:id]) # CACHE Student Load (0.0ms)
end
end
# Completed 200 OK in 61ms (Views: 20.4ms | ActiveRecord: 13.8ms (4 queries, 1 cached) | GC: 0.9ms)go deeper
Recall that each SQL line shows the model, the operation and its duration, and that CACHE means the database was not asked.
Explain what the duration includes and excludes, that the query count includes cached hits, and that SQL run during rendering is moved from Views to ActiveRecord.
Use the split between views, SQL and the remainder to tell a slow query from a query loop or from Ruby-side instantiation, and know these lines vanish at info level.
Decide which request-level timings your team watches in production, since per-statement SQL lines are a development tool.
## Where the numbers come from Every statement Active Record runs is wrapped in an instrumentation event named `sql.active_record`. Two consumers read it: - Active Record's **log subscriber**, which prints one line per statement at the `debug` level; - a **runtime registry**, which adds the duration to per-request totals that Action Controller prints on its `Completed` line. So both the per-line figures and the totals come from the same measurement. ## The per-statement line ``` Grade Load (12.4ms) SELECT "grades".* FROM "grades" WHERE "grades"."student_id" = ? [["student_id", 42]] ``` | Part | Meaning | |---|---| | `Grade Load` | the model and the operation: `Load`, `Count`, `Create`, `Update`, `Destroy`; plain `SQL` for statements without a model name | | `(12.4ms)` | wall-clock time inside the adapter for this statement | | the SQL | the statement as sent, including any query log tag comment | | `[[...]]` | bind values, passed through the model's attribute filter | What the 12.4 ms **includes**: sending the SQL, the database executing it, the network round trip and the adapter reading the result. What it **excludes**: building `Grade` instances from the rows, which Active Record instruments separately, and any Ruby work you do with the records afterwards. A query that takes 2 ms but returns 50,000 rows can still make the page slow. Two prefixes change the meaning: - **`CACHE Grade Load (0.0ms)`**: the query cache, active during a request, saw identical SQL earlier in the same request and returned the stored result. Nothing went to the database. A write in the request clears this cache. - **`ASYNC Grade Load (0.4ms) (db time 9.8ms)`**: the relation was started with `load_async`; the first number is how long the request thread waited for it, the second how long the query itself took on the background thread. ## The `Completed` line ``` Completed 200 OK in 512ms (Views: 101.3ms | ActiveRecord: 350.1ms (48 queries, 6 cached) | GC: 12.0ms) ``` Reading the `ActiveRecord:` part: 1. **350.1ms** is the sum of the durations of every statement in the action, cache hits included (they are close to zero). 2. **48 queries** counts statements, including the **6 cached** ones; transaction statements are not counted. 3. Queries that ran **while a template rendered**, such as a lazy association iterated in a partial, are subtracted from `Views:` and added to `ActiveRecord:`. `Views:` is therefore template time without SQL. 4. The remainder, `512 - 101.3 - 350.1`, is Ruby outside views and SQL: controller code, object building, serialisation, and part of `GC:`. ## Turning numbers into a diagnosis - **Many cheap statements, same shape**: dozens of `Grade Load (0.3ms)` with different ids is a query-per-record loop. Fixing it belongs to eager loading; the log tells you it is happening. - **One expensive statement**: a single `Grade Load (310.0ms)` points at the database side: indexes, plan, row counts. - **Small SQL total, slow request**: if `ActiveRecord:` is small but the total is large, the time is in Ruby, often instantiating far more records than the page shows. - **Many `CACHE` lines**: harmless for the database, but each still rebuilds objects from the cached rows. ## A worked reading ``` Student Load (0.3ms) SELECT "students".* FROM "students" WHERE "students"."id" = ? LIMIT ? Grade Load (0.4ms) SELECT "grades".* FROM "grades" WHERE "grades"."enrollment_id" = ? Grade Load (0.4ms) SELECT "grades".* FROM "grades" WHERE "grades"."enrollment_id" = ? ... 38 more ... Completed 200 OK in 240ms (Views: 150.2ms | ActiveRecord: 21.0ms (42 queries, 0 cached) | GC: 3.0ms) ``` Forty-two statements adding up to 21 ms is not a slow database; it is a loop. The remaining time is in the views and Ruby, so the next step is the `↳` lines and the number of records each partial renders. ## Caveats - These lines are `debug` output. A production app at the default `info` level shows only the `Completed` line, without the per-statement detail. - Timings are from the Ruby process, so they include network latency to the database; they are not the database's own execution time. - A first request after boot pays for loading schema information and connecting; compare warm requests.
- A report card page shows Views: 900ms, but the template looks trivial. What should you check first?Whether the time is really view time. Queries fired during rendering are moved into `ActiveRecord:`, so a large `Views:` usually means Ruby work in the templates: rendering many partials, helpers building strings, or instantiating many records. Look at the per-statement lines and their `↳` locations to see what the templates triggered, and at how many records each query returned.
- Why can a CACHE line still cost something even though it shows 0.0ms?The query cache skips the database round trip, but Active Record still builds new model objects from the cached rows for each call. Repeating a large cached query in a loop costs Ruby time and memory, which shows up in the request total rather than in the `ActiveRecord:` figure.
- The (12.4ms) figure is the database server's own execution time only.
- CACHE lines mean the result came from Rails.cache or Solid Cache.
- The query count on the Completed line excludes cached queries.
- Queries run inside a partial are counted in the Views: time.
- The per-statement time includes building the model objects from the rows.