Keeping Laravel Fast Where the Real Data Lives
- Laravel
- Performance
- k6
- mySQL
Most of the slow pages I've had to fix in Laravel apps were fast on my machine. The code was the same and so were the queries, but the data wasn't. A query that scans a few hundred rows locally will scan a few million rows in production, and that scale difference can cause seconds of additional load time for users. For a long time I only found these pages when someone complained, and by then the slowness had usually been there for a while.
So I stopped trying to judge performance locally and started measuring it where the data actually lives. I do this in two ways: a probe that times pages from the outside, and the database's own statistics from the inside. Together they catch most of what I run into. Neither catches all of it, and I'll get to that at the end.
Why local lies
Local environments differ from production in lots of small ways, like OPcache settings, queue drivers and network latency. But the difference that matters most is the scale of the data.
Take a filter on a column with no index. Locally the table has 400 rows, so MySQL reads all 400, the page renders in 80 ms and nobody thinks twice. In production the same table has a few million rows, and that full table scan runs on every page load. The code review passes, the tests pass and the page feels fine in development. The only place the problem exists is the environment you weren't looking at.
That's the whole argument for measuring on staging and production: they're the only places where the numbers are true.
Probing pages with k6
I use k6 as a probe rather than as a load generator. One virtual user signs in as a real account, then works through a fixed list of pages in shuffled rounds, one request roughly every ten seconds, for 30 to 60 minutes. That's gentle enough to run against production, because it stays under the site's rate limits and looks like one fairly slow person clicking around. Run it after a deploy, or whenever something feels off, and you get a time for every page.
The number I care about most is the p95. If you sort every response time for a page from fastest to slowest, the p95 is the value 95% of requests come in under. An average hides the slow tail, while the p95 is where it shows up. When most pages sit in the 250 to 350 ms range at p95 and one sits at a full second, that page is where I look first.
Two cautions I learned the hard way. First, a p95 from ten samples is barely more than the slowest one or two requests, so I treat a high p95 as a reason to look and use the p50 (the median) to confirm that a page is really slower. I run a longer probe when I need the p95 itself to be steady. Second, I time http_req_waiting (time to first byte) rather than the full duration, so runs from different days and networks can be compared fairly.
Floors in every round
The most useful thing in the probe isn't the page timings themselves. It's two extra requests mixed into every round that aren't pages at all:
- A static file, a built CSS file from
/build/assets/. Serving it involves no PHP, so its time is the network and web server cost. - A bare 404, a path that doesn't exist. A 404 still boots the whole framework, so its time is the fixed cost every request pays before any of your code runs.
A few smaller choices matter as well. The order is shuffled each round, so a page doesn't always land right after a login or on a cold cache. Redirects aren't followed, so if a session expires and a page bounces to the login screen, it counts as an error rather than a fast page. And if the probe hits a rate limit, the run is marked as compromised rather than slow, because at that point it's measuring the throttle, not the site.
The floors are what make the numbers readable. Here's a baseline from a real run (routes renamed):
| Page | n | p50 (ms) | p95 (ms) |
|---|---|---|---|
/account/edit |
10 | 265 | 1030 |
/orders/:id |
22 | 324 | 540 |
/dashboard |
11 | 351 | 459 |
/orders |
11 | 271 | 363 |
(floor) 404 |
10 | 114 | 146 |
(floor) static |
11 | 59 | 65 |
Every page here pays about 60 ms of network time and about 115 ms to boot Laravel before it does anything of its own. When I compare two runs and the pages and the 404 floor have all moved by the same amount, the change is per-request: OPcache, the database connection, a monitoring agent, the server size. When only a few pages move, the cause is in those pages. The /account/edit row is also a good example of the earlier caution: its p95 comes from one or two slow samples, and its p50 is in line with the rest.
Drilling into an outlier
Once a route stands out, I find the cause by drilling down with an AI assistant. I give it the route and let it follow the request through: the controller or Filament resource, the queries it runs, the relationships it loads and what it renders. Almost every time it ends in one of two places:
- A query, often an N+1, a missing eager load, or a filter or sort on a column with no index.
- Too much time in PHP, such as building a large collection to show ten rows, or doing work on each request that could be cached.
The probe tells me where to look, and the drill-down tells me why. Neither is much use without the other.
Watching queries with the performance schema
The probe only sees pages I've put in its list. To catch slow queries wherever they come from, including queue jobs and pages I didn't think to list, I use MySQL's own statistics on the remote database.
The performance schema is enabled by default in modern MySQL. Its digest summary table groups every statement by its shape, with the literal values stripped out, and keeps running totals for each: how often it ran, how long it took and how many rows it examined compared with how many it returned. Something like this gives me a starting list:
SELECT DIGEST_TEXT,
COUNT_STAR AS calls,
ROUND(AVG_TIMER_WAIT / 1e9, 1) AS avg_ms,
ROUND(SUM_TIMER_WAIT / 1e12, 1) AS total_s,
SUM_ROWS_EXAMINED,
SUM_ROWS_SENT,
SUM_NO_INDEX_USED
FROM performance_schema.events_statements_summary_by_digest
WHERE SCHEMA_NAME = 'your_database'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 20;
- Timers are in picoseconds, hence the division.
- Sorting by total time rather than average puts the moderately slow query that runs 50,000 times a day above the very slow one that runs twice.
- Rows examined compared with rows sent is the clearest sign of a full table scan. If a query examines millions of rows to return twenty, it's either missing an index or written in a way that can't use one.
- The
sysschema has a ready-madestatements_with_full_table_scansview that answers that question directly.
These are running totals since the server started (or since the table was last truncated), so I look at them periodically and pay attention to the queries that are consistently near the top, not the ones that spiked once during a deploy or an import. Fixing them is usually unglamorous: add the index the query needed, or restructure the query so it can use one that already exists.
The gap neither of these covers
Both of these methods measure one thing at a time. The probe sends one request every ten seconds, and the digest table describes each query on its own. Neither tells you what happens when a lot of things happen at once: a burst of traffic, a scheduled job that locks a table everyone else is reading, a queue backlog competing with web requests for the same database. Those problems only appear under concurrency, and a quiet probe will happily report that everything's fine right up until it isn't.
The plugin below has a separate load-test mode with ramp and spike profiles for this, but it answers a different question ("how much can this take?") and needs its own care, including test accounts, a pool of users and an environment you're allowed to load. I think of it as a third method with its own rules, not as an extension of the probe.
Try it yourself
I've packaged the probe as a Claude Code plugin, along with the command that builds and verifies the page list and the load-test mode. It logs in to Filament panels, Breeze or Fortify forms, or a custom login you write. It keeps passwords in .env, and it compares each probe with the previous one:
/plugin marketplace add bAngerman/claude-skills
/plugin install load-testing@bangerman
The source is at github.com/bAngerman/claude-skills.
What I'd tell someone else
- Don't trust local timings for anything that touches a big table. Measure on an environment with real data, gently, and do it regularly so a slow page shows up as a change rather than a complaint.
- Put floors in your measurements. A static file and a 404 in every round tell you whether the whole app got slower or just one page did.
- Use p95 to find suspects and p50 to convict them, and don't read much into a p95 from a handful of samples.
If you'd like help finding where your own Laravel app is slow, I'm available.