diff options
| author | Shadowghost <Ghost_of_Stone@web.de> | 2026-05-04 10:22:13 +0200 |
|---|---|---|
| committer | Shadowghost <Ghost_of_Stone@web.de> | 2026-05-04 10:25:02 +0200 |
| commit | fa65a392b0e754848caf94f08724ba19ec8bdd9f (patch) | |
| tree | 3e404f475eed663163f8fb16135bad54df57950a /MediaBrowser.LocalMetadata | |
| parent | 622947e37425f3620432995cde5d4a0809d91694 (diff) | |
Fix Playlist and Boxset query and count perf
Diffstat (limited to 'MediaBrowser.LocalMetadata')
| -rw-r--r-- | MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs | 32 |
1 files changed, 27 insertions, 5 deletions
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<Guid>(); + foreach (var link in linkedChildren) + { + if (link.ItemId.HasValue && !link.ItemId.Value.Equals(Guid.Empty)) + { + idsToResolve.Add(link.ItemId.Value); + } + } + + Dictionary<Guid, string?>? pathById = null; + if (idsToResolve.Count > 0) + { + var batched = LibraryManager.GetItemList(new InternalItemsQuery + { + ItemIds = [.. idsToResolve] + }); + pathById = new Dictionary<Guid, string?>(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)) |
