EMZETT.
Login

N+1 problem

In short: The N+1 problem occurs when a program runs one query for a list and then another query for each of the N results. One query turns into N+1.

In more detail: An example: load 100 posts (1 query) and then fetch each post’s author individually (100 queries). Each query is fast on its own, but together they add up to a lot of network and waiting time.

In Depth

  • Spotting it: The log shows many nearly identical queries in a row. Query counters or tracing tools make it visible.
  • Fix 1: A JOIN that fetches everything in one query.
  • Fix 2: Two queries: first the list, then all authors with WHERE id IN (…), combined in code.
  • ORMs: Many offer “eager loading” (include, with) to control this.
  • Measure: Fewer queries are not always faster. Measure instead of guessing.

See also: Connection pooling