[Spce-user] Fraud calculation takes hours

Rene Krenn rkrenn at sipwise.com
Thu Jan 25 10:45:25 EST 2018


Hi,

thanks for pointing.

Can you try running this modified query below on your test database a few times,
and report back how long it took (longer or less 2 minutes)?

SELECT bpinfo.id, bpinfo.reseller_id, bpinfo.interval, bpinfo.type,
       IF (bpinfo.fraud_use_reseller_rates > 0, 
          SUM(cdr.source_reseller_cost),
          SUM(cdr.source_customer_cost)) as interval_cost,
       bpinfo.interval_limit,
       bpinfo.interval_lock,
       bpinfo.interval_notify,
       bpinfo.fraud_use_reseller_rates as use_reseller_rates
FROM (
  SELECT c.id, n.reseller_id, bp.fraud_use_reseller_rates, i.interval,
    IF (i.interval = 'month',
        IF (cfp.fraud_interval_limit > 0,
            'account_limit', 'profile_limit'),
        IF (cfp.fraud_daily_limit > 0,
            'account_limit', 'profile_limit')
       ) AS type,
    IF (i.interval = 'month',
        IF (cfp.fraud_interval_limit > 0,
            cfp.fraud_interval_limit, bp.fraud_interval_limit),
        IF (cfp.fraud_daily_limit > 0,
            cfp.fraud_daily_limit, bp.fraud_daily_limit)
       ) AS interval_limit,
    IF (i.interval = 'month',
        IF (cfp.fraud_interval_limit > 0,
            cfp.fraud_interval_lock, bp.fraud_interval_lock),
        IF (cfp.fraud_daily_limit > 0,
            cfp.fraud_daily_lock, bp.fraud_daily_lock)
       ) AS interval_lock,
    IF (i.interval = 'month',
        IF (cfp.fraud_interval_limit > 0,
            cfp.fraud_interval_notify, bp.fraud_interval_notify),
        IF (cfp.fraud_daily_limit > 0,
            cfp.fraud_daily_notify, bp.fraud_daily_notify)
       ) AS interval_notify
  FROM (SELECT IF("day" = 'month','month','day') AS 'interval') i,
       billing.contracts c
  INNER JOIN (
    select ca.id as contract_id,
      (select bma.id from billing.billing_mappings bma where
      bma.contract_id=ca.id and
      (bma.end_date >= NOW() OR bma.end_date IS NULL) 
      AND (bma.start_date <= NOW() OR bma.start_date IS NULL) order
      by bma.start_date desc, bma.id desc limit 1) as id from
      billing.contracts ca
   ) AS bm_actual ON bm_actual.contract_id = c.id
  JOIN billing.billing_mappings bm ON bm.id = bm_actual.id
  JOIN billing.billing_profiles bp ON bp.id = bm.billing_profile_id
  JOIN billing.contacts n ON n.id = c.contact_id
  JOIN billing.resellers r ON r.id = n.reseller_id
  LEFT JOIN billing.contract_fraud_preferences cfp 
    ON cfp.contract_id = c.id
  WHERE c.status = 'active'
  HAVING interval_limit > 0
) AS bpinfo
JOIN accounting.cdr ON cdr.source_account_id = bpinfo.id
WHERE CASE WHEN bpinfo.interval = 'month'
  THEN cdr.start_time
    BETWEEN UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-01 00:00:00'))
        AND UNIX_TIMESTAMP(DATE_FORMAT(NOW() + INTERVAL 1 MONTH,
        '%Y-%m-01 00:00:00'))-1
  ELSE cdr.start_time
    BETWEEN UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00'))
        AND UNIX_TIMESTAMP(DATE_FORMAT(NOW() + INTERVAL 1 DAY,
        '%Y-%m-%d 00:00:00'))-1
  END
GROUP BY bpinfo.id
HAVING interval_cost >= interval_limit;




thanks&regards,
rene




More information about the Spce-user mailing list