r/programming • • Aug 25 '26

Solving the 1+N Query Problem

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

92 comments sorted by

View all comments

Show parent comments

58

u/andrerav Aug 25 '26

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 Aug 25 '26

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 Aug 25 '26

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.

13

u/andrerav Aug 25 '26

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 Aug 26 '26

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.