Comment 7 for bug 1496522

Revision history for this message
Blake GH (bmagic) wrote :

Remington,

I put your view on our production. I found that the query was causing action trigger cron jobs to dog pile. The query was too slow. To fix it in the immediate, I editing the query to only return balance_owed > 0

CREATE OR REPLACE VIEW money.usr_summary_per_org_unit AS
 WITH located_xact AS (
         SELECT circulation.id,
            circulation.circ_lib AS billing_ou
           FROM action.circulation
        UNION
         SELECT grocery.id,
            grocery.billing_location AS billing_ou
           FROM money.grocery
        )
 SELECT mmbts.usr,
    located_xact.billing_ou,
    sum(mmbts.total_paid) AS total_paid,
    sum(mmbts.total_owed) AS total_owed,
    sum(mmbts.balance_owed) AS balance_owed,
    COALESCE(max(mmbts.last_payment_ts), '0001-01-01 00:00:00'::timestamp without time zone::timestamp with time zone) AS last_payment_ts
   FROM money.materialized_billable_xact_summary mmbts
     JOIN located_xact ON located_xact.id = mmbts.id
  WHERE mmbts.balance_owed > 0::numeric
  GROUP BY mmbts.usr, located_xact.billing_ou;

It also cut back on the number of "invalid" action_trigger.event rows.

Of course, it's not the solution unless we change the name of the view to reflect the filter.