Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF7 fires several queries instead of 1 query with smart join -- and worse #4520

Closed
jancg opened this issue Feb 9, 2016 · 5 comments
Closed

Comments

@jancg
Copy link

jancg commented Feb 9, 2016

HAS BEEN SOLVED IN 1.0.0

var enrichedProducts = this.productContext.Products
  .AsNoTracking()
  .Where(p => productNumbers.Contains(p.SAPProductNumber))
  .Select(p => new
    {
      SAPProductNumber = p.SAPProductNumber,
      DisplayName = p.DisplayName,
      Brand = p.Brand,
      Type = p.Type,
      MultimediaValueEpochTime = p.ProductMultimedias
        .Where(pm => pm.Visible == true
                  && pm.ValueEpochTime != null)
        .OrderByDescending(pm => pm.MainItem == true)
        .ThenBy(pm => pm.SortOrder)
        .Select(pm => pm.ValueEpochTime)
        .FirstOrDefault()
    })
  .ToList();

productNumbers is a List and contains 8 product numbers.

EF7 fires 9 queries to SQL Server. The first query actually translates neatly to IN (...8 numbers...). Next EF7 fires 8 identical queries to SQL Server that aren't even correlated to the 8 numbers. 8 times tens of thousands of records from the ProductMultimedias table are fetched!

@maumar
Copy link
Contributor

maumar commented Feb 11, 2016

This happens because of

MultimediaValueEpochTime = p.ProductMultimedias
        .Where(pm => pm.Visible == true
                  && pm.ValueEpochTime != null)
        .OrderByDescending(pm => pm.MainItem == true)
        .ThenBy(pm => pm.SortOrder)
        .Select(pm => pm.ValueEpochTime)
        .FirstOrDefault()

part in the projection. EF currently can't handle collection projections well. Queries you see are not correlated with the main query because that correlation is happening on the client currently. You can try using Include on that navigation, projecting the entire collection and then performing the rest of the query on the client. It will still fetch all the rows from ProductMultimedias but should do so only once, and not 8 times.

@maumar
Copy link
Contributor

maumar commented Feb 11, 2016

Something like this:

var enrichedProducts = this.productContext.Products
  .Include(p => p.ProductMultimedias)
  .AsNoTracking()
  .Where(p => productNumbers.Contains(p.SAPProductNumber))
  .ToList()
  .Select(p => new
    {
      SAPProductNumber = p.SAPProductNumber,
      DisplayName = p.DisplayName,
      Brand = p.Brand,
      Type = p.Type,
      MultimediaValueEpochTime = p.ProductMultimedias
        .Where(pm => pm.Visible == true
                  && pm.ValueEpochTime != null)
        .OrderByDescending(pm => pm.MainItem == true)
        .ThenBy(pm => pm.SortOrder)
        .Select(pm => pm.ValueEpochTime)
        .FirstOrDefault()
    });

@rowanmiller
Copy link
Contributor

Improving this translation is already tracked by #4007, so I'll close this one out.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@jancg
Copy link
Author

jancg commented Oct 16, 2022

'not planned'? Why? We always intent to fix bugs in our code. I guess I should stick to EF6 forever? EF Core is still not up to par compared to EF6 when it comes down to query generation.

@ajcvickers
Copy link
Contributor

ajcvickers commented Oct 16, 2022

@jancg This is a really old issue that was closed 6 years ago. All that is happening here is that the GitHub closed-reason is changing. If you think there is still a bug here, then please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants