Skip to content

High performance self-hosted photo and video management solution.

fix(server) long album load time on Album and Sharing page (#1890)

Alex25cff6a74886
diff --git a/server/apps/immich/src/api-v1/album/album-repository.ts b/server/apps/immich/src/api-v1/album/album-repository.ts
index f71d3dfa29..e0cf3d6281 100644
--- a/server/apps/immich/src/api-v1/album/album-repository.ts
+++ b/server/apps/immich/src/api-v1/album/album-repository.ts
@@ -79,6 +79,7 @@ export class AlbumRepository implements IAlbumRepository {
const queryProperties: FindManyOptions<AlbumEntity> = {
relations: { sharedUsers: true, assets: true, sharedLinks: true, owner: true },
+ select: { assets: { id: true } },
order: { assets: { fileCreatedAt: 'ASC' }, createdAt: 'ASC' },
};
@@ -112,10 +113,6 @@ export class AlbumRepository implements IAlbumRepository {
});
}
- const albums = await albumsQuery;
-
- albums.sort((a, b) => new Date(b.createdAt).valueOf() - new Date(a.createdAt).valueOf());
-
return albumsQuery;
}
diff --git a/server/apps/immich/src/api-v1/album/album.service.ts b/server/apps/immich/src/api-v1/album/album.service.ts
index 58d44b45f7..525cf50658 100644
--- a/server/apps/immich/src/api-v1/album/album.service.ts
+++ b/server/apps/immich/src/api-v1/album/album.service.ts
@@ -66,11 +66,11 @@ export class AlbumService {
*/
async getAllAlbums(authUser: AuthUserDto, getAlbumsDto: GetAlbumsDto): Promise<AlbumResponseDto[]> {
let albums: AlbumEntity[];
-
if (typeof getAlbumsDto.assetId === 'string') {
albums = await this.albumRepository.getListByAssetId(authUser.id, getAlbumsDto.assetId);
} else {
albums = await this.albumRepository.getList(authUser.id, getAlbumsDto);
+
if (getAlbumsDto.shared) {
const publicSharingAlbums = await this.albumRepository.getPublicSharingList(authUser.id);
albums = [...albums, ...publicSharingAlbums];
diff --git a/server/libs/infra/src/db/entities/album.entity.ts b/server/libs/infra/src/db/entities/album.entity.ts
index 1bc481f135..565fdf217c 100644
--- a/server/libs/infra/src/db/entities/album.entity.ts
+++ b/server/libs/infra/src/db/entities/album.entity.ts
@@ -18,7 +18,7 @@ export class AlbumEntity {
@PrimaryGeneratedColumn('uuid')
id!: string;
- @ManyToOne(() => UserEntity, { eager: true, onDelete: 'CASCADE', onUpdate: 'CASCADE', nullable: false })
+ @ManyToOne(() => UserEntity, { onDelete: 'CASCADE', onUpdate: 'CASCADE', nullable: false })
owner!: UserEntity;
@Column()
@@ -36,11 +36,11 @@ export class AlbumEntity {
@Column({ comment: 'Asset ID to be used as thumbnail', type: 'varchar', nullable: true })
albumThumbnailAssetId!: string | null;
- @ManyToMany(() => UserEntity, { eager: true })
+ @ManyToMany(() => UserEntity)
@JoinTable()
sharedUsers!: UserEntity[];
- @ManyToMany(() => AssetEntity, { eager: true })
+ @ManyToMany(() => AssetEntity)
@JoinTable()
assets!: AssetEntity[];
diff --git a/server/libs/infra/src/db/entities/asset.entity.ts b/server/libs/infra/src/db/entities/asset.entity.ts
index de8ee22959..6c7b76f15f 100644
--- a/server/libs/infra/src/db/entities/asset.entity.ts
+++ b/server/libs/infra/src/db/entities/asset.entity.ts
@@ -27,7 +27,7 @@ export class AssetEntity {
@Column()
deviceAssetId!: string;
- @ManyToOne(() => UserEntity, { eager: true, onDelete: 'CASCADE', onUpdate: 'CASCADE', nullable: false })
+ @ManyToOne(() => UserEntity, { onDelete: 'CASCADE', onUpdate: 'CASCADE', nullable: false })
owner!: UserEntity;
@Column()
@@ -92,11 +92,11 @@ export class AssetEntity {
@OneToOne(() => SmartInfoEntity, (smartInfoEntity) => smartInfoEntity.asset)
smartInfo?: SmartInfoEntity;
- @ManyToMany(() => TagEntity, (tag) => tag.assets, { cascade: true, eager: true })
+ @ManyToMany(() => TagEntity, (tag) => tag.assets, { cascade: true })
@JoinTable({ name: 'tag_asset' })
tags!: TagEntity[];
- @ManyToMany(() => SharedLinkEntity, (link) => link.assets, { cascade: true, eager: true })
+ @ManyToMany(() => SharedLinkEntity, (link) => link.assets, { cascade: true })
@JoinTable({ name: 'shared_link__asset' })
sharedLinks!: SharedLinkEntity[];
}
diff --git a/server/libs/infra/src/db/entities/shared-link.entity.ts b/server/libs/infra/src/db/entities/shared-link.entity.ts
index 5825d8b080..1995d70f08 100644
--- a/server/libs/infra/src/db/entities/shared-link.entity.ts
+++ b/server/libs/infra/src/db/entities/shared-link.entity.ts
@@ -52,6 +52,7 @@ export class SharedLinkEntity {
@ManyToMany(() => AssetEntity, (asset) => asset.sharedLinks)
assets!: AssetEntity[];
+ @Index('IDX_sharedlink_albumId')
@ManyToOne(() => AlbumEntity, (album) => album.sharedLinks)
album?: AlbumEntity;
}
diff --git a/server/libs/infra/src/db/migrations/1677535643119-AddIndexForAlbumInSharedLinkTable.ts b/server/libs/infra/src/db/migrations/1677535643119-AddIndexForAlbumInSharedLinkTable.ts
new file mode 100644
index 0000000000..f3fb4a6c63
--- /dev/null
+++ b/server/libs/infra/src/db/migrations/1677535643119-AddIndexForAlbumInSharedLinkTable.ts
@@ -0,0 +1,14 @@
+import { MigrationInterface, QueryRunner } from "typeorm";
+
+export class AddIndexForAlbumInSharedLinkTable1677535643119 implements MigrationInterface {
+ name = 'AddIndexForAlbumInSharedLinkTable1677535643119'
+
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(`CREATE INDEX "IDX_sharedlink_albumId" ON "shared_links" ("albumId") `);
+ }
+
+ public async down(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(`DROP INDEX "public"."IDX_sharedlink_albumId"`);
+ }
+
+}
diff --git a/server/libs/infra/src/db/repository/shared-link.repository.ts b/server/libs/infra/src/db/repository/shared-link.repository.ts
index baacc07a50..d999c28f79 100644
--- a/server/libs/infra/src/db/repository/shared-link.repository.ts
+++ b/server/libs/infra/src/db/repository/shared-link.repository.ts
@@ -26,6 +26,7 @@ export class SharedLinkRepository implements ISharedLinkRepository {
assets: {
exifInfo: true,
},
+ owner: true,
},
},
order: {
@@ -49,7 +50,9 @@ export class SharedLinkRepository implements ISharedLinkRepository {
},
relations: {
assets: true,
- album: true,
+ album: {
+ owner: true,
+ },
},
order: {
createdAt: 'DESC',
diff --git a/web/src/routes/(user)/albums/+page.svelte b/web/src/routes/(user)/albums/+page.svelte
index 0b01b04fce..95848d2014 100644
--- a/web/src/routes/(user)/albums/+page.svelte
+++ b/web/src/routes/(user)/albums/+page.svelte
@@ -1,7 +1,6 @@
<script lang="ts">
import AlbumCard from '$lib/components/album-page/album-card.svelte';
import { goto } from '$app/navigation';
- import { onMount } from 'svelte';
import ContextMenu from '$lib/components/shared-components/context-menu/context-menu.svelte';
import MenuOption from '$lib/components/shared-components/context-menu/menu-option.svelte';
import DeleteOutline from 'svelte-material-icons/DeleteOutline.svelte';
@@ -20,13 +19,10 @@
contextMenuPosition,
createAlbum,
deleteSelectedContextAlbum,
- loadAlbums,
showAlbumContextMenu,
closeAlbumContextMenu
} = useAlbums({ albums: data.albums });
- onMount(loadAlbums);
-
const handleCreateAlbum = async () => {
const newAlbum = await createAlbum();
if (newAlbum) {