r/programming 8d ago

Solving the 1+N Query Problem

https://acadia.engineering/blog/solving-the-1-plus-N-query-problem
124 Upvotes

94 comments sorted by

View all comments

59

u/disposepriority 8d ago

There is a really easy way of avoiding this when you just don't use ORMs

4

u/devraj7 7d ago

But it requires expertise in SQL.

If you use an ORM, that problem is solved automatically without you having to be a SQL expert, which is one of the points of ORMs.

WebLogic had resolved the N+1 problem in the early 2000s.

9

u/read_at_own_risk 7d ago

So instead of learning how to define your schema in SQL and how to use joins and indexes, you learn how to define your schema in code that gets mapped to SQL, and about eager/lazy loading (implying joins), how to use indexes (they're still there), how to bypass the ORM to run SQL directly, how to invalidate your cache after you've done that, how to rehidrate a resultset that has a composite candidate key (lol, no, you can't), how to get to the SQL the ORM generates so that you can diagnose performance issues, how to manipulate the ORM into generating the SQL you want...

ORMs make the easy parts easy and the hard parts much harder.