You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT [t].[Id], (
SELECT TOP(1) (
SELECT TOP(1) [t30].[Title]
FROM [VarDesign_Parts-Title_Translates] AS [t30]
WHERE ([t30].[LangISOCode] = 'SLV') AND ([t20].[Id] = [t30].[PartTitleId])
)
FROM [VarDesign_Part-Titles] AS [t20]
WHERE [t].[Id] = [t20].[PartId]
ORDER BY [t20].[IsDefault]
) AS [title]
FROM [VarDesign_Parts] AS [t]
ORDER BY (
SELECT TOP(1) (
SELECT TOP(1) [t3].[Title]
FROM [VarDesign_Parts-Title_Translates] AS [t3]
WHERE ([t3].[LangISOCode] = 'SLV') AND ([t2].[Id] = [t3].[PartTitleId])
)
FROM [VarDesign_Part-Titles] AS [t2]
WHERE [t].[Id] = [t2].[PartId]
ORDER BY [t2].[IsDefault]
)
OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY
but better would be:
SELECT [t].[Id], (
SELECT TOP(1) (
SELECT TOP(1) [t30].[Title]
FROM [VarDesign_Parts-Title_Translates] AS [t30]
WHERE ([t30].[LangISOCode] = 'SLV') AND ([t20].[Id] = [t30].[PartTitleId])
)
FROM [VarDesign_Part-Titles] AS [t20]
WHERE [t].[Id] = [t20].[PartId]
ORDER BY [t20].[IsDefault]
) AS [title]
FROM [VarDesign_Parts] AS [t]
ORDER BY [title]
OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY
There is no need to select fields again for ORDER BY, just use already returned field if exists.
I am attaching screenshot for ClientStatistics in Management Studio. Trial 1 and 2 are SQL created by EF. Trial 3 and 4 are when I manually correct sort. The difference in execution is approx half faster.
Further technical details
EF Core version: 2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017
The text was updated successfully, but these errors were encountered:
MaklaCof
changed the title
Query: optimize for OrderBy on navigation property
Query: optimize OrderBy on navigation property
Nov 23, 2017
De-duping a SelectExpression in order by is non-trivial task. It is same QueryModel but when we translate it both times, we generate different aliases to avoid name clash. So inside SelectExpression we cannot match and de-dupe it like we do with other complex expression.
@MaklaCof - Given there is perf differences, we will discuss this in team in next triage meeting. Thanks for detailed info on perf.
Given there is perf differences, we will discuss this in team in next triage meeting. Thanks for detailed info on perf.
Great. Thank you.
I imagine this is common scenario when you display result in grid (together with columns from detail tables) and you want to order by column from detail table.
OrderBy could be optimized on navigational property.
For example, this query
produce this SQL:
but better would be:
There is no need to select fields again for ORDER BY, just use already returned field if exists.
I am attaching screenshot for ClientStatistics in Management Studio. Trial 1 and 2 are SQL created by EF. Trial 3 and 4 are when I manually correct sort. The difference in execution is approx half faster.
Further technical details
EF Core version: 2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017
The text was updated successfully, but these errors were encountered: