From 446d5d282fe8f94d4a476fca8e99949a3d0aa985 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 8 Feb 2022 12:16:26 -0500 Subject: [PATCH] Fix for cdr_with_charge performance --- deploy/cdr_with_charge_faster.sql | 24 ++++++++++++++++++++++++ revert/cdr_with_charge_faster.sql | 22 ++++++++++++++++++++++ sqitch.plan | 3 +++ verify/cdr_with_charge_faster.sql | 7 +++++++ 4 files changed, 56 insertions(+) create mode 100644 deploy/cdr_with_charge_faster.sql create mode 100644 revert/cdr_with_charge_faster.sql create mode 100644 verify/cdr_with_charge_faster.sql diff --git a/deploy/cdr_with_charge_faster.sql b/deploy/cdr_with_charge_faster.sql new file mode 100644 index 0000000..e664a87 --- /dev/null +++ b/deploy/cdr_with_charge_faster.sql @@ -0,0 +1,24 @@ +-- Deploy jmp:cdr_with_charge_faster to pg +-- requires: cdr_with_charge + +BEGIN; + +DROP VIEW cdr_with_charge; + +CREATE VIEW cdr_with_charge AS + SELECT DISTINCT ON (cdr_id, customer_id) + cdr.*, + rate, + (CASE WHEN cdr.disposition = 'VOICEMAIL' THEN 0 + ELSE rate * ceiling(billsec / 60.0) END) AS charge + FROM + cdr + INNER JOIN customer_plans USING (customer_id) + LEFT JOIN call_rates ON + cdr.direction = call_rates.direction AND + customer_plans.plan_name = call_rates.plan_name AND + cdr.tel LIKE call_rates.prefix || '%' + ORDER BY cdr_id, customer_id, prefix DESC; + + +COMMIT; diff --git a/revert/cdr_with_charge_faster.sql b/revert/cdr_with_charge_faster.sql new file mode 100644 index 0000000..73f370b --- /dev/null +++ b/revert/cdr_with_charge_faster.sql @@ -0,0 +1,22 @@ +-- Revert jmp:cdr_with_charge_faster from pg + +BEGIN; + +DROP VIEW cdr_with_charge; + +CREATE VIEW cdr_with_charge AS + SELECT DISTINCT ON (cdr_id) + cdr.*, + rate, + (CASE WHEN cdr.disposition = 'VOICEMAIL' THEN 0 + ELSE rate * ceiling(billsec / 60.0) END) AS charge + FROM + cdr + INNER JOIN customer_plans USING (customer_id) + LEFT JOIN call_rates ON + cdr.direction = call_rates.direction AND + customer_plans.plan_name = call_rates.plan_name AND + cdr.tel LIKE call_rates.prefix || '%' + ORDER BY cdr_id, prefix DESC; + +COMMIT; diff --git a/sqitch.plan b/sqitch.plan index 6a0d870..6d37a07 100644 --- a/sqitch.plan +++ b/sqitch.plan @@ -23,3 +23,6 @@ cdr_charge [customer_plans plans cdr_with_charge] 2021-11-17T18:09:49Z Stephen P insert_charge_for_cdr [cdr_charge cdr_with_charge transactions] 2021-11-23T14:41:15Z Stephen Paul Weber,,, # Trigger to insert a transaction for the charge for this CDR notify_low_balance [transactions balances] 2021-12-22T00:36:03Z Stephen Paul Weber,,, # NOTIFY when balance dips low @2022039 2022-02-08T15:35:37Z Stephen Paul Weber,,, # Call limits and billing rollout + +cdr_with_charge_faster [cdr_with_charge] 2022-02-08T17:12:16Z Stephen Paul Weber,,, # Allow Postgres to push down WHERE over customer_id +@2022039.1 2022-02-08T17:16:11Z Stephen Paul Weber,,, # Fix for cdr_with_charge performance diff --git a/verify/cdr_with_charge_faster.sql b/verify/cdr_with_charge_faster.sql new file mode 100644 index 0000000..3fa280e --- /dev/null +++ b/verify/cdr_with_charge_faster.sql @@ -0,0 +1,7 @@ +-- Verify jmp:cdr_with_charge_faster on pg + +BEGIN; + +-- Check verify/cdr_with_charge.sql + +ROLLBACK; -- 2.45.2