← All writing

Measure query count before guessing at a slow page

Laravel 13 / PHP 8.3+Sources checked 2026-09-14

A page feels slow, so the first proposal is to add Redis. Before introducing another system, measure whether the request performs one slow query, hundreds of small queries, or spends its time outside the database.

Observe query events in a controlled environment

DB::listen(function ($query) {
    // Record duration and a safe query fingerprint in local diagnostics.
    // $query->time is the query duration in milliseconds.
});

This fragment shows the observation point, not a production logging policy. Query bindings may contain personal data or credentials. Do not dump every raw query and binding into a publicly readable log.

Count and duration answer different questions

An N+1 problem often appears as many similar queries. A missing index may appear as one expensive query. A slow provider call will not appear in database timing at all. Compare total request time with the measured components.

Use representative data volumes. A dashboard with four fixture rows can hide a full-table scan that becomes painful with hundreds of thousands of records. Record the query plan and the relevant row counts before and after a proposed index or eager-loading change.

Retest the same request with a cold and warm cache if caching is part of the design. State which condition produced the result. A faster local page is useful evidence, but not a promise of the same production latency on shared hosting. Keep performance claims tied to the environment and dataset actually measured.

Reference

Official documentation.