objection.js: Eager loading doesn't use joins?
I’m finding eager loading works via a second query with an IN clause, is there some reason joins are not used?
This prevents me from e.g aggregating an eagerly loaded value like so
Product.
query().
eager('reviews')
avg('reviews.rating').
as('avgRating').
groupBy('column1', 'column2', ...)
Thanks
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 20 (1 by maintainers)
Finally sober 😉
To make the eager stuff use joins while keeping the current features we would basically have to rewrite objection. As an example, let’s consider this simple eager query:
We would need to generate a query like this:
And assuming that each relation yields just 2 rows the result object would be something like this:
Now don’t care about the errors in the SQL or the actual result column values, I just wrote this down without testing it.
Two main points to notice here:
This example only had a simple eager expression with tables that had only two columns and all relations had just 2 results. And even with this tiny example the result object is big. Now try to create this example in your minds with eager expression like
[a.b.c.d.e.[f, g.h], i.j, l.m.n, o.[p, q, r]]
with realistic tables that have 6 or more columns and with result sets with hundreds of rows. This is not a far fetched example, I write code like this all the time.The problems:
jsonSchema
required or add some other mechanism for finding out the columns..filterEager('a.b.d', builder => builder.where('p1', 10))
We would need to map the propertyp1
intoA_a_c_d.p1
behind the scenes and because of the lack of reflection features in knex, it is not easily doable.Conclusions
So replacing the current query system with joins is just not going to happen. We can however consider adding another mechanism for building nested joins. Or possibly with a lot of work, we could provide an option to choose which method to use for the eager queries, but really this would mean rewriting everything.
I don’t think that the performance benefits are that big. Please send any benchmarks you have that show that using separate queries is significantly slower than using joins for the common cases.
This seems to interest people even though it is closed so I’m reopening it. I’m drunk right now so I’ll answer this later 😀
I’m going to add join based eager queries as an option to the current
where in
queries. The loading algorithm can be selected per-query, per-model and per-project. I’ll keep you posted.I’ll soon release the 0.5.0-alpha.0 version that has the new
joinRelation
method. You can install it usingnpm install objection@next