Skip to content

Commit

Permalink
perf(APIv2): self-joining in aggregation with a reduced (pundit) scope
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Nov 26, 2024
1 parent 10ba39b commit 2329e6f
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/concerns/v2/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ def join_aggregated(relation)
# INNER JOIN (
# SELECT "resource"."id", AGG("association"."XY"), AGG("association"."XZ")
# FROM "resource" LEFT OUTER JOIN "association" ...
# GROUP BY "resource"."id" WHERE pundit_scope()
# ) "aggregate_association" ON "aggregate_association"."id" = "resource"."id";
# ```
#
def subquery_fragment(association, aliases)
sq = resource.left_outer_joins(association)
.group(resource.primary_key)
.merge_with_alias(pundit_scope(resource))
.select(resource.primary_key, *aliases)
resource.arel_self_join(sq.arel.as(association.to_s))
end
Expand Down

0 comments on commit 2329e6f

Please sign in to comment.