Slow queries are the most common reason a Laravel app gets slower as it succeeds. The query that returned instantly against a few hundred rows in development is the same query scanning two million rows in production, and nothing in your code changed. The data did. That's the frustrating shape of database performance: it degrades gradually, on a curve you can't see from the code, until a page that was always fine crosses a threshold and starts timing out. Catching it early is entirely about measurement, because you can't reason your way to which query grew.
Layer 1: Database slow-query log
Your database already knows which queries were slow. Turn the log on as a baseline.
PostgreSQL (postgresql.conf)
log_min_duration_statement = 500 # log anything over 500ms
log_line_prefix = '%m [%p] %q%u@%d '
log_statement = 'none'MySQL (my.cnf)
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 0.5
log_queries_not_using_indexes = 1It's cheap and it sees every query, but it has no request context: you get the SQL but not which controller fired it or what the user was doing. Useful for capacity planning and index design; not actionable for "the /orders page is slow."
Layer 2: Laravel query hooks
Laravel gives you two hooks in AppServiceProvider::boot().
Fire once per request when total query time exceeds 500ms
use Illuminate\Support\Facades\DB;
DB::whenQueryingForLongerThan(500, function ($connection, $event) {
logger()->warning('Slow request: SQL queries exceeded 500ms', [
'url' => request()->fullUrl(),
'method' => request()->method(),
'user_id' => auth()->id(),
]);
});Fire on every query (keep the callback cheap)
DB::listen(function ($query) {
if ($query->time < 100) return;
logger()->warning('Slow query', [
'sql' => $query->sql,
'bindings' => $query->bindings,
'time_ms' => $query->time,
'connection' => $query->connectionName,
]);
}); DB::listen runs for every query. At 1,000 req/sec with 10 queries per request, that's 10,000 callback invocations per second. Keep the callback cheap (never write to the database or call the network from it) and prefer the whenQueryingForLongerThan variant for production-facing logic.
Layer 3: Full APM with pattern grouping
The real unlock is grouping queries by fingerprint. A raw log says "this query took 800ms at 14:23." Pattern grouping says "this query pattern has fired 840,000 times today with a p95 of 640ms, up from 90ms yesterday."
Laravel already sends bound values as ? placeholders, so queries from the same line of code usually share one SQL string. Nightwatch's fingerprint is a hash of the connection name and that SQL, with in (...) lists and insert values collapsed so a different number of items doesn't split the group. Literals the query builder writes straight into the SQL, like the limit, stay as they are:
-- SQL Laravel sends (the values travel separately as bindings):
select * from "orders" where "user_id" = ? and "status" in (?, ?) order by "created_at" desc limit 10
select * from "orders" where "user_id" = ? and "status" in (?, ?, ?) order by "created_at" desc limit 10
-- Both roll up into one pattern, hashed as:
select * from "orders" where "user_id" = ? and "status" in (...?) order by "created_at" desc limit 10
-- A different limit is a separate pattern:
select * from "orders" where "user_id" = ? and "status" in (?, ?) order by "created_at" desc limit 25Options that do this out of the box:
- Laravel Nightwatch Cloud: official
- NightOwl: BYOD Postgres dashboard on the same Nightwatch instrumentation
- Sentry / Scout / New Relic: generic APMs, weaker at Laravel-specific views
What to actually fix first
Sorted by impact:
- Queries repeated dozens of times in one request: N+1s. See our N+1 guide.
- Patterns with high call count and mid-range duration: 0.5M daily calls at a 150ms average is 75,000 seconds of DB time per day. Missing index, usually.
- Individually slow queries (p95 > 1s): often full table scans or large OFFSETs. Use EXPLAIN to see the plan. EXPLAIN ANALYZE runs the statement for real, so point it at reads or a copy of the data.
- Queries with growing duration trend: healthy yesterday, slow today. Usually the table grew past an index's sweet spot.