From 625db7dbe71a7315921562bfab82420c04aa6c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Mon, 18 Sep 2023 21:15:37 +0200 Subject: [PATCH] fix: slow query taking up 66% of db time (#298) Query was scanning the full index on listen table, when we really only need data from the last hour --- src/sources/spotify/spotify.service.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/sources/spotify/spotify.service.ts b/src/sources/spotify/spotify.service.ts index dbb309b..28d31ba 100644 --- a/src/sources/spotify/spotify.service.ts +++ b/src/sources/spotify/spotify.service.ts @@ -42,6 +42,10 @@ export class SpotifyService { private readonly spotifyAuth: SpotifyAuthService, ) {} + /** + * Returns a list of users that should be crawled for new listens. + * Only returns the lastListen date if it was within the last hour. + */ @Span() async getCrawlableUserInfo(): Promise< { userID: string; lastListen: Date }[] @@ -57,6 +61,7 @@ export class SpotifyService { .select(`listen."userId"`) .addSelect(`listen."playedAt"`) .from("listen", "listen") + .where(`listen."playedAt" > now() - interval '1 hour'`) .orderBy("listen.userId", "DESC") .addOrderBy("listen.playedAt", "DESC"), "listen",