question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

One-to-many projection creates many subqueries

See original GitHub issue

First of all - this tool practically saved my life when migrating from EF6 to EF Core, great job, everything works better than EF Core, instead of one thing

LinqToDB.Common.Configuration.Linq.AllowMultipleQuery = true;

var querySample = db.TicketNumbers.Where(p => p.Ticket.Name == "Lacna").Select(p => new
{
    Id = p.Id,
    TicketName = p.Ticket.Name,
    CustomFields = p.CustomFieldValues.Select(cf => new
    {
        Name = cf.CustomFieldDefinition.Name,
        Value = cf.Value
    }).ToList()
}).ToLinqToDB().ToList();

This guery yields back 300 results, from the logging it seems that Linq2Db core runs the subquery for each of the obtained item. [= 300 subqueries, 300 roundtrips to the DB]. For our scenario, for example, this creates an issue as our SQL Server is physically located elsewhere than the calling app creating a huge performance gap due to the latency, but I believe this might be an issue for other scenarios too (running 301 queries when in fact only 2 are needed)

From observing the logs EF Core handles this differently - running a single subquery on the very same condition as main “root” query, effectively creating only one more roundtrip to the DB, then linking the data based on their foreign-key declaration.

To sum it up, I have 3 questions:

  • Is there anything I can do to change the one-to-many behavior?
  • Are there any plans to change the subdata loading routine?
  • If I wanted to hack a solution for our needs by changing the Linq2Db core, could you advise me where should I look?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
sdanylivcommented, Jun 9, 2019

@brunolau, good news there. I had created first Eager Loading prototype using current sources and It looks very promising, later will point to PR which introduces this feature. A lot of work here but i hope in one or two weeks it will be done.

1reaction
sdanylivcommented, Mar 27, 2020

I hope tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

one-to-many projected LINQ query executes repeatedly
The Crux of the Issue: in the 2nd LINQ example I implement IQueriable statements and do not call ToList() function and for some...
Read more >
How to write EXISTS subqueries with JPA and Hibernate
Introduction. In this article, I'm going to show you how to write EXISTS subqueries with JPA and Hibernate.
Read more >
Why, When and How to Use DTO Projections with JPA ...
DTO projections are the most efficient ones for read operations. Let me show you how to use them in JPQL, Criteria and native...
Read more >
How to fetch a one-to-many DTO projection with JPA and ...
In this article, I'm going to show you how you can fetch a one-to-many relationship as a DTO projection when using JPA and...
Read more >
Hibernate Tip: How to Use a SubQuery in a CriteriaQuery
I use a simple model consisting of an Author and a Book entity and a many-to-many association between them. In the following code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found