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

SQL optimization when Including collection and results are filtered by parent PK #2182

Closed
rowanmiller opened this issue May 13, 2015 · 3 comments
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed.

Comments

@rowanmiller
Copy link
Contributor

Given the following query...

            var order = db.Orders
                .Include(o => o.Lines)
                .SingleOrDefault(o => o.OrderId == orderId);

We generate the following SQL statements. The join to Order could be eliminated in the second query because it filters by the same column that is being used to join (i.e. we could filter by the FK rather than the PK).

SELECT TOP(2) [o].[CheckoutBegan], [o].[OrderId], [o].[OrderPlaced], [o].[State], [o].[Total], [o].[Username]
FROM [Order] AS [o]
WHERE [o].[OrderId] = @__orderId_0
ORDER BY [o].[OrderId]

SELECT [o].[OrderId], [o].[PricePerUnit], [o].[ProductId], [o].[Quantity]
FROM [OrderLine] AS [o]
INNER JOIN (
    SELECT DISTINCT TOP(2) [o].[OrderId]
    FROM [Order] AS [o]
    WHERE [o].[OrderId] = @__orderId_0
) AS [o0] ON [o].[OrderId] = [o0].[OrderId]
ORDER BY [o0].[OrderId]
@rowanmiller rowanmiller added this to the 7.0.0 milestone May 15, 2015
@rowanmiller rowanmiller removed the pri1 label May 15, 2015
@rowanmiller rowanmiller modified the milestones: Backlog, 7.0.0 May 15, 2015
@rowanmiller rowanmiller added the help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. label May 15, 2015
@smitpatel
Copy link
Contributor

@anpete

@anpete
Copy link
Contributor

anpete commented Aug 15, 2017

@smitpatel Not done.

@divega divega added good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. and removed help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. good first issue This issue should be relatively straightforward to fix. labels May 31, 2019
@Muppets
Copy link
Contributor

Muppets commented Jun 11, 2019

I'm not sure if this is still an issue. Testing the same test as above:

            using (var context = CreateContext())
            {
                var order = context.Customers
                    .Include(c => c.Orders)
                    .SingleOrDefault(o => o.CustomerID == "1");
            }

Now produces a single SQL call:

SELECT [t].[CustomerID], [t].[Address], [t].[City], [t].[CompanyName], [t].[ContactName], [t].[ContactTitle], [t].[Country], [t].[Fax], [t].[Phone], [t].[PostalCode], [t].[Region], [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM (
    SELECT TOP(2) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
    FROM [Customers] AS [c]
    WHERE [c].[CustomerID] = N'1'
    ORDER BY [c].[CustomerID]
) AS [t]
LEFT JOIN [Orders] AS [o] ON [t].[CustomerID] = [o].[CustomerID]
ORDER BY [t].[CustomerID

Feels like we can close this now unless I've missed something?

@smitpatel smitpatel added closed-not-needed and removed help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-enhancement labels Jun 11, 2019
@smitpatel smitpatel removed this from the Backlog milestone Jun 11, 2019
@ajcvickers ajcvickers added the closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. label Mar 10, 2022
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed.
Projects
None yet
Development

No branches or pull requests

6 participants