perf: add AsSplitQuery to repository queries with multiple Include calls
This commit is contained in:
@@ -109,6 +109,7 @@ public class AlbumRepository(LactoseDbContext context) : IAlbumRepository {
|
||||
|
||||
/// <inheritdoc />
|
||||
public Album? Find(Guid id) => context.Albums
|
||||
.AsSplitQuery()
|
||||
.Include(a => a.CoverAsset)
|
||||
.Include(a => a.PersonOwner)
|
||||
.Include(a => a.Assets)
|
||||
@@ -117,6 +118,7 @@ public class AlbumRepository(LactoseDbContext context) : IAlbumRepository {
|
||||
/// <inheritdoc />
|
||||
public Album? FindVisible(Guid id, Guid? userId, EAccessLevel accessLevel) {
|
||||
var album = context.Albums
|
||||
.AsSplitQuery()
|
||||
.Include(a => a.CoverAsset)
|
||||
.Include(a => a.PersonOwner)
|
||||
.Include(a => a.Assets)
|
||||
|
||||
@@ -21,7 +21,9 @@ public class AssetRepository(LactoseDbContext context) : IAssetRepository {
|
||||
>= EAccessLevel.Admin => query.FirstOrDefault(),
|
||||
EAccessLevel.Curator => query.FirstOrDefault(a =>
|
||||
a.DeletedAt == null || a.UploadedBy == userId),
|
||||
EAccessLevel.Maintainer when userId.HasValue => query.Include(a => a.Albums)
|
||||
EAccessLevel.Maintainer when userId.HasValue => query
|
||||
.AsSplitQuery()
|
||||
.Include(a => a.Albums)
|
||||
.FirstOrDefault(a => a.DeletedAt == null && (
|
||||
a.Visibility == EVisibility.Public ||
|
||||
a.Visibility == EVisibility.Protected ||
|
||||
@@ -41,6 +43,7 @@ public class AssetRepository(LactoseDbContext context) : IAssetRepository {
|
||||
|
||||
/// <inheritdoc />
|
||||
public Asset? FindWithAlbums(Guid id) => context.Assets
|
||||
.AsSplitQuery()
|
||||
.Include(a => a.Albums)
|
||||
.FirstOrDefault(a => a.Id == id);
|
||||
|
||||
@@ -83,7 +86,10 @@ public class AssetRepository(LactoseDbContext context) : IAssetRepository {
|
||||
public IEnumerable<Asset> FindByUploader(Guid uploaderId) => context.Assets.Where(a => a.UploadedBy == uploaderId);
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<Asset> FindBulk(IEnumerable<Guid> ids) => context.Assets.Include(a => a.Albums).Where(a => ids.Contains(a.Id));
|
||||
public IEnumerable<Asset> FindBulk(IEnumerable<Guid> ids) => context.Assets
|
||||
.AsSplitQuery()
|
||||
.Include(a => a.Albums)
|
||||
.Where(a => ids.Contains(a.Id));
|
||||
|
||||
/// <inheritdoc />
|
||||
public Asset? FindByPath(string path) => context.Assets.FirstOrDefault(a => a.OriginalPath == path);
|
||||
@@ -205,6 +211,7 @@ public class AssetRepository(LactoseDbContext context) : IAssetRepository {
|
||||
.ToList();
|
||||
|
||||
var assets = context.Assets
|
||||
.AsSplitQuery()
|
||||
.Where(a => shuffledIds.Contains(a.Id))
|
||||
.Include(a => a.Albums!).ThenInclude(a => a.PersonOwner)
|
||||
.ToList();
|
||||
@@ -212,7 +219,9 @@ public class AssetRepository(LactoseDbContext context) : IAssetRepository {
|
||||
return shuffledIds.Select(id => assets.First(a => a.Id == id)).ToList();
|
||||
}
|
||||
|
||||
IQueryable<Asset> dataQuery = query.Include(a => a.Albums!).ThenInclude(a => a.PersonOwner);
|
||||
IQueryable<Asset> dataQuery = query
|
||||
.AsSplitQuery()
|
||||
.Include(a => a.Albums!).ThenInclude(a => a.PersonOwner);
|
||||
dataQuery = orderRandomly ? dataQuery.OrderBy(a => a.Id) : dataQuery.OrderByDescending(a => a.CreatedAt);
|
||||
|
||||
return dataQuery
|
||||
|
||||
@@ -28,7 +28,10 @@ public class MediaRepository(LactoseDbContext context, IPersonRepository personR
|
||||
asset.DeletedAt == null || asset.UploadedBy == userId);
|
||||
|
||||
if (accessLevel == EAccessLevel.Maintainer && userId.HasValue) {
|
||||
var asset = query.Include(a => a.Albums).FirstOrDefault();
|
||||
var asset = query
|
||||
.AsSplitQuery()
|
||||
.Include(a => a.Albums)
|
||||
.FirstOrDefault();
|
||||
if (asset == null || asset.DeletedAt != null) return null;
|
||||
if (asset.Visibility == EVisibility.Public) return asset;
|
||||
if (asset.Visibility == EVisibility.Protected) return asset;
|
||||
|
||||
@@ -17,6 +17,7 @@ public class PersonRepository(LactoseDbContext context) : IPersonRepository {
|
||||
|
||||
/// <inheritdoc />
|
||||
public Person? Find(Guid id) => context.People
|
||||
.AsSplitQuery()
|
||||
.Include(p => p.ProfileAsset)
|
||||
.Include(p => p.Albums)!.ThenInclude(a => a.CoverAsset)
|
||||
.Include(p => p.Albums)!.ThenInclude(a => a.Assets)
|
||||
@@ -27,6 +28,7 @@ public class PersonRepository(LactoseDbContext context) : IPersonRepository {
|
||||
string? albumSearch = null, string? albumSortBy = null, bool albumSortAsc = true,
|
||||
int albumPage = 0, int albumPageSize = PagedParametersDto.MaxPageSize) {
|
||||
var person = context.People
|
||||
.AsSplitQuery()
|
||||
.Include(p => p.ProfileAsset)
|
||||
.Include(p => p.Albums)!.ThenInclude(a => a.CoverAsset)
|
||||
.Include(p => p.Albums)!.ThenInclude(a => a.Assets)
|
||||
@@ -185,6 +187,7 @@ public class PersonRepository(LactoseDbContext context) : IPersonRepository {
|
||||
return [];
|
||||
|
||||
var pagedPeople = context.People
|
||||
.AsSplitQuery()
|
||||
.Include(p => p.Albums)
|
||||
.Where(p => personIds.Contains(p.Id))
|
||||
.ToList();
|
||||
|
||||
Reference in New Issue
Block a user