Explain the 'N+1 Query Problem' from a database perspective. Why does it happen, and how can it be mitigated?
Last updated
N+1 means one query fetches parent rows, then one extra query runs per row for related data, usually from ORM lazy loading inside a loop.
The cost comes from round-trip latency: many tiny queries are far slower than one join.
Mitigate with eager loading (
JOINor ORMincludes) or batch loading viaWHERE id IN (...), loading only associations you actually use.
- https://shiftasia.com/community/n-1-queries/
- https://www.manageengine.com/it-operations-management/application-observability/fix-nplus1-query-problem.html
- https://medium.com/databases-in-simple-words/the-n-1-database-query-problem-a-simple-explanation-and-solutions-ef11751aef8a
- https://woodruff.dev/blog/the-n1-query-problem-in-ef-core/
- https://medium.com/@vasu_ghanta/solving-the-n-1-query-problem-a-developers-guide-to-database-performance-d01ece91245f
- https://milanjovanovic.tech/blog/n-plus-one-query-ef-core
- https://dev.to/namit2111/what-is-the-n1-query-problem-and-how-to-solve-it-1amg
- https://www.explainthis.io/en/swe/n-plus-one-problem