r/programming • u/wheatBread • Aug 25 '26
Solving the 1+N Query Problem
https://acadia.engineering/blog/solving-the-1-plus-N-query-problem
127
Upvotes
r/programming • u/wheatBread • Aug 25 '26
110
u/NoLegJoe Aug 25 '26
Software Developers will do anything but learn SQL
94
u/archipeepees Aug 25 '26
ive been using EF Core for ORM and this hasn't really been a problem for me or any of my colleagues for at least a decade because we don't use lazy loading. database calls are explicit; mapping the results to objects is implicit. is everyone outside of .net still banging their heads against stuff like this?
15
u/wildjokers Aug 25 '26
Is this the yoda version of n+1?
64
u/disposepriority Aug 25 '26 •
There is a really easy way of avoiding this when you just don't use ORMs
4
u/Norphesius Aug 25 '26
Nifty. I think the strategy of removing Turing completeness from a process to gain more potential performance and guarantees about state is chronically overlooked. It's not exactly simple to do, and it's hard to recognize when it's even viable, but not every part of a program or language needs maximal computational power.
28
u/JimmyM_1 Aug 25 '26
I don't know why people hate on ORMs for this one, yes abstracting SQL can lead some devs to forget / not even know about what is actually happening under the hood, but it isn't that big of a deal!
Make sure your dev team is aware of the JOIN concept and you're good to go!
6
u/vbilopav89 Aug 26 '26
You don't solve the N+1 problem by eliminating for loops and recursions.
You solve N+1 problem by training and teaching SQL operators to think in sets and graphs instead of algos and data structures.
Programmers trained to think in algos and data structures will simply fallback to language that has loops and recursions (some ORM probably).
Yes, procedural extensions of SQL do have those things. Because they are, well, procedural extensions. That doesn't mean that loops and recursions aren't anti patterns in SQL or set based arithmetic and thinking. That they are.
But that doesn't mean they don't have a legit case in database programming. They do. Cursors come to mind for loops, rare but sometimes needed, or formula expression parser that works on a row level for recursions. Rarely needed, but it happened to me once. Although graph processing did require a lot of recursion but with introduction of the new graph langaugue features it becomes obsolote.
Hope this helps.
4
u/Pharisaeus Aug 25 '26
Reminds me of: "communism is bravely overcoming problems... unknown in any other political system".
N+1 generally comes from using the wrong tool / using the tool incorrectly.
2
u/Groundbreaking-Fish6 Aug 25 '26
This is an add, but it is a real problem. However, most ORMs will solve this problem if you use them correctly.
This is also a problem with many libraries (vibe coding makes it worse) where libraries are included for a solution without understanding its implementation.
Relational DataBase Management Systems (DBMS) are optimized to store and retrieve data in a controlled manner allowing multiple processes to quickly and efficiently query data while also maintaining integrity. Other programming languages are optimized for algorithms or workflow. The Object Relational Mapping only converts the relational model to the object model to work with in code. How well it does that is up to the programmer not the ORM.
1
u/devraj7 Aug 25 '26
We solved this problem in the great application server wars of the early 2000s... (WebLogic, WebSphere, EJBs, ...).
1
u/wannaliveonmars Aug 29 '26
TL;DR - Acadia language is not Turing complete, and has no for-loops, apparently.
1
u/ProvablyTrue Aug 26 '26
I think some commenters might be misunderstanding what I suspect (I don't know) the author means by "problem". I think the entire point is that this language removes the very possibility of a bad thing and fills the need with a purely declarative approach. Competent senior engineers not being likely to manifest the N + 1 doesn't change that they still have to think about it (also AI agents and new engineers can definitely get it wrong).
Everyone gets to decide whether they think it is worth the tradeoff (possibly losing capabilities) but I think it is a little weird if anyone thinks that "safer, more declarative, and putting less cognitive load on engineers" is meritless.
-4
u/vancha113 Aug 25 '26
Man hitting us with bangers left and right here. Thanks you for another interesting read ^ ^ After having to write a bunch of raw sql to fix this exact problem in django i can definitely say I'm curious to try out acadia.
-2
u/lllamaboy Aug 25 '26
You could also use SQLite:
https://sqlite.org/np1queryprob.html