- Home
- Skills
- Data & Databases
- Fix N+1 Queries in Laravel, Measured Before and After
Fix N+1 Queries in Laravel, Measured Before and After
Your coding agent finds the N+1 queries and slow Eloquent calls in your Laravel app, fixes them one small change at a time, and shows you the numbers before and after each fix.
Free
Works with the AI Agents you already use
Fix N+1 Queries in Laravel, Measured Before and After
Example session with this skill installed
Our /orders page takes 3 seconds to load. I think it's N+1. Can you find what's slow and fix it?
- Read your context and instructions
- Compiled the fix n+1 queries
Summary (example run). GET /orders ran 214 queries. Two N+1 problems and a missing index. After the fixes: 5 queries.
Before and after
| Endpoint | Queries | SQL ms | Page ms | Memory |
|---|---|---|---|---|
| GET /orders before | 214 | 1,240 | 3,050 | 46 MB |
| GET /orders after | 5 | 38 | 260 | 14 MB |
What I changed
customerwas loaded once per row. Addedwith('customer:id,name')inOrderController@index.$order->items()->sum('price')ran in the view for each order. Replaced withwithSum('items', 'price')and readitems_sum_price.EXPLAINshowed a full scan onordersfiltered bystatusand sorted bycreated_at. Added a composite index(status, created_at).EXPLAINafter: index used, rows read went from 48,000 to 50.
$orders = Order::with('customer:id,name')
->withSum('items', 'price')
->where('status', $status)
->latest()
->paginate(25);
Locked with a test
DB::enableQueryLog();
$this->get('/orders')->assertOk();
expect(count(DB::getQueryLog()))->toBeLessThan(10);
Next steps
orders.indexis also rendered by the export controller. Samewith()needed there.- Run the migration on staging and check write speed on
orders.
Connects securely to your tools. The creator never sees your data.
About this skill
Your Laravel app got slow. A list page runs hundreds of queries, a job runs out of memory, an endpoint times out. You know it's probably N+1, but finding every one by hand takes a day.
This skill gives your agent a clear method, and one rule: no fix without a number before and a number after.
What it does
- Measures the slow page, job or command first: query count, SQL time, page time, memory.
- Turns on
preventLazyLoadingto catch lazy loads, then greps for the ones it misses, like->comments()->count()inside a loop or an accessor that runs a query. - Fixes with the smallest change first: eager loading,
withCountandwithSum, constrained eager loads, indexes checked withEXPLAIN,chunkByIdandlazyByIdfor big tables, caching last. - Checks your Laravel version before using a recipe, so you don't get Laravel 11 code on a Laravel 10 app.
- Measures again and adds a test that fails if the query count comes back.
What you get A before and after table per endpoint, the code changes, and a test that holds the gain. On a Laravel 13 test app seeded with a 3-level N+1 (200 posts, 1,000 comments): 1,404 queries down to 8.
What it doesn't do
It won't add an index without EXPLAIN, won't hide an N+1 behind a cache or a global auto eager load switch, and won't make up numbers. With no database to run on, it does a static audit and marks every number as estimated.
Requires Laravel 10+. Per-parent eager load limits and Cache::flexible require Laravel 11+. Cannot optimize non-SQL drivers. Measurement requires a seeded database or production copy.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Free forever
No account required to browse
Trust & safety
Security scan
Security scan passed 3 days ago
- Free with an Agensi account
Requires
Permissions
File Scopes