Query optimization: remove inner join from subquery navigation contains

Open
#4,389 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, sql
Domain
backend, databases

Research direction

Reproduce the Where_subquery_on_navigation test described in the issue and inspect the generated SQL for the navigation Contains query. Compare the current and proposed SQL, then verify that same-table inner joins are eliminated without changing the result or query semantics.

Written by the indexing model from the issue text.

Description

area-query blocked

When a sub query uses Contains on a navigation, like the following:

        [ConditionalFact]
        public virtual void Where_subquery_on_navigation()
        {
            using (var context = CreateContext())
            {
                var query = from p in context.Products
                            where p.OrderDetails.Contains(context.OrderDetails.FirstOrDefault(orderDetail => orderDetail.Quantity == 1))
                            select p;

                var result = query.ToList();

                Assert.Equal(1, result.Count);
            }
        }

The SQL contains an inner join on the same table:

SELECT [p].[ProductID], [p].[Discontinued], [p].[ProductName], [p].[UnitsInStock]
FROM [Products] AS [p]
WHERE (
    SELECT CASE
        WHEN EXISTS (
            SELECT 1
            FROM (
                SELECT [o].[OrderID], [o].[ProductID]
                FROM [Order Details] AS [o]
                WHERE [p].[ProductID] = [o].[ProductID]
            ) AS [t]
            INNER JOIN (
                SELECT TOP(1) [orderDetail].[OrderID], [orderDetail].[ProductID]
                FROM [Order Details] AS [orderDetail]
                WHERE [orderDetail].[Quantity] = 1
            ) AS [t0] ON ([t].[OrderID] = [t0].[OrderID]) AND ([t].[ProductID] = [t0].[ProductID]))
        THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
    END
) = 1

In cases where the Inner Join is on the same table, it may be possible to eliminate the join

SELECT [p].[ProductID], [p].[Discontinued], [p].[ProductName], [p].[UnitsInStock]
FROM [Products] AS [p]
WHERE (
    SELECT CASE
        WHEN EXISTS (
            SELECT 1
            FROM (
                SELECT TOP(1) [orderDetail].[ProductID]
                FROM [Order Details] AS [orderDetail]
                WHERE [orderDetail].[Quantity] = 1
            ) AS [t]
            WHERE [p].[ProductID] = [t].[ProductID])
        THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
    END
) = 1
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.