Skip to content

Instantly share code, notes, and snippets.

@sauloperez
Last active January 7, 2021 11:34
Show Gist options
  • Save sauloperez/fc91d4eb874a63643cb8eb6976a16814 to your computer and use it in GitHub Desktop.
Save sauloperez/fc91d4eb874a63643cb8eb6976a16814 to your computer and use it in GitHub Desktop.
Skip unnecessary join. To be applied on top of https://gist.github.com/sauloperez/8fbcedd7824ec39af7631fa061e16a7c
commit 74fb66e93619074dda3bd98cfe935c4ced30143f
Author: Pau Perez <[email protected]>
Date: Thu Jan 7 12:29:40 2021 +0100
Count occurrences of foreign key instead of join
As we just care for the count of users and not about any of their
details there's no need to join with the users table. The foreign key
column is enough to count.
diff --git a/app/queries/decidim/action_delegator/delegates_votes.rb b/app/queries/decidim/action_delegator/delegates_votes.rb
index 1a04a59..c1a5797 100644
--- a/app/queries/decidim/action_delegator/delegates_votes.rb
+++ b/app/queries/decidim/action_delegator/delegates_votes.rb
@@ -4,10 +4,8 @@ module Decidim
module ActionDelegator
class DelegatesVotes < Rectify::Query
def query
- Decidim::User
- .joins("INNER JOIN decidim_action_delegator_delegations
- ON decidim_users.id = decidim_action_delegator_delegations.granter_id
- INNER JOIN decidim_consultations_votes
+ Decidim::ActionDelegator::Delegation
+ .joins("INNER JOIN decidim_consultations_votes
ON decidim_consultations_votes.decidim_author_id = decidim_action_delegator_delegations.granter_id")
end
diff --git a/app/queries/decidim/action_delegator/delegates_votes_by_question.rb b/app/queries/decidim/action_delegator/delegates_votes_by_question.rb
index 27f6c23..11c1b35 100644
--- a/app/queries/decidim/action_delegator/delegates_votes_by_question.rb
+++ b/app/queries/decidim/action_delegator/delegates_votes_by_question.rb
@@ -9,7 +9,7 @@ module Decidim
end
def query
- relation.new.query.merge(question.votes).distinct.count
+ relation.new.query.merge(question.votes).distinct.count(:granter_id)
end
private
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment