diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2016-10-16 18:57:54 +0200 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2016-10-16 19:10:16 +0200 |
commit | de1f3aab868f8d18198438c405d0852c58f34e10 (patch) | |
tree | 914ad2285da94197fccaee7b462f3d7013823707 /app/models | |
parent | 1de2833f3045d48ab6d696a109a7a71f6a469135 (diff) |
Fix #16 - Optimize n+1 queries when checking reblogged/favourited values for status lists in API
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/account.rb | 2 | ||||
-rw-r--r-- | app/models/follow_suggestion.rb | 2 | ||||
-rw-r--r-- | app/models/status.rb | 6 |
3 files changed, 6 insertions, 4 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index c1b52ea1b..8eba4da79 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -48,6 +48,8 @@ class Account < ApplicationRecord scope :with_followers, -> { where('(select count(f.id) from follows as f where f.target_account_id = accounts.id) > 0') } scope :expiring, -> (time) { where(subscription_expires_at: nil).or(where('subscription_expires_at < ?', time)).remote.with_followers } + scope :with_counters, -> { select('accounts.*, (select count(f.id) from follows as f where f.target_account_id = accounts.id) as followers_count, (select count(f.id) from follows as f where f.account_id = accounts.id) as following_count, (select count(s.id) from statuses as s where s.account_id = accounts.id) as statuses_count') } + def follow!(other_account) active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account) end diff --git a/app/models/follow_suggestion.rb b/app/models/follow_suggestion.rb index 25d28f5ac..dbe86a0e4 100644 --- a/app/models/follow_suggestion.rb +++ b/app/models/follow_suggestion.rb @@ -22,7 +22,7 @@ END account_ids = results['data'].map(&:first) blocked_ids = Block.where(account_id: for_account_id).pluck(:target_account_id) - accounts_map = Account.where(id: account_ids - blocked_ids).map { |a| [a.id, a] }.to_h + accounts_map = Account.where(id: account_ids - blocked_ids).with_counters.map { |a| [a.id, a] }.to_h account_ids.map { |id| accounts_map[id] }.compact rescue Neography::NeographyError, Excon::Error::Socket => e diff --git a/app/models/status.rb b/app/models/status.rb index 3f150c5de..58b2cb1f3 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -2,7 +2,7 @@ class Status < ApplicationRecord include Paginable include Streamable - belongs_to :account, inverse_of: :statuses + belongs_to :account, -> { with_counters }, inverse_of: :statuses belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs @@ -90,11 +90,11 @@ class Status < ApplicationRecord end def self.favourites_map(status_ids, account_id) - Favourite.where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h + Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h end def self.reblogs_map(status_ids, account_id) - where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h + select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h end before_validation do |