Optimize query for creating subscription feeds

This commit is contained in:
Omar Roth 2019-06-02 11:48:18 -05:00
parent 71bf8b6b4d
commit 108648b427
No known key found for this signature in database
GPG Key ID: B8254FB7EC3D37F2
3 changed files with 17 additions and 12 deletions

View File

@ -1224,9 +1224,9 @@ post "/login" do |env|
view_name = "subscriptions_#{sha256(user.email)}" view_name = "subscriptions_#{sha256(user.email)}"
PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ PG_DB.exec("CREATE MATERIALIZED VIEW #{view_name} AS \
SELECT * FROM channel_videos WHERE \ SELECT * FROM channel_videos WHERE
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')::text[]) \ ucid IN (SELECT unnest(subscriptions) FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')
ORDER BY published DESC;") ORDER BY published DESC")
if Kemal.config.ssl || config.https_only if Kemal.config.ssl || config.https_only
secure = true secure = true

View File

@ -74,6 +74,11 @@ def refresh_feeds(db, logger, config)
end end
end end
if db.query_one("SELECT pg_get_viewdef('#{view_name}')", as: String).includes? "ucid = ANY"
logger.write("Materialized view #{view_name} is out-of-date, recreating...\n")
db.exec("DROP MATERIALIZED VIEW #{view_name}")
end
db.exec("REFRESH MATERIALIZED VIEW #{view_name}") db.exec("REFRESH MATERIALIZED VIEW #{view_name}")
db.exec("UPDATE users SET feed_needs_update = false WHERE email = $1", email) db.exec("UPDATE users SET feed_needs_update = false WHERE email = $1", email)
rescue ex rescue ex
@ -90,9 +95,9 @@ def refresh_feeds(db, logger, config)
if db.query_one?("SELECT true FROM users WHERE email = $1", email, as: Bool) if db.query_one?("SELECT true FROM users WHERE email = $1", email, as: Bool)
logger.write("CREATE #{view_name}\n") logger.write("CREATE #{view_name}\n")
db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \
SELECT * FROM channel_videos WHERE \ SELECT * FROM channel_videos WHERE
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{email.gsub("'", "\\'")}')::text[]) \ ucid IN (SELECT unnest(subscriptions) FROM users WHERE email = E'#{email.gsub("'", "\\'")}')
ORDER BY published DESC;") ORDER BY published DESC")
db.exec("UPDATE users SET feed_needs_update = false WHERE email = $1", email) db.exec("UPDATE users SET feed_needs_update = false WHERE email = $1", email)
end end
rescue ex rescue ex

View File

@ -149,9 +149,9 @@ def get_user(sid, headers, db, refresh = true)
begin begin
view_name = "subscriptions_#{sha256(user.email)}" view_name = "subscriptions_#{sha256(user.email)}"
db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \
SELECT * FROM channel_videos WHERE \ SELECT * FROM channel_videos WHERE
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')::text[]) \ ucid IN (SELECT unnest(subscriptions) FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')
ORDER BY published DESC;") ORDER BY published DESC")
rescue ex rescue ex
end end
end end
@ -171,9 +171,9 @@ def get_user(sid, headers, db, refresh = true)
begin begin
view_name = "subscriptions_#{sha256(user.email)}" view_name = "subscriptions_#{sha256(user.email)}"
db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \ db.exec("CREATE MATERIALIZED VIEW #{view_name} AS \
SELECT * FROM channel_videos WHERE \ SELECT * FROM channel_videos WHERE
ucid = ANY ((SELECT subscriptions FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')::text[]) \ ucid IN (SELECT unnest(subscriptions) FROM users WHERE email = E'#{user.email.gsub("'", "\\'")}')
ORDER BY published DESC;") ORDER BY published DESC")
rescue ex rescue ex
end end
end end