Start free trial
Start free trial Log in

14-day free trial. No credit card.

What is an N+1 Query Problem?

A common reason a Laravel page is slow, and how to catch it before your users do.

  • Glossary
  • 3 minute read

What is an N+1 query?

An N+1 query is a performance anti-pattern where loading a list of N parent records triggers N additional queries (one per parent) to fetch a related record for each. Instead of 2 efficient queries (parents + batched children), you fire 1 + N queries, producing linear performance degradation as the list grows.

Updated

The N+1 is one of the easiest performance bugs to ship without noticing. The code looks innocent: a loop over some models, reaching for a relationship inside it. Eloquent makes that line so natural to write that you rarely think about the query it fires. Multiply it by the number of rows on the page and a fast endpoint quietly becomes a slow one.

Why the name?

The "1" is the initial query that returns a list of N rows. The "N" is the N follow-up queries (one per row) to load related data. For 50 posts loaded with their author, that's 51 total queries instead of the 2 you actually need.

The classic Laravel example

N+1: fires 1 + 50 = 51 queries
// 1 query: SELECT * FROM posts ORDER BY ... LIMIT 50
$posts = Post::latest()->take(50)->get();

foreach ($posts as $post) {
    // 50 queries: SELECT * FROM users WHERE id = ?
    echo $post->author->name;
}
Fixed: fires 2 queries
// 2 queries: 1 for posts, 1 batched for all authors
$posts = Post::with('author')->latest()->take(50)->get();

foreach ($posts as $post) {
    echo $post->author->name; // already in memory
}

Why it matters

Every query has network and protocol overhead, roughly 1-5ms when the database is on another host. The queries themselves might be fast, but 50 round-trips stack up: a page that should run in 50ms runs in about 100-300ms. Scale that to 500 items and the round trips alone add 0.5 to 2.5 seconds.

Because the extra cost grows with the list, fixing an N+1 pays off most on the pages with the longest lists.

Where they come from

Almost always a relationship accessed inside a loop, but the loop isn't always where you'd look. A few places they hide:

  • In Blade. @foreach ($posts as $post) {{ $post->author->name }} is an N+1 even though there's no visible query in your controller: the relationship resolves lazily when the view renders.
  • In API resources. A PostResource that returns $this->author->name fires one query per item in the collection, and the loop lives inside Laravel's serialization where you never see it.
  • After a refactor. Someone adds with('author') when they write the endpoint. A later change adds a second relationship ($post->comments) and only the first stays eager-loaded. It passed review and degrades in production as data grows. That's the case worth monitoring for, because code review already missed it once.

How to detect N+1 in production

Model::preventLazyLoading() catches them in development. In production you need an APM that groups repeated query fingerprints per request. The signal is "the same normalized SQL fired 47 times in one request."

How NightOwl shows it

Find the N+1 queries in your own app

Open a slow request in NightOwl and its timeline gives identical SQL the same colour. The Duplicates tab lists only the repeated queries, so a relationship loaded in a loop stands out right away.

No credit card needed.

Frequently asked questions

Anything else, write to support@usenightowl.com

Why is it called N+1?

The '1' is the initial query that returns a list of N parent records. The 'N' is the N additional queries (one per parent) to load a related record for each. Total: 1 + N. For a list of 50 items, that's 51 queries instead of the 2 you actually need.

Is an N+1 always a bug?

Practically yes, though the severity varies. In a 5-item list it's a small waste. In a 500-item list at 1-5ms per round trip, it adds roughly 0.5 to 2.5 seconds to a page that should take 50ms. For small indexed lookups the round trip costs more than the query itself. Each trip is cheap; 500 trips in a loop are not.

How much slower is an N+1 query?

Each database round-trip costs roughly 1-5ms of network/protocol overhead on top of execution when the database is on another host. A 100-item list with an N+1 adds 100-500ms of latency, even if the queries themselves are fast.

Does eager loading always fix N+1 queries?

It fixes N+1s caused by relationships, which is most of them. A query you write yourself inside a loop (a where() per row) needs rewriting as one whereIn() query instead. For relationships, choose the right variant: with() on the query, load() after the fact, loadMissing() to load only relations that aren't loaded yet. Polymorphic morphTo relations eager load with a plain with(); add morphWith() when each morph type needs its own nested relations. For column-selection use with('author:id,name'). For nested relations use dotted syntax: with('posts.comments').

Keep reading

Flat pricing, whatever your traffic does

Every plan starts with a 14-day free trial. No credit card needed.

Hobby
$5 a month

One app for a side project. Issues and alerts, with a 14-day dashboard window.

Start my 14-day trial
TeamRecommended
$15 a month

Up to 3 apps and 5 people per team. Full history, the MCP server, data clearing and agent health.

Start my 14-day trial
Agency
$69 a month

Unlimited client apps and people, one overview of every app, and apps you can hand over.

Start my 14-day trial