From fa65a392b0e754848caf94f08724ba19ec8bdd9f Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Mon, 4 May 2026 10:22:13 +0200 Subject: Fix Playlist and Boxset query and count perf --- MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs | 32 +++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'MediaBrowser.LocalMetadata') diff --git a/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs b/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs index a065b68321..b0f51aec71 100644 --- a/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs +++ b/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; @@ -6,7 +7,6 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml; -using Jellyfin.Data.Enums; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; @@ -485,16 +485,38 @@ namespace MediaBrowser.LocalMetadata.Savers return; } + // Batch-resolve all ItemIds to paths in a single query to avoid an N+1 round-trip per linked child + var idsToResolve = new HashSet(); + foreach (var link in linkedChildren) + { + if (link.ItemId.HasValue && !link.ItemId.Value.Equals(Guid.Empty)) + { + idsToResolve.Add(link.ItemId.Value); + } + } + + Dictionary? pathById = null; + if (idsToResolve.Count > 0) + { + var batched = LibraryManager.GetItemList(new InternalItemsQuery + { + ItemIds = [.. idsToResolve] + }); + pathById = new Dictionary(batched.Count); + foreach (var batchedItem in batched) + { + pathById[batchedItem.Id] = batchedItem.Path; + } + } + await writer.WriteStartElementAsync(null, pluralNodeName, null).ConfigureAwait(false); foreach (var link in linkedChildren) { - // Resolve ItemId to get the item's path for XML portability string? path = null; - if (link.ItemId.HasValue && !link.ItemId.Value.Equals(Guid.Empty)) + if (pathById is not null && link.ItemId.HasValue && pathById.TryGetValue(link.ItemId.Value, out var resolvedPath)) { - var linkedItem = LibraryManager.GetItemById(link.ItemId.Value); - path = linkedItem?.Path; + path = resolvedPath; } if (!string.IsNullOrWhiteSpace(path)) -- cgit v1.2.3