Приведенный ниже запрос вызывает другую процедуру getcurrencyexchangerate () .Как это можно назвать один раз и использовать везде?Как ниже условия "CASE WHEN in_currency! = -1 THEN speedy.currency_code = in_currency ELSE TRUE END" можно использовать вне условия where, чтобы повысить производительность?
SELECT
-- LOGINS
COALESCE(sum(
CASE WHEN speedy.base_transaction_type IN ('logins') THEN
speedy.count ELSE 0 END),0) as login,
-- REGISTRATIONS
COALESCE(sum(
CASE WHEN speedy.base_transaction_type IN ('registrations') THEN
speedy.count ELSE 0 END),0) as registration,
-- COMPLETED DEPOSITS
COALESCE(sum(
CASE WHEN speedy.base_transaction_type IN ('manual_deposit', 'deposit') AND
LOWER(speedy.status) IN ('success', 'pending approval') THEN
(speedy.amount_1 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) )
ELSE 0 END),0) as deposit,
-- COMPLETED WITHDRAWS
COALESCE(sum(
CASE WHEN speedy.base_transaction_type IN ('withdraw') AND
LOWER(speedy.status) IN ('success') THEN
(speedy.amount_2 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) )
ELSE 0 END),0) as withdraw,
-- BONUS SUM
COALESCE(sum(
CASE WHEN speedy.base_transaction_type IN ('bonus', 'manual_bonus') AND
LOWER(speedy.status) IN ('success') THEN
(speedy.amount_1 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) )
ELSE 0 END),0) as bonus,
-- MANUAL BONUS SUM
COALESCE(sum(
CASE WHEN speedy.base_transaction_type IN ('manual_bonus') AND
LOWER(speedy.status) IN ('success') THEN
(speedy.amount_1 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) )
ELSE 0 END),0) as manualbonus,
-- BONUS WAGERED
COALESCE(sum(
CASE WHEN speedy.base_transaction_type IN ('bonus_wagered') AND
LOWER(speedy.status) IN ('success') THEN
(speedy.amount_1 * (CASE WHEN in_currency = -1 THEN (SELECT getcurrencyexchangerate(speedy.currency_code)) ELSE 1 END) )
ELSE 0 END),0) as bonuswager
FROM speedy_reports_data AS speedy
WHERE
speedy.for_date BETWEEN '2017-08-31' AND '2018-08-31' AND
(CASE WHEN in_currency != -1 THEN speedy.currency_code = in_currency ELSE TRUE END)
Таблица speedy_reports_data имеет составной ключ для столбцов (for_date, skin_id, country_code, currency_code, is_test, base_transaction_type, status) в том же порядке.Других индексов в этой таблице нет