The Free Software Media System - Server Backend & API
Optimize EF Core queries and remove unnecessary AsQueryable calls
Patrick Barronf07553abdf9c
diff --git a/Jellyfin.Server.Implementations/Activity/ActivityManager.cs b/Jellyfin.Server.Implementations/Activity/ActivityManager.csindex 9d6ca6aabe..fc03cd6ae4 100644--- a/Jellyfin.Server.Implementations/Activity/ActivityManager.cs+++ b/Jellyfin.Server.Implementations/Activity/ActivityManager.cs@@ -48,18 +48,10 @@ namespace Jellyfin.Server.Implementations.Activityvar dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);await using (dbContext.ConfigureAwait(false)){- IQueryable<ActivityLog> entries = dbContext.ActivityLogs- .OrderByDescending(entry => entry.DateCreated);-- if (query.MinDate.HasValue)- {- entries = entries.Where(entry => entry.DateCreated >= query.MinDate);- }-- if (query.HasUserId.HasValue)- {- entries = entries.Where(entry => (!entry.UserId.Equals(default)) == query.HasUserId.Value);- }+ var entries = dbContext.ActivityLogs+ .OrderByDescending(entry => entry.DateCreated)+ .Where(entry => query.MinDate == null || entry.DateCreated >= query.MinDate)+ .Where(entry => !query.HasUserId.HasValue || entry.UserId.Equals(default) != query.HasUserId.Value);return new QueryResult<ActivityLogEntry>(query.Skip,@@ -67,8 +59,16 @@ namespace Jellyfin.Server.Implementations.Activityawait entries.Skip(query.Skip ?? 0).Take(query.Limit ?? 100)- .AsAsyncEnumerable()- .Select(ConvertToOldModel)+ .Select(entity => new ActivityLogEntry(entity.Name, entity.Type, entity.UserId)+ {+ Id = entity.Id,+ Overview = entity.Overview,+ ShortOverview = entity.ShortOverview,+ ItemId = entity.ItemId,+ Date = entity.DateCreated,+ Severity = entity.LogSeverity+ })+ .AsQueryable().ToListAsync().ConfigureAwait(false));}diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.csindex 15ac5c668a..a9b974be0e 100644--- a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs+++ b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs@@ -54,7 +54,7 @@ namespace Jellyfin.Server.Implementations.Devicesvar dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);await using (dbContext.ConfigureAwait(false)){- deviceOptions = await dbContext.DeviceOptions.AsQueryable().FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).ConfigureAwait(false);+ deviceOptions = await dbContext.DeviceOptions.FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).ConfigureAwait(false);if (deviceOptions is null){deviceOptions = new DeviceOptions(deviceId);@@ -132,22 +132,11 @@ namespace Jellyfin.Server.Implementations.Devicesvar dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);await using (dbContext.ConfigureAwait(false)){- var devices = dbContext.Devices.AsQueryable();-- if (query.UserId.HasValue)- {- devices = devices.Where(device => device.UserId.Equals(query.UserId.Value));- }-- if (query.DeviceId is not null)- {- devices = devices.Where(device => device.DeviceId == query.DeviceId);- }-- if (query.AccessToken is not null)- {- devices = devices.Where(device => device.AccessToken == query.AccessToken);- }+ var devices = dbContext.Devices+ .OrderBy(d => d.Id)+ .Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value))+ .Where(device => query.DeviceId == null || device.DeviceId == query.DeviceId)+ .Where(device => query.AccessToken == null || device.AccessToken == query.AccessToken);var count = await devices.CountAsync().ConfigureAwait(false);@@ -179,11 +168,10 @@ namespace Jellyfin.Server.Implementations.Devices/// <inheritdoc />public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId, bool? supportsSync){- IAsyncEnumerable<Device> sessions;var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);await using (dbContext.ConfigureAwait(false)){- sessions = dbContext.Devices+ IAsyncEnumerable<Device> sessions = dbContext.Devices.Include(d => d.User).OrderByDescending(d => d.DateLastActivity).ThenBy(d => d.DeviceId)diff --git a/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs b/Jellyfin.Server.Implementations/Security/AuthenticationManager.csindex 810e578075..1b56237fe7 100644--- a/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs+++ b/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs@@ -40,7 +40,6 @@ namespace Jellyfin.Server.Implementations.Securityawait using (dbContext.ConfigureAwait(false)){return await dbContext.ApiKeys- .AsAsyncEnumerable().Select(key => new AuthenticationInfo{AppName = key.Name,@@ -60,7 +59,6 @@ namespace Jellyfin.Server.Implementations.Securityawait using (dbContext.ConfigureAwait(false)){var key = await dbContext.ApiKeys- .AsQueryable().Where(apiKey => apiKey.AccessToken == accessToken).FirstOrDefaultAsync().ConfigureAwait(false);diff --git a/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs b/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.csindex fddad1c4f9..8936f57c62 100644--- a/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs+++ b/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs@@ -62,7 +62,6 @@ namespace Jellyfin.Server.Implementations.Userspublic IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client){return _dbContext.ItemDisplayPreferences- .AsQueryable().Where(prefs => prefs.UserId.Equals(userId) && !prefs.ItemId.Equals(default) && string.Equals(prefs.Client, client)).ToList();}@@ -71,7 +70,6 @@ namespace Jellyfin.Server.Implementations.Userspublic Dictionary<string, string?> ListCustomItemDisplayPreferences(Guid userId, Guid itemId, string client){return _dbContext.CustomItemDisplayPreferences- .AsQueryable().Where(prefs => prefs.UserId.Equals(userId)&& prefs.ItemId.Equals(itemId)&& string.Equals(prefs.Client, client))@@ -82,7 +80,6 @@ namespace Jellyfin.Server.Implementations.Userspublic void SetCustomItemDisplayPreferences(Guid userId, Guid itemId, string client, Dictionary<string, string?> customPreferences){var existingPrefs = _dbContext.CustomItemDisplayPreferences- .AsQueryable().Where(prefs => prefs.UserId.Equals(userId)&& prefs.ItemId.Equals(itemId)&& string.Equals(prefs.Client, client));diff --git a/Jellyfin.Server.Implementations/Users/UserManager.cs b/Jellyfin.Server.Implementations/Users/UserManager.csindex 19ac007b93..5f9814ed37 100644--- a/Jellyfin.Server.Implementations/Users/UserManager.cs+++ b/Jellyfin.Server.Implementations/Users/UserManager.cs@@ -143,7 +143,6 @@ namespace Jellyfin.Server.Implementations.Usersawait using (dbContext.ConfigureAwait(false)){if (await dbContext.Users- .AsQueryable().AnyAsync(u => u.Username == newName && !u.Id.Equals(user.Id)).ConfigureAwait(false)){