N+1 problem

Running one extra database query per row in a loop, which makes pages slow as data grows.

Also called: eager loading, with(), lazy loading (database)

Source
General
Languages
PHP, SQL

In plain English

Loading 50 bookings and then each booking's villa separately is 51 queries. Eager loading with with() turns it into 2.

Example

I fixed an N+1 on the listings page: 203 queries down to 3.

In code

$bookings = Booking::with('villa')->latest()->take(50)->get();

Learn more

Related terms

Added 2026-11-30 · Last reviewed 2026-11-30