Skip to content

Commit

Permalink
Merge pull request #930 from CommitChange/optimizations-of-queries
Browse files Browse the repository at this point in the history
Optimization of queries on QuerySupporter
  • Loading branch information
wwahammy authored Sep 25, 2024
2 parents d4f309b + 7de5f43 commit 970abd1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app/legacy_lib/nonprofit_query_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ def supporters
def supporter_notes
Qx.select("supporter_notes.*").from(:supporters).join(:supporter_notes, "supporters.id = supporter_notes.supporter_id")
end


def tag_masters
Qx.select("*").from(:tag_masters).where("nonprofit_id = $id AND NOT deleted", id: @id)
end

def tag_joins_through_supporters
Qx.select("tag_joins.id, tag_joins.supporter_id, tag_joins.tag_master_id")
.from(:tag_joins)
.join(:supporters, "supporters.id = tag_joins.supporter_id")
end
end
13 changes: 3 additions & 10 deletions app/legacy_lib/query_supporters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,18 @@ 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")
.join(:supporters, "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))
.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)
end
Expand All @@ -191,7 +184,6 @@ def self.full_filter_expr(np_id, query)
.from(Qx.select('*').from(:payments).where("nonprofit_id = $id", id: np_id).as(:payments).parse)
.join(Qx.select('id')
.from(:supporters)
.where("supporters.nonprofit_id = $id and deleted != 'true'", id: np_id )
.as("payments_to_supporters"), "payments_to_supporters.id = payments.supporter_id"
)
.as("outer_from_payment_to_supporter")
Expand All @@ -214,6 +206,7 @@ def self.full_filter_expr(np_id, query)
.with(:tag_masters, np_queries.tag_masters)
.with(:supporters, np_queries.supporters)
.with(:supporter_notes, np_queries.supporter_notes)
.with(:tag_joins, np_queries.tag_joins_through_supporters)
.from(:supporters)
.join('nonprofits', 'nonprofits.id=supporters.nonprofit_id')
.left_join(
Expand Down

0 comments on commit 970abd1

Please sign in to comment.