liveBook · Manning

N + 1 query problem

As explained in Troubleshooting Java, Second Edition

The N + 1 query problem is an access pattern in which one initial query is followed by a separate query for each of N retrieved records.

The N + 1 query problem occurs when an initial query retrieves N records and the application then issues one additional query for each record. The resulting N + 1 queries can create significant latency. section 6.3.2

Investigate the pattern by asking:

  • How many times does each SQL query execute? section 6.3.2
  • Does a framework fetch related table data in one composed query or issue a query per retrieved record? section 6.3.2
  • Can a profiler reveal execution time and query counts more clearly than logs or a debugger? section 6.3.2
Figure 6.15 Even when working with a framework, the profiler can still intercept the SQL queries. This makes your investigation a lot easier because you can’t copy the query directly from the code like when using JDBC and native queries.

Use profiler interception to inspect SQL generated behind a persistence framework, including execution time and execution count. section 6.3.2

How Troubleshooting Java, Second Edition covers it

How deep it goes
Defines the access pattern and explains profiler-based detection, but stops short of prescribing a specific code fix.
Examples
Uses multi-table framework fetching and profiler query counts to expose one initial query followed by N additional queries.
What you can do after
Diagnose/debug with a profiler by locating repeated queries, execution counts, and latency.
How it connects
Connects persistence frameworks with SQL queries, profilers, latency, logs, and debuggers.

Also explained in

Compare how 4 books explain N + 1 query problem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java, Second Edition. The section links point to the parts of the book it draws on.