Optimize EF Core queries and remove unnecessary AsQueryable calls

pull/9108/head
Patrick Barron 2 years ago
parent ab6baf6486
commit f07553abdf

@ -48,18 +48,10 @@ namespace Jellyfin.Server.Implementations.Activity
var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false); var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false)) await using (dbContext.ConfigureAwait(false))
{ {
IQueryable<ActivityLog> entries = dbContext.ActivityLogs var entries = dbContext.ActivityLogs
.OrderByDescending(entry => entry.DateCreated); .OrderByDescending(entry => entry.DateCreated)
.Where(entry => query.MinDate == null || entry.DateCreated >= query.MinDate)
if (query.MinDate.HasValue) .Where(entry => !query.HasUserId.HasValue || entry.UserId.Equals(default) != query.HasUserId.Value);
{
entries = entries.Where(entry => entry.DateCreated >= query.MinDate);
}
if (query.HasUserId.HasValue)
{
entries = entries.Where(entry => (!entry.UserId.Equals(default)) == query.HasUserId.Value);
}
return new QueryResult<ActivityLogEntry>( return new QueryResult<ActivityLogEntry>(
query.Skip, query.Skip,
@ -67,8 +59,16 @@ namespace Jellyfin.Server.Implementations.Activity
await entries await entries
.Skip(query.Skip ?? 0) .Skip(query.Skip ?? 0)
.Take(query.Limit ?? 100) .Take(query.Limit ?? 100)
.AsAsyncEnumerable() .Select(entity => new ActivityLogEntry(entity.Name, entity.Type, entity.UserId)
.Select(ConvertToOldModel) {
Id = entity.Id,
Overview = entity.Overview,
ShortOverview = entity.ShortOverview,
ItemId = entity.ItemId,
Date = entity.DateCreated,
Severity = entity.LogSeverity
})
.AsQueryable()
.ToListAsync() .ToListAsync()
.ConfigureAwait(false)); .ConfigureAwait(false));
} }

@ -54,7 +54,7 @@ namespace Jellyfin.Server.Implementations.Devices
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.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) if (deviceOptions is null)
{ {
deviceOptions = new DeviceOptions(deviceId); deviceOptions = new DeviceOptions(deviceId);
@ -132,22 +132,11 @@ namespace Jellyfin.Server.Implementations.Devices
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false)) await using (dbContext.ConfigureAwait(false))
{ {
var devices = dbContext.Devices.AsQueryable(); var devices = dbContext.Devices
.OrderBy(d => d.Id)
if (query.UserId.HasValue) .Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value))
{ .Where(device => query.DeviceId == null || device.DeviceId == query.DeviceId)
devices = devices.Where(device => device.UserId.Equals(query.UserId.Value)); .Where(device => query.AccessToken == null || device.AccessToken == query.AccessToken);
}
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 count = await devices.CountAsync().ConfigureAwait(false); var count = await devices.CountAsync().ConfigureAwait(false);
@ -179,11 +168,10 @@ namespace Jellyfin.Server.Implementations.Devices
/// <inheritdoc /> /// <inheritdoc />
public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId, bool? supportsSync) public async Task<QueryResult<DeviceInfo>> GetDevicesForUser(Guid? userId, bool? supportsSync)
{ {
IAsyncEnumerable<Device> sessions;
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false)) await using (dbContext.ConfigureAwait(false))
{ {
sessions = dbContext.Devices IAsyncEnumerable<Device> sessions = dbContext.Devices
.Include(d => d.User) .Include(d => d.User)
.OrderByDescending(d => d.DateLastActivity) .OrderByDescending(d => d.DateLastActivity)
.ThenBy(d => d.DeviceId) .ThenBy(d => d.DeviceId)

@ -40,7 +40,6 @@ namespace Jellyfin.Server.Implementations.Security
await using (dbContext.ConfigureAwait(false)) await using (dbContext.ConfigureAwait(false))
{ {
return await dbContext.ApiKeys return await dbContext.ApiKeys
.AsAsyncEnumerable()
.Select(key => new AuthenticationInfo .Select(key => new AuthenticationInfo
{ {
AppName = key.Name, AppName = key.Name,
@ -60,7 +59,6 @@ namespace Jellyfin.Server.Implementations.Security
await using (dbContext.ConfigureAwait(false)) await using (dbContext.ConfigureAwait(false))
{ {
var key = await dbContext.ApiKeys var key = await dbContext.ApiKeys
.AsQueryable()
.Where(apiKey => apiKey.AccessToken == accessToken) .Where(apiKey => apiKey.AccessToken == accessToken)
.FirstOrDefaultAsync() .FirstOrDefaultAsync()
.ConfigureAwait(false); .ConfigureAwait(false);

@ -62,7 +62,6 @@ namespace Jellyfin.Server.Implementations.Users
public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client) public IList<ItemDisplayPreferences> ListItemDisplayPreferences(Guid userId, string client)
{ {
return _dbContext.ItemDisplayPreferences return _dbContext.ItemDisplayPreferences
.AsQueryable()
.Where(prefs => prefs.UserId.Equals(userId) && !prefs.ItemId.Equals(default) && string.Equals(prefs.Client, client)) .Where(prefs => prefs.UserId.Equals(userId) && !prefs.ItemId.Equals(default) && string.Equals(prefs.Client, client))
.ToList(); .ToList();
} }
@ -71,7 +70,6 @@ namespace Jellyfin.Server.Implementations.Users
public Dictionary<string, string?> ListCustomItemDisplayPreferences(Guid userId, Guid itemId, string client) public Dictionary<string, string?> ListCustomItemDisplayPreferences(Guid userId, Guid itemId, string client)
{ {
return _dbContext.CustomItemDisplayPreferences return _dbContext.CustomItemDisplayPreferences
.AsQueryable()
.Where(prefs => prefs.UserId.Equals(userId) .Where(prefs => prefs.UserId.Equals(userId)
&& prefs.ItemId.Equals(itemId) && prefs.ItemId.Equals(itemId)
&& string.Equals(prefs.Client, client)) && string.Equals(prefs.Client, client))
@ -82,7 +80,6 @@ namespace Jellyfin.Server.Implementations.Users
public void SetCustomItemDisplayPreferences(Guid userId, Guid itemId, string client, Dictionary<string, string?> customPreferences) public void SetCustomItemDisplayPreferences(Guid userId, Guid itemId, string client, Dictionary<string, string?> customPreferences)
{ {
var existingPrefs = _dbContext.CustomItemDisplayPreferences var existingPrefs = _dbContext.CustomItemDisplayPreferences
.AsQueryable()
.Where(prefs => prefs.UserId.Equals(userId) .Where(prefs => prefs.UserId.Equals(userId)
&& prefs.ItemId.Equals(itemId) && prefs.ItemId.Equals(itemId)
&& string.Equals(prefs.Client, client)); && string.Equals(prefs.Client, client));

@ -143,7 +143,6 @@ namespace Jellyfin.Server.Implementations.Users
await using (dbContext.ConfigureAwait(false)) await using (dbContext.ConfigureAwait(false))
{ {
if (await dbContext.Users if (await dbContext.Users
.AsQueryable()
.AnyAsync(u => u.Username == newName && !u.Id.Equals(user.Id)) .AnyAsync(u => u.Username == newName && !u.Id.Equals(user.Id))
.ConfigureAwait(false)) .ConfigureAwait(false))
{ {

Loading…
Cancel
Save