News for hackers. Stuff that matters.
Re:Understanding N + 1 queries problem by Arch-TK on Monday January 02, 2023 @03:17PM (#34223073)
This is specifically just a problem with all ORMs. They attempt to solve the object-relational mismatch and fail because these two concepts (sets of tuples and graphs) are completely orthogonal.
Re:Understanding N + 1 queries problem by jbverschoor on Monday January 02, 2023 @04:01PM (#34223591)
No it's not.. you can and will easily have the same problem with SQL if you're not fetching beforehand..

I bet wordpress (or many plugins) is/are so slow exactly because of this.

ORMs will even make it easier to write more efficient queries is many cases.

Re:Understanding N + 1 queries problem by Arch-TK on Monday January 02, 2023 @04:31PM (#34223939)
If you intentionally write poor code with plain SQL you will have the same problem as the auto-generated poor code of a lot of ORMs? Yes?

I don't think Wordpress is relevant here in any way. If the code is poorly written and has N+1 queries problems then it is either poorly written or is trying to half-ass an ORM.

Lastly, I would like a lot of citations to prove to me that ORMs produce more efficient queries. What produces more efficient queries in all my experience is a good understanding of relational databases, better query planners and better database design.

Re:Understanding N + 1 queries problem by hinkley on Monday January 02, 2023 @04:19PM (#34223819)
There are too many questions to make any absolute statements. You hint at a couple here.

Specialization of requests reduces data transport, but increases testing surface area and drastically reduces caching options - including within the database. Years ago we discovered that our bottleneck in Oracle was being caused by query cache evictions (worse, in 9i only queries currently in the cache could run, so new queries were blocking in the query planner. I'm not sure if this was ever fixed.)

One of the important uses of etags within your infrastructure is to allow you to incorporate data manipulation into your caches. If your functions are deterministic you can store the results of a calculation, instead of the source data for that calculation. Cache hits then don't just save bandwidth, they also save computation, which reduces the sequential parts of the response, fighting Amdahl's Law.

Re:Understanding N + 1 queries problem by chc on Monday January 02, 2023 @05:30PM (#34224594)
This was very much a problem for WordPress back in its "glory days." I had to go in and optimize a lot of stuff to get our website's performance to be sub-5s. (I haven't worked with WordPress in years, so I can't comment on its current state, but I wouldn't be surprised if this is still a thing.)
Re:Understanding N + 1 queries problem by fabian2k on Monday January 02, 2023 @04:23PM (#34223859)
This is not a problem with all ORMs. This is caused by lazy loading the related data when it is accessed, which is not how all ORMs work. If your ORM requires you to define the included related data explicitly, you won't have this particular problem.
Re:Understanding N + 1 queries problem by Arch-TK on Monday January 02, 2023 @04:32PM (#34223957)
And an ORM which eagerly loads all related data before it is accessed will have a different but related problem. I'm actually not aware of any ORM which tries to do more than trivial guessing of when data needs to be eagerly loaded. There is no easy way of solving this problem within an ORM without either causing a different problem or without leaking the abstraction a lot and requiring lots of additional complexity to annotate what you want the ORM to actually do.
Re:Understanding N + 1 queries problem by Lapsa on Tuesday January 03, 2023 @02:33AM (#34228859)
real fun begins when random Cartesian products shits up all your RAM
Re:Understanding N + 1 queries problem by fabian2k on Tuesday January 03, 2023 @02:45AM (#34228913)
In EF Core for example you have to specify which relations to include, either using ".Include()" or transforming the entire query with a "Select()". In the latter case this works pretty much automatically as long as you stay within the kinds of expressions EF Core can translate to SQL.

There is a different performance problem lurking with this design though. You have to decide whether to tell EF Core to do a query per relation or everything in one query. The single query is very efficient for many simple cases and ensures consistency, but it can fail spectacularly if you have a very large number of relations. This is a pretty significant footgun, but if you really don't want to be bothered with this you can just always use split queries.

ORMs can't really abstract over all performance considerations. They are convenient, but they can't fully hide what the database is doing under the hood.

Re:Understanding N + 1 queries problem by imtringued on Tuesday January 03, 2023 @05:14AM (#34229809)
You just specify which relations you want to load in your query. It is that simple.

There is no gotcha here.

Re:Understanding N + 1 queries problem by guggle on Tuesday January 03, 2023 @05:44AM (#34229964)
I don't know why this comment is downvoted. That's how Django ORM works too (select_related)
Re:Understanding N + 1 queries problem by Izkata on Tuesday January 03, 2023 @09:26AM (#34231693)
The abstraction leakage in Django is to know whether you should use "select_related" or "prefetch_related".
Re:Understanding N + 1 queries problem by hgsgm on Monday January 02, 2023 @04:37PM (#34224014)
Tuples are Objects Graphs are Relations.

ORM (or GTM if you like) helps you navigate your Graph of Tuples.

Re:Understanding N + 1 queries problem by Arch-TK on Monday January 02, 2023 @08:53PM (#34226537)
Sets of tuples and graphs of objects are fundamentally incompatible and I have not found a single ORM which didn't completely fail to pave over this fundamental incompatibility.

The object-relational impedance mismatch is not just something I came up with to shit on ORM. It is a well understood and widely known phenomenon which ORM users often like to insist on pretending doesn't exist. To claim that sets of tuples are the same as object graphs is like claiming that you have solved the halting problem.

Feel free to claim that the impedance mismatch is small enough that a sufficiently advanced ORM might successfully abstract it away, I don't think this claim is defensible either but on the other hand claiming that the OR impedance mismatch doesn't exist makes me think you have no idea what problem an ORM actually solves. It certainly makes me wonder how you would justify the size and complexity of every ORM out there.

Re:Understanding N + 1 queries problem by 8note on Tuesday January 03, 2023 @03:08AM (#34229066)
Isn't an adjacency list a set of 2-ples?

That's a set of tuples that defines a graph

Re:Understanding N + 1 queries problem by Arch-TK on Tuesday January 03, 2023 @05:24PM (#34239007)
Try re-creating a relational database in a graph database or vice versa by applying this approach and report back. I promise you that if you put a graph in a SQL database or vice versa you will end up with absolutely abysmal performance unless you explicitly go out of your way to special case everything into being performant.
Re:Understanding N + 1 queries problem by nightski on Tuesday January 03, 2023 @09:13AM (#34231516)
The problem isn't the mismatch of the data structures. Like at all. Imo it comes down to lazy loading and that's about it. Using lazy loading to navigate relationships is where the impedance mismatch manifests and it is completely avoidable (basically a do at your own risk, danger ahead type of scenario).
Re:Understanding N + 1 queries problem by Arch-TK on Tuesday January 03, 2023 @05:22PM (#34238989)
How is it avoidable? It's a necessary part of using an ORM. Moreover, you're severely understating the complexity of dealing with how to load data through an ORM and how to control the underlying queries.

SQL is already a perfectly adequate language for describing how to query relational data, by putting a effectively a graph query language in front of it and adding a translation layer you immediately need an immense amount of special casing, the common cases might be handled by a sufficiently sophisticated ORM but once you veer even slightly outside the realm of a basic hello world application which accesses a database you are immediately hit with a wave of all the complexity which results from the abstraction leaking. Every time I've personally dealt with an ORM or seen code which deals with an ORM, the amount of unnecessary hassle which is required just to get the ORM to do what you want it to do certainly outweighs any imagined benefit of using it. If you think SQL is hard to write then you're putting the blinders on to claim that ORMs are easier to deal with.

If you have never hit upon these sorts of issues then you must have never actually needed to do anything non-trivial and performant with an ORM.

Re:Understanding N + 1 queries problem by nightski on Tuesday January 03, 2023 @06:32PM (#34239687)
I primarily use EF, although I have used many others in the past. You are right that not all ORMs are equal, not even close.

But with EF at least those problems aren't really a big deal. You are right that SQL is perfectly adequate - great actually. I generally think in terms of SQL before loading data with a Linq query.

Often I'll do a query that projects to a model/dto. Or if I do need to load the entity and it's relations, I'll craft it just like I would in SQL. You can load the desired related entities first and then when you load the main entity they get auto populated since they are in the context. It's easy to build exactly what you need this way.

So why use an ORM? Well for one, on commands if I load an entity I get change detection for free. I get bulk inserts/updates/deletes for free (Using ef extensions). I get strongly typed queries for free. I'm able to find all references to a particular table or column across my entire code base with a single click. I can build up queries dynamically via linq. There's a lot of huge benefits that save a lot of time.

Re:Understanding N + 1 queries problem by taffer on Tuesday January 03, 2023 @04:58AM (#34229730)
The problem lies in the word navigate. The code in the article navigates through a graph. There is simply no way to navigate graphs in an eagerly evaluated language without running into the N+1 problem. A declarative language that can be lazily evaluated by the DBMS is the only way to efficiently query a graph.
Re:Understanding N + 1 queries problem by avereveard on Tuesday January 03, 2023 @05:52AM (#34230003)
proper orm allow you to specify how to process relationships, best one on a per query basis and not on a per model basis. then they aggregate the table resulting from the join presenting a deduplicated view of the root object and all the related one fetched.
Re:Understanding N + 1 queries problem by Arch-TK on Tuesday January 03, 2023 @05:26PM (#34239028)
This is a major abstraction leak and is very rarely as simple as you describe it. The only time I've seen it be this simple is with people who design their databases entirely around an ORM and in that case they're already leaving a large amount of performance on the table which you can no longer recover by reaching behind the scenes to help the ORM figure out what it needs to do.