diff options
| author | Shadowghost <Ghost_of_Stone@web.de> | 2026-03-08 15:10:01 +0100 |
|---|---|---|
| committer | Shadowghost <Ghost_of_Stone@web.de> | 2026-03-08 15:26:35 +0100 |
| commit | ba722b45175a15b66d6c934d80a50bbb1ed6e695 (patch) | |
| tree | a8d1c8eea6c36c46095455c1e10b1f21476f29d9 /Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs | |
| parent | 1d8bdcc411e1ba34841c8558992c4f0fb2c25708 (diff) | |
Optimize Search and NextUp queries
Diffstat (limited to 'Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs')
| -rw-r--r-- | Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs b/Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs index b1f7326d06..83f108bed0 100644 --- a/Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs +++ b/Jellyfin.Server.Implementations/Item/BaseItemRepository.QueryBuilding.cs @@ -68,22 +68,24 @@ public sealed partial class BaseItemRepository // for that case the invoker has to run a DistinctBy(e => e.PresentationUniqueKey) on their own var enableGroupByPresentationUniqueKey = EnableGroupByPresentationUniqueKey(filter); - // Use Min(Id) instead of OrderBy(Id).FirstOrDefault() to avoid EF Core generating - // a correlated scalar subquery per group. + // Materialize GroupBy IDs first to split the complex expression tree. + // This runs the filter+GroupBy+Min as one simple SQL query, then the downstream + // Order/Paging/Navigations work on a flat WHERE Id IN (...) list, avoiding + // EF Core having to compile a deeply nested expression tree. if (enableGroupByPresentationUniqueKey && filter.GroupBySeriesPresentationUniqueKey) { - var tempQuery = dbQuery.GroupBy(e => new { e.PresentationUniqueKey, e.SeriesPresentationUniqueKey }).Select(e => e.Min(x => x.Id)); - dbQuery = context.BaseItems.Where(e => tempQuery.Contains(e.Id)); + var groupedIds = dbQuery.GroupBy(e => new { e.PresentationUniqueKey, e.SeriesPresentationUniqueKey }).Select(e => e.Min(x => x.Id)).ToList(); + dbQuery = context.BaseItems.Where(e => groupedIds.Contains(e.Id)); } else if (enableGroupByPresentationUniqueKey) { - var tempQuery = dbQuery.GroupBy(e => e.PresentationUniqueKey).Select(e => e.Min(x => x.Id)); - dbQuery = context.BaseItems.Where(e => tempQuery.Contains(e.Id)); + var groupedIds = dbQuery.GroupBy(e => e.PresentationUniqueKey).Select(e => e.Min(x => x.Id)).ToList(); + dbQuery = context.BaseItems.Where(e => groupedIds.Contains(e.Id)); } else if (filter.GroupBySeriesPresentationUniqueKey) { - var tempQuery = dbQuery.GroupBy(e => e.SeriesPresentationUniqueKey).Select(e => e.Min(x => x.Id)); - dbQuery = context.BaseItems.Where(e => tempQuery.Contains(e.Id)); + var groupedIds = dbQuery.GroupBy(e => e.SeriesPresentationUniqueKey).Select(e => e.Min(x => x.Id)).ToList(); + dbQuery = context.BaseItems.Where(e => groupedIds.Contains(e.Id)); } else { |
