DEV Community

Discussion on: How the N+1 query problem can burn your database

Collapse
 
volker_schukai profile image
Volker Schukai

Interesting article. ORMs often tempt you not to deal with the queries.

Collapse
 
aarone4 profile image
Aaron Reese

Agreed.
Naieve implementation of ORM pattern will say: GET Customers_Collection.
For each Customer in Customers_Collection, Append, GET Orders_for_customer (Customer.Customer_ID)
If you have 100 customers, that is 1 call to get the customer list (1) and 100 calls to get the orders (N), hence (N+1).
Developers tend to work with very small datasets on fast machines with local data stores so the performance issue is often not seen until regression testing or possibly UAT testing where the environment is much closer to what you would see in production. Worse, for fledgling apps then there are only 100 customers in the entire system the N+1 issue may not be apparrent, however once you are up to 10M customers, it is going to hurt!