@matigo Moving to caching the query results qualifies as a trick to me. 😆 So much of performance tuning can be very sensitive to access patterns like that.
Did you create a table and keep it up to date with triggers, or would a materialized view be workable?