Skip to content

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

Closed as not planned

Description

@rowanmiller

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]

Activity

  1. added this to the 7.0.0 milestone on May 15, 2015
  2. modified the milestones: Backlog, 7.0.0 on May 15, 2015
  3. added
    help wanted
    on May 15, 2015
  4. smitpatel commented on Aug 15, 2017

    @smitpatel
    Contributor
  5. anpete commented on Aug 15, 2017

    @anpete
    Contributor

    @smitpatel Not done.

  6. added
    good first issue
    help wanted
    and removed
    help wanted
    good first issue
    on May 31, 2019
  7. Muppets commented on Jun 11, 2019

    @Muppets
    Contributor

    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?

  8. added and removed
    help wanted
    on Jun 11, 2019
  9. removed this from the Backlog milestone on Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions