N+1 Query Problem

Noun · Development

Definitions

  1. A performance anti-pattern where code executes 1 query to fetch a list of N items, then N additional queries to fetch related data for each item. Common with ORMs that use lazy loading. Fixed with eager loading (JOIN), batch loading, or dataloaders.

    In plain English: Accidentally making hundreds of database queries when one or two would do, usually because of how an ORM loads related data.

    Example: "The page was making 201 queries — 1 for the 200 users and 1 each for their profile photos. Classic N+1 problem."

Related Terms