mirror of
https://github.com/immich-app/immich.git
synced 2025-10-31 18:47:09 -04:00
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
This commit is contained in:
parent
1a2a46014e
commit
9da138e01e
@ -71,6 +71,11 @@ where
|
|||||||
and "shared_link"."albumId" in ($2)
|
and "shared_link"."albumId" in ($2)
|
||||||
|
|
||||||
-- AccessRepository.asset.checkAlbumAccess
|
-- AccessRepository.asset.checkAlbumAccess
|
||||||
|
with
|
||||||
|
"target" as (
|
||||||
|
select
|
||||||
|
array[$1]::uuid[] as "ids"
|
||||||
|
)
|
||||||
select
|
select
|
||||||
"asset"."id",
|
"asset"."id",
|
||||||
"asset"."livePhotoVideoId"
|
"asset"."livePhotoVideoId"
|
||||||
@ -82,8 +87,12 @@ from
|
|||||||
left join "album_user" as "albumUsers" on "albumUsers"."albumsId" = "album"."id"
|
left join "album_user" as "albumUsers" on "albumUsers"."albumsId" = "album"."id"
|
||||||
left join "user" on "user"."id" = "albumUsers"."usersId"
|
left join "user" on "user"."id" = "albumUsers"."usersId"
|
||||||
and "user"."deletedAt" is null
|
and "user"."deletedAt" is null
|
||||||
|
cross join "target"
|
||||||
where
|
where
|
||||||
array["asset"."id", "asset"."livePhotoVideoId"] && array[$1]::uuid[]
|
(
|
||||||
|
"asset"."id" = any (target.ids)
|
||||||
|
or "asset"."livePhotoVideoId" = any (target.ids)
|
||||||
|
)
|
||||||
and (
|
and (
|
||||||
"album"."ownerId" = $2
|
"album"."ownerId" = $2
|
||||||
or "user"."id" = $3
|
or "user"."id" = $3
|
||||||
|
|||||||
@ -136,6 +136,7 @@ class AssetAccess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return this.db
|
return this.db
|
||||||
|
.with('target', (qb) => qb.selectNoFrom(sql`array[${sql.join([...assetIds])}]::uuid[]`.as('ids')))
|
||||||
.selectFrom('album')
|
.selectFrom('album')
|
||||||
.innerJoin('album_asset as albumAssets', 'album.id', 'albumAssets.albumsId')
|
.innerJoin('album_asset as albumAssets', 'album.id', 'albumAssets.albumsId')
|
||||||
.innerJoin('asset', (join) =>
|
.innerJoin('asset', (join) =>
|
||||||
@ -143,11 +144,13 @@ class AssetAccess {
|
|||||||
)
|
)
|
||||||
.leftJoin('album_user as albumUsers', 'albumUsers.albumsId', 'album.id')
|
.leftJoin('album_user as albumUsers', 'albumUsers.albumsId', 'album.id')
|
||||||
.leftJoin('user', (join) => join.onRef('user.id', '=', 'albumUsers.usersId').on('user.deletedAt', 'is', null))
|
.leftJoin('user', (join) => join.onRef('user.id', '=', 'albumUsers.usersId').on('user.deletedAt', 'is', null))
|
||||||
|
.crossJoin('target')
|
||||||
.select(['asset.id', 'asset.livePhotoVideoId'])
|
.select(['asset.id', 'asset.livePhotoVideoId'])
|
||||||
.where(
|
.where((eb) =>
|
||||||
sql`array["asset"."id", "asset"."livePhotoVideoId"]`,
|
eb.or([
|
||||||
'&&',
|
eb('asset.id', '=', sql<string>`any(target.ids)`),
|
||||||
sql`array[${sql.join([...assetIds])}]::uuid[] `,
|
eb('asset.livePhotoVideoId', '=', sql<string>`any(target.ids)`),
|
||||||
|
]),
|
||||||
)
|
)
|
||||||
.where((eb) => eb.or([eb('album.ownerId', '=', userId), eb('user.id', '=', userId)]))
|
.where((eb) => eb.or([eb('album.ownerId', '=', userId), eb('user.id', '=', userId)]))
|
||||||
.where('album.deletedAt', 'is', null)
|
.where('album.deletedAt', 'is', null)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user