Skip to content

Commit

Permalink
Merge pull request #921 from CommitChange/optimization-for-tag_joins
Browse files Browse the repository at this point in the history
This should optimize a bit
  • Loading branch information
wwahammy authored Sep 23, 2024
2 parents 0e66e4c + 348f446 commit ddfbcd4
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions app/legacy_lib/query_supporters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,28 @@ def self._full_search(np_id, query)
# return new_supporters
# end

def self.undeleted_supporters(np_id)

Qx.select('id')
.from(:supporters)
.where("supporters.nonprofit_id = $id and deleted != 'true'", id: np_id )
end

def self.tag_joins_only_for_nonprofit_query(np_id)
Qx.select("tag_joins.id, tag_joins.supporter_id, tag_joins.tag_master_id")
.from(:tag_joins)
.join(undeleted_supporters(np_id).as('tags_to_supporters'), "tags_to_supporters.id = tag_joins.supporter_id")
end

def self.build_tag_query(np_id)

tags_query = Qx.select("tag_joins.supporter_id", "ARRAY_AGG(tag_masters.id) AS ids", "ARRAY_AGG(tag_masters.name::text) AS names")
.from(tag_joins_only_for_nonprofit_query(np_id).as(:tag_joins))
.join(:tag_masters, "tag_masters.id=tag_joins.tag_master_id")
.where("tag_masters.nonprofit_id = $id AND NOT tag_masters.deleted ", id: np_id.to_i)
.group_by("tag_joins.supporter_id")
.as(:tags)
end

# Perform all filters and search for /nonprofits/id/supporters dashboard and export
def self.full_filter_expr(np_id, query)
Expand All @@ -178,12 +200,14 @@ def self.full_filter_expr(np_id, query)
.group_by(:supporter_id)
.as(:payments)

tags_subquery = Qx.select("tag_joins.supporter_id", "ARRAY_AGG(tag_masters.id) AS ids", "ARRAY_AGG(tag_masters.name::text) AS names")
.from(:tag_joins)
.join(:tag_masters, "tag_masters.id=tag_joins.tag_master_id")
.where("tag_masters.nonprofit_id = $id AND NOT tag_masters.deleted", id: np_id.to_i)
.group_by("tag_joins.supporter_id")
.as(:tags)
# tags_subquery = Qx.select("tag_joins.supporter_id", "ARRAY_AGG(tag_masters.id) AS ids", "ARRAY_AGG(tag_masters.name::text) AS names")
# .from(:tag_joins)
# .join(:tag_masters, "tag_masters.id=tag_joins.tag_master_id")
# .where("tag_masters.nonprofit_id = $id AND NOT tag_masters.deleted", id: np_id.to_i)
# .group_by("tag_joins.supporter_id")
# .as(:tags)

tags_subquery = build_tag_query(np_id)

expr = Qx.select('supporters.id')
.from(:supporters)
Expand Down

0 comments on commit ddfbcd4

Please sign in to comment.