Avoid splitting query with filtered include Take(1)

Open
#23,657 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, postgresql
Domain
databases

Research direction

Start by reproducing the query using AsSplitQuery, Include, Take(1), and ToArray with the supplied PostgreSQL model and SQL. Trace the EF Core query translation and split-query entry points to understand why the filtered include is split. Done means the filtered include can use a single join where appropriate while split queries still avoid cartesian explosion for additional includes, with coverage for the generated SQL.

Written by the indexing model from the issue text.

Description

area-query customer-reported needs-design
public class Post
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Author> Authors { get; set; }
}

public class Author
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class MyDbContext : DbContext
{
    public static readonly ILoggerFactory LoggerFactory = LoggerFactory.Create(b => b.AddConsole());

    public DbSet<Post> Posts { get; set; }
    public DbSet<Author> Authors { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options) =>
        options.UseNpgsql("User ID=postgres;Password=root;Host=localhost;Port=5432;Database=test")
            .UseLoggerFactory(LoggerFactory);
}

await using (var db = new MyDbContext());
    Post[] posts = db.Posts
        .AsSplitQuery()
        .Include(p => p.Authors.Take(1))
        .ToArray();

The above code using AsSplitQuery generates the two queries:

SELECT p."Id", p."Name" FROM "Posts" AS p ORDER BY p."Id";

SELECT t0."Id", t0."Name", t0."PostId", p."Id"
FROM "Posts" AS p
INNER JOIN (
    SELECT t."Id", t."Name", t."PostId"
    FROM (
        SELECT a."Id", a."Name", a."PostId", ROW_NUMBER() OVER(PARTITION BY a."PostId" ORDER BY a."Id") AS row
        FROM "Authors" AS a
    ) AS t
WHERE t.row <= 1) AS t0 ON p."Id" = t0."PostId"
ORDER BY p."Id", t0."PostId", t0."Id";

when only one with a join could be made because of the Take(1), something like:

SELECT p."Id", p."Name", a."Id", a."Name"
FROM "Posts" AS p 
JOIN "Authors" AS a ON p."Id" = a."PostId"
ORDER BY p."Id", a."Id";

One could say don't use AsSplitQuery but let's assume I'm also including all comments of a post, splitting query would help avoid cartesian explosion.

Also the generated join seems very complex, is it expected or should I open another issue?

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.