efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Include and Take generate incorrect SQL without Skip

Open SimonTouchtech opened this issue 11 years ago • 5 comments

Version: Beta 8 Model: EFGetStarted.Blog DB: SQLite

Code:

db.Blogs.Include(x => x.Posts).OrderBy(x => x.Url).Take(10).ToList(); 

Note: The OrderBy call is not necessary to repro, but more clearly shows the problem (that not all posts are included)

SQL:

SELECT "x"."BlogId", "x"."Url"
FROM "Blog" AS "x"
ORDER BY "x"."Url", "x"."BlogId"
LIMIT 10

SELECT "p"."PostId", "p"."BlogId", "p"."Content", "p"."Title"
FROM "Post" AS "p"
INNER JOIN (
    SELECT DISTINCT "x"."Url", "x"."BlogId"
    FROM "Blog" AS "x"
    LIMIT 10
) AS "x" ON "p"."BlogId" = "x"."BlogId"
ORDER BY "x"."Url", "x"."BlogId"

(There should be an ORDER BY in the inner query)

Workaround:

db.Blogs.Include(x => x.Posts).OrderBy(x => x.Url).Skip(0).Take(10).ToList(); 

SimonTouchtech avatar Nov 12 '15 16:11 SimonTouchtech

The OrderBy is lost when Take is used with an Include but preserved when Skip is used. This appears to be because the SelectExpression.IsDistinct checks for an offset (but not a limit) and pushes the query into a subquery.

If SelectExpression.IsDistincit is modified to push the query into a subquery on limit, the OrderBy is preserved, but there are problems with the materialization related to #4083.

The following example using the Northwind context illustrates the issue:

            using (var context = CreateContext())
            {
                var results = context.Customers.Include(c => c.Orders).OrderBy(c => c.ContactName).Take(10).ToList();

                var orders = context.ChangeTracker.Entries().Select(e => e.Entity).OfType<Order>();

                var orderCount = results.SelectMany(c => c.Orders).Distinct().Count();
            }

Results in 11 Order instances materializing with the following generated SQL:

@__p_0: 10

SELECT TOP(@__p_0) [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]
ORDER BY [c].[ContactName], [c].[CustomerID]

@__p_0: 10

SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
INNER JOIN (
    SELECT DISTINCT TOP(@__p_0) [c].[ContactName], [c].[CustomerID]
    FROM [Customers] AS [c]
) AS [c] ON [o].[CustomerID] = [c].[CustomerID]
ORDER BY [c].[ContactName], [c].[CustomerID]

If limit is pushed into the subquery, only 1 Order instance is materialized with the following SQL:

@__p_0: 10

SELECT TOP(@__p_0) [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]
ORDER BY [c].[ContactName], [c].[CustomerID]

@__p_0: 10

SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
INNER JOIN (
    SELECT DISTINCT [t0].*
    FROM (
        SELECT TOP(@__p_0) [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]
        ORDER BY [c].[ContactName], [c].[CustomerID]
    ) AS [t0]
) AS [c] ON [o].[CustomerID] = [c].[CustomerID]

mikary avatar Dec 14 '15 23:12 mikary

Clearing milestone for triage with related issues

mikary avatar Dec 14 '15 23:12 mikary

I worked a bit with @mikary on the analysis today and it looks like there are several aspects at play in this bug and it would be better for @anpete to work on it. So I am restoring the milestone to be 7.0.0-rc2 and assigning it to him.

divega avatar Dec 15 '15 00:12 divega

We are still experiencing this issue on the recent version: 6.1.3. What is the current status of this?

michalczerwinski avatar Mar 21 '17 15:03 michalczerwinski

@michalczerwinski This repository (and issue) is for Entity Framework Core. If you're using Entity Framework 6.x you should go to https://github.com/aspnet/EntityFramework6

SimonTouchtech avatar Mar 21 '17 15:03 SimonTouchtech