efcore icon indicating copy to clipboard operation
efcore copied to clipboard

When is "order by" generated in the underlying sql command text

Open blfuentes opened this issue 2 years ago • 2 comments

I've been trying to understand when the query generated by EF core adds an "order by" clause in the executed sql query. I noticed that querying with and .Include often adds the order by for the primary key/s of the main queried table, but not always. Reading this https://www.entityframeworktutorial.net/efcore/querying-in-ef-core.aspx I couldn't see any specific explanation about it.

Any documentation regarding this behaviour?

EF Core version: 7x Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .net 8

blfuentes avatar Oct 24 '24 18:10 blfuentes

I don't think its documented on the doc sites specifically. We have some in-depth information on the topic on github. Specifically: https://github.com/dotnet/efcore/issues/20076 and https://github.com/dotnet/efcore/issues/29171

in short, we add ordering every time you project entity and associated collection. (e.g customers.Include(x => x.Orders) When constructing objects we are converting from flat structure:

CustomerId | CustomerName | OrderId | OrderName

1             c1            1         o1
1             c1            2         o2
1             c1            4         o4
2             c2            5         c5

to

Customer { CustomerId = 1, CustomerName = c1, Orders = [Order { OrderId = 1, OrderName = o1 }, Order { OrderId = 2, OrderName = o2 }, Order { OrderId = 4, OrderName = o4 }]
Customer { CustomerId = 2, CustomerName = c2, Orders = [Order(5, o5}]

At some point we decided we want those results to be streamable - you can foreach over the results and they should make sense


foreach (var customer in ctx.Customers.Include(x => x.Orders))
{
    // count should be correct here
    Console.WriteLine(customer.Orders.Count();
}

The way we do this is we order the results based on customer Id, create customer object and then read rows from the database which contain information about orders, until we detect that the customerId value has changed. This is a signal for us, that all the orders for that customer have already been read, and so that the Orders collection for that customer is complete and we can return the result (and the customer.Orders.Count() will give you the correct value.

If we didn't add the order, it's possible that the rows would come out of the database like this:

CustomerId | CustomerName | OrderId | OrderName

1             c1            1         o1
2             c2            5         c5 // out of order!
1             c1            2         o2
1             c1            4         o4

and we wouldn't know if all the orders for customer 1 has been read until we go through all the rows coming from the query, so streaming would be impossible.

maumar avatar Oct 25 '24 06:10 maumar

Thanks for the clarification!

blfuentes avatar Oct 25 '24 07:10 blfuentes