From 9da138e01e9f1fb47d53d7e8a9d42d98377d39d4 Mon Sep 17 00:00:00 2001 From: Sergey Katsubo Date: Mon, 6 Oct 2025 18:54:07 +0300 Subject: [PATCH] feat(server): improve checkAlbumAccess query performance (#22467) * Fix slow SQL query in checkAlbumAccess caused by the array overlap operator && * Update access.repository.sql * Rewrite the query to pass assetIds once as a single array parameter --- server/src/queries/access.repository.sql | 11 ++++++++++- server/src/repositories/access.repository.ts | 11 +++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/server/src/queries/access.repository.sql b/server/src/queries/access.repository.sql index 9aecaafb52..e98c5c6d98 100644 --- a/server/src/queries/access.repository.sql +++ b/server/src/queries/access.repository.sql @@ -71,6 +71,11 @@ where and "shared_link"."albumId" in ($2) -- AccessRepository.asset.checkAlbumAccess +with + "target" as ( + select + array[$1]::uuid[] as "ids" + ) select "asset"."id", "asset"."livePhotoVideoId" @@ -82,8 +87,12 @@ from left join "album_user" as "albumUsers" on "albumUsers"."albumsId" = "album"."id" left join "user" on "user"."id" = "albumUsers"."usersId" and "user"."deletedAt" is null + cross join "target" where - array["asset"."id", "asset"."livePhotoVideoId"] && array[$1]::uuid[] + ( + "asset"."id" = any (target.ids) + or "asset"."livePhotoVideoId" = any (target.ids) + ) and ( "album"."ownerId" = $2 or "user"."id" = $3 diff --git a/server/src/repositories/access.repository.ts b/server/src/repositories/access.repository.ts index 5cceb6dbe0..ca12ff040b 100644 --- a/server/src/repositories/access.repository.ts +++ b/server/src/repositories/access.repository.ts @@ -136,6 +136,7 @@ class AssetAccess { } return this.db + .with('target', (qb) => qb.selectNoFrom(sql`array[${sql.join([...assetIds])}]::uuid[]`.as('ids'))) .selectFrom('album') .innerJoin('album_asset as albumAssets', 'album.id', 'albumAssets.albumsId') .innerJoin('asset', (join) => @@ -143,11 +144,13 @@ class AssetAccess { ) .leftJoin('album_user as albumUsers', 'albumUsers.albumsId', 'album.id') .leftJoin('user', (join) => join.onRef('user.id', '=', 'albumUsers.usersId').on('user.deletedAt', 'is', null)) + .crossJoin('target') .select(['asset.id', 'asset.livePhotoVideoId']) - .where( - sql`array["asset"."id", "asset"."livePhotoVideoId"]`, - '&&', - sql`array[${sql.join([...assetIds])}]::uuid[] `, + .where((eb) => + eb.or([ + eb('asset.id', '=', sql`any(target.ids)`), + eb('asset.livePhotoVideoId', '=', sql`any(target.ids)`), + ]), ) .where((eb) => eb.or([eb('album.ownerId', '=', userId), eb('user.id', '=', userId)])) .where('album.deletedAt', 'is', null)