Re:Understanding N + 1 queries problem
by henning on Monday January 02, 2023 @02:58PM
(#34222841)
A great example of how "modern software" creates as many problems as it solves. ORMs are horrible, worse-than-useless pieces of shit and Active Record is probably the worst part of Rails.
[ Parent
| on HN ]
You're dismissing ORMs because of one (well-understood and easy to mitigate) downside without considering the upsides.
ORM do make development much easier and are fine in the majority of cases to begin with. For the edge-cases, some can be addressed by giving the ORM extra hints about your intentions (such as for the N+1 queries problem) and those that can't can be rewritten as raw SQL.
Why dismiss them entirely in favour of Raw SQL if you can get the best of both worlds by picking the appropriate tool for the job?
If you simply don't attempt to shoehorn your data into a graph structure then you don't need an ORM in the first place. ORMs only make development easier insofar as you insist on turning your relational data into graphs. In all instances I've found that raw SQL or a good query builder (not an ORM, e.g. SQLAlchemy core) and an approach to handling the data as it is (relational) and not as you might want to pretend it to be (graph) is sufficient to ensure the resulting software is easier to develop and maintain.
In situations where I have needed graph data I have found graph databases to work well there too.
How is the example given in the post an "edge" case? All except the simplest domain models include these kind of relationships (and more complex ones). The Post->Comment->Vote pattern is practically everywhere as soon as you are modeling real-world scenarios.
Re:Understanding N + 1 queries problem
by isbvhodnvemrwvn on Tuesday January 03, 2023 @05:20AM
(#34229841)
Solving those issues takes you SIGNIFICANTLY less time than mapping SQL results in any moderately complex relation graph.
SQLAlchemy looks at you and wonders what you’re talking about. ORMs can be pretty amazing, but like any tool, you have to know how to use them. If you want to use an ORM (or straight SQL!) to shoot your own foot, nothing’s stopping you.