Skip to content

Query with nested projections result in a expensive ORDER BY #24239

Closed as duplicate

Description

@mm98

I have noticed some strange behavior, when using nested projections (DTO) with EFCore. I don't know if this also happened in the previous versions.

This is a simplified version of the code.

public class Conversation
{
	public int ID { get; private set; }

	private readonly List<ConversationParticipant> _participants = new List<ConversationParticipant>();
	public IEnumerable<ConversationParticipant> Participants => _participants;
}

public class ConversationParticipant
{
	public int ID { get; private set; }

	public int ConversationID { get; private set; }
	public Conversation Conversation { get; private set; }

	public int UserID { get; private set; }
	public UserBase User { get; private set; }

	public int? GroupID { get; private set; }
	public GroupBase Group { get; private set; }
}

var dbSet = _dbContext.Set<Conversation>();

var query1 = dbSet.Where(e => e.ID == 1).Select(e => new { e.ID, e.Guid });
var query2 = dbSet.Where(e => e.ID == 1).Select(e => new { e.ID, e.Guid, Participants = e.Participants.Select(p => new { p.ConversationID }) });

var output1 = query1.ToQueryString();
var output2 = query2.ToQueryString();

The SQL from output1 is correct

SELECT [c].[ConversationID] AS [ID], [c].[Guid]
FROM [Conversations] AS [c]
WHERE [c].[ConversationID] = 1

The SQL for output2 suddenly have multiple colums in the ORDER BY. (UserID is suddenly included also)

SELECT [c].[ConversationID], [c].[Guid], [c0].[ConversationID], [c0].[UserID]
FROM [Conversations] AS [c]
LEFT JOIN [ConversationParticipants] AS [c0] ON [c].[ConversationID] = [c0].[ConversationID]
WHERE [c].[ConversationID] = 1
ORDER BY [c].[ConversationID], [c0].[ConversationID], [c0].[UserID]

This is kinda bad, since multiple nested projections can contain 10 columns in the ORDER BY - even with a SingleOrDefaultAsyns(). The performance will become very bad.

I noticed this, when looking at MiniProfiler output, for a single query. In my case, the SQL generated was the following (notice the generated order by):

DECLARE @__user_ID_0 int = 1,
        @__guid_1 uniqueidentifier = '32a2fb7f-a93c-403f-9c0d-0515470228e2';

SELECT [t].[ConversationID], [t].[Guid], [t].[CreatorID], [t].[Title], [t].[DateCreated], [t].[DateModified], [t].[ConversationID0], [t].[UserID], [t1].[MessageID], [t1].[Guid], [t1].[SenderID], [t1].[Text], [t1].[DateCreated], [t1].[Guid0], [t1].[FileName], [t1].[FileSize], [t1].[MessageAttachmentID], [t1].[Reaction], [t1].[UserID], [t1].[MessageID0], [t1].[UserID0], [t1].[DateRead], [t1].[MessageID1], [t2].[UserID], [t2].[Name], [t2].[Avatar], [t2].[ConversationID], [t2].[UserID0]
FROM (
    SELECT TOP(2) [c].[ConversationID], [c].[Guid], [c].[CreatorID], [c].[Title], [c].[DateCreated], [c].[DateModified], [c0].[ConversationID] AS [ConversationID0], [c0].[UserID]
    FROM [Conversations] AS [c]
    INNER JOIN [ConversationParticipants] AS [c0] ON [c].[ConversationID] = [c0].[ConversationID]
    WHERE ([c0].[UserID] = @__user_ID_0) AND ([c].[Guid] = @__guid_1)
) AS [t]
LEFT JOIN (
    SELECT [m].[MessageID], [m].[Guid], [m].[SenderID], [m].[Text], [m].[DateCreated], [m0].[Guid] AS [Guid0], [m0].[FileName], [m0].[FileSize], [m0].[MessageAttachmentID], [m1].[Reaction], [m1].[UserID], [m1].[MessageID] AS [MessageID0], [t0].[UserID] AS [UserID0], [t0].[DateRead], [t0].[MessageID] AS [MessageID1], [m].[ConversationID]
    FROM [Messages] AS [m]
    LEFT JOIN [MessageAttachments] AS [m0] ON [m].[MessageID] = [m0].[MessageID]
    LEFT JOIN [MessageReactions] AS [m1] ON [m].[MessageID] = [m1].[MessageID]
    LEFT JOIN (
        SELECT [m2].[UserID], [m2].[DateRead], [m2].[MessageID]
        FROM [MessageStates] AS [m2]
        WHERE [m2].[Read] = CAST(1 AS bit)
    ) AS [t0] ON [m].[MessageID] = [t0].[MessageID]
) AS [t1] ON [t].[ConversationID] = [t1].[ConversationID]
LEFT JOIN (
    SELECT [u].[UserID], [u].[Name], [u].[Avatar], [c1].[ConversationID], [c1].[UserID] AS [UserID0]
    FROM [ConversationParticipants] AS [c1]
    INNER JOIN [Users] AS [u] ON [c1].[UserID] = [u].[UserID]
) AS [t2] ON [t].[ConversationID] = [t2].[ConversationID]
ORDER BY [t].[ConversationID], [t].[ConversationID0], [t].[UserID], [t1].[MessageID], [t1].[MessageAttachmentID], [t1].[MessageID0], [t1].[UserID], [t1].[Reaction], [t1].[MessageID1], [t1].[UserID0], [t2].[ConversationID], [t2].[UserID0], [t2].[UserID];

EF Core version: 5.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 5.0.3
Operating system: Windows 10
IDE: Microsoft Visual Studio Community 2019, Version 16.8.6

Activity

  1. changed the title [-]Query with nested projections add multiple orderby in generated SQL[/-] [+]Query with nested projections result in a expensive ORDER BY[/+] on Feb 23, 2021
  2. roji commented on Feb 23, 2021

    @roji
    Member

    You probably want to take a look at #19571 in order to understand what those ORDER BY is doing there - they are necessary (the last one specifically can be removed, this is tracked by #19828).

    As a general rule, doing many one-to-many joins (whether with Include or with projections) can be problematic performance-wise, and not just because of the ORDER BY - the so-called cartesian explosion effect can cause a lot of data to get duplicated. You may want to consider using split queries.

  3. mm98 commented on Feb 23, 2021

    @mm98
    Author

    @roji Thanks for the update! Did search the issues for ORDER BY, but I forgot - that it was only searching in open issues ... Sorry about that.

  4. roji commented on Feb 23, 2021

    @roji
    Member

    Duplicate of #19571

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions