N + 1 Query Problem
What does the name mean?
It actually means the positive. It means, for every 1 query, we do an additional N queries which can be avoided.
N + 1 in API
query is a problem that's not just database specific. Even for simple REST APIs, one microservice call can trigger further N calls before it returns the final response.
N + 1 in Databases
In databases, is pretty common since we can load the parent table first and then for every row in the parent table, we can load data from child tables to build the full data set.
Lazy vs Eager loading
- Lazy loading is what exactly leads to query problem.
- Eager loading avoids this problem. ORM libraries usually issue JOIN statements so that just one query is sent out and we prepare the objects accordingly using the single query response.