From 317d8703ca506394af1adf5f72b1c66c83bb704b Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Thu, 6 Jun 2019 21:33:30 -0500 Subject: [PATCH] Optimize query for pulling popular videos --- src/invidious/helpers/jobs.cr | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/invidious/helpers/jobs.cr b/src/invidious/helpers/jobs.cr index 63f7c16f8..ee468d1ad 100644 --- a/src/invidious/helpers/jobs.cr +++ b/src/invidious/helpers/jobs.cr @@ -200,13 +200,10 @@ end def pull_popular_videos(db) loop do - subscriptions = db.query_all("SELECT channel FROM \ - (SELECT UNNEST(subscriptions) AS channel FROM users) AS d \ - GROUP BY channel ORDER BY COUNT(channel) DESC LIMIT 40", as: String) - - videos = db.query_all("SELECT DISTINCT ON (ucid) * FROM \ - channel_videos WHERE ucid IN (#{arg_array(subscriptions)}) \ - ORDER BY ucid, published DESC", subscriptions, as: ChannelVideo).sort_by { |video| video.published }.reverse + videos = db.query_all("SELECT DISTINCT ON (ucid) * FROM channel_videos WHERE ucid IN \ + (SELECT channel FROM (SELECT UNNEST(subscriptions) AS channel FROM users) AS d \ + GROUP BY channel ORDER BY COUNT(channel) DESC LIMIT 40) \ + ORDER BY ucid, published DESC", as: ChannelVideo).sort_by { |video| video.published }.reverse yield videos sleep 1.minute