Skip to content

N+1 query

In an N+1 pattern, a list query is followed by another fetch for each result. The structure matters, not merely a high call count.

Also known asN+1 problem
Software Development

An N+1 query occurs when an application first loads a list and then performs another query for each entry. The number of accesses therefore grows with the number of entries.

A small calculation

In a hypothetical portal, a list of 10 orders is followed by individual fetches of their delivery addresses. Together these are 11 database calls. Depending on the need, related data could also be loaded together within appropriate limits.

Many queries alone do not prove this pattern. The relationship to iteration over the results must be visible. Nor is an arbitrarily large combined query automatically better. Required data volume, permissions and actual execution cost remain part of the investigation.

Sources

Articles on this topic

Software Development with ASES

Custom software, web and mobile apps