Avoid splitting query with filtered include Take(1)
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100