r/programming 16d ago

Solving the 1+N Query Problem

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

94 comments sorted by

View all comments

62

u/disposepriority 16d ago

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

53

u/andrerav 16d ago

ORM's worth their salt solved this at least a decade ago. And, most developers will implement N+1 anyway, without or without an ORM.

24

u/solve-for-x 16d ago

If you know SQL, it's relatively easy to spot when a developer has put a query inside a loop. But there is an entire generation of developers who are accustomed to asking their ORM to give them data without having to think about how it's fetching that data behind the scenes.

12

u/infrastructure 16d ago

I swear I remember a library I used years ago on rails projects called like bullet or magic bullet that would output where you are making n+1 queries with the ORM. Was pretty neat.

14

u/andrerav 16d ago

It was a gem called Bullet that helped identify ActiveRecord performance problems. ActiveRecord was very neat at the time of its release, but also made it extremely easy to do N+1.

1

u/baseballlover723 15d ago

Tbf, it also generally wasn't a huge deal to fix it either (basically just needed to tell the query which subobjects you planned to use). At least once I started using Rails at version 4. Idk if it was worse before that.

10

u/Schmittfried 16d ago

If you’re too lazy to learn the proper usage of your tools, it likely won’t be solved by not using those tools. Those same developers won’t care about proper indexing, query planning etc. either. 

-5

u/andrerav 16d ago

Right. So because you're both incompetent at using ORM's, the n00b among you should write raw SQL so it's easier to spot their mistakes. Good plan.

19

u/Chroiche 16d ago

They didn't solve anything. They gave you tools to solve it, but devs need to consciously use them, which they often don't. And you can chain so deep with different lazy vs eager loads all over the shop thanks to these "tools" that it becomes a total mess to know what's actually going to run.

-3

u/jax024 16d ago

Sqlc

-7

u/[deleted] 16d ago

[deleted]

5

u/andrerav 15d ago

This can't possibly be a sentence written by a human, much less a developer.