Level 75
Just a suggestion but consider binding your parameters.
2 likes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have written this query but it is so slow. Please help me optimize it
if (request()->filled('starting_date') && request()->filled('ending_date')) {
$startingDate = Carbon::parse(request()->input('starting_date'))->startOfDay();
$endingDate = Carbon::parse(request()->input('ending_date'))->endOfDay();
$dateQuery = " AND tr_date BETWEEN '" . $startingDate . "' AND '" . $endingDate . "'";
}
if (request()->filled('geo_filter')) {
$geoQuery = " AND tui_bill_country = '" . request()->geo_filter . "'";
}
if (request()->filled('products_filter')) {
$productQuery = " AND product_title = '" . request()->products_filter . "'";
}
if (request()->filled('sub_affiliates_filter')) {
$subAffiliateQuery = " AND tui_udf01 = '" . request()->sub_affiliates_filter . "'";
}
if (request()->filled('affiliates_filter')) {
$affiliateQuery = " AND affiliate = '" . request()->affiliates_filter . "'";
}
$queryResults = DB::select("WITH
allRecords AS (
SELECT id, cust_id_ext, tr_amount_usd
FROM tbl_rg_transactions
WHERE ttype_name IN ('AUTH_ONLY', 'SALE', '3D_LOOKUP')
AND attempt_no = 1
AND respc_id IN (0, 1, 2, 3, 4, 5)
AND tstate_name IN ('CONFIRMED', 'TICKETED', 'SELECTED', 'SETTLED', 'CREDITED', 'PARTIAL', 'PARTIAL-FAIL', 'POSTING', 'CREDIT-PEND')
$dateQuery
$geoQuery
$productQuery
$subAffiliateQuery
$affiliateQuery
),
totalSuccess AS (
SELECT id, cust_id_ext, tr_amount_usd
FROM tbl_rg_transactions
WHERE cust_id_ext IN (SELECT cust_id_ext FROM allRecords)
AND respc_id = 0
AND ttype_name = 'SALE'
AND tstate_name IN ('CONFIRMED', 'TICKETED', 'SELECTED', 'SETTLED', 'CREDITED', 'PARTIAL', 'PARTIAL-FAIL', 'POSTING', 'CREDIT-PEND')
),
cardUpdaterCount AS (
SELECT card_updater_count
FROM tbl_rg_transactions
WHERE card_updater_count > 0
AND cust_id_ext IN (SELECT cust_id_ext FROM allRecords)
),
lookup3ds AS (
SELECT id
FROM tbl_rg_transactions
WHERE cust_id_ext IN (SELECT cust_id_ext FROM allRecords)
AND ttype_name = '3D_LOOKUP'
),
alerts AS (
SELECT ethoca_refund_fee_usd
FROM tbl_rg_transactions
WHERE cust_id_ext IN (SELECT cust_id_ext FROM allRecords)
AND is_ethoca_refunded = 1
),
authOnly AS (
SELECT id
FROM tbl_rg_transactions
WHERE cust_id_ext IN (SELECT cust_id_ext FROM allRecords)
AND ttype_name = 'AUTH_ONLY'
),
chargeBacks AS (
SELECT tr_amount_usd, bank_chargeback_fee_usd
FROM tbl_rg_transactions
WHERE cust_id_ext IN (SELECT cust_id_ext FROM allRecords)
AND respc_id = 0
AND is_chargeback = 1
),
refunds AS (
SELECT id, tr_amount_usd
FROM tbl_rg_transactions
WHERE cust_id_ext IN (SELECT cust_id_ext FROM allRecords)
AND respc_id = 0
AND ttype_name = 'CREDIT'
AND tstate_name IN ('CONFIRMED', 'TICKETED', 'SELECTED', 'SETTLED', 'CREDITED', 'PARTIAL', 'PARTIAL-FAIL', 'POSTING', 'CREDIT-PEND'
)
),
totalFailed AS (
SELECT id
FROM tbl_rg_transactions
WHERE cust_id_ext IN (SELECT cust_id_ext FROM allRecords)
AND respc_id IN (1, 2, 3, 4, 5)
AND ttype_name IN ('AUTH_ONLY', 'SALE')
)
SELECT
-- (SELECT count(distinct cust_id_ext) FROM totalSuccess) AS totalInitialsCount,
(SELECT count(ethoca_refund_fee_usd) FROM alerts) AS totalAlertsCount,
(SELECT sum(ethoca_refund_fee_usd) FROM alerts) AS totalAlertsFee,
(SELECT count(cust_id_ext) FROM totalSuccess) AS totalSuccessCount,
(SELECT sum(tr_amount_usd) FROM totalSuccess) AS totalGrossIncome,
(SELECT count(id) FROM lookup3ds) AS lookup3ds,
(SELECT sum(card_updater_count) FROM cardUpdaterCount) AS cardUpdaterCount,
(SELECT count(id) FROM authOnly) AS authOnly,
(SELECT sum(tr_amount_usd) FROM chargeBacks) AS chargeBacksAmount,
(SELECT count(tr_amount_usd) FROM chargeBacks) AS chargeBacksCount,
(SELECT sum(bank_chargeback_fee_usd) FROM chargeBacks) AS chargeBackFee,
(SELECT count(id) FROM refunds) AS refundCount,
(SELECT sum(tr_amount_usd) FROM refunds) AS refundAmount,
(SELECT count(id) FROM totalFailed) AS totalFailed
");
Please or to participate in this conversation.