The query below takes 0.6748seconds to execute, but when there are many users attempting to do the same activity at the same time, the processes becomes very slow and takes about 30seconds to execute.
Server Bandwidth: 7TB
I have made several attempts to optimize the query and the performance improved, but as the database grew larger, the performance has declined again, I am left with no more option to try.
Please assist to help optimize my query further
SELECT * FROM (SELECT * FROM
(SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated , a.id ,a.user_unique_id ,a.loan_location ,a.ippis ,a.tel_no ,a.organisation ,a.branch ,a.loan_agree ,a.loan_type ,a.appr ,a.sold ,a.loan_status , a.channel, a.top_up ,a.current_loan, a.retrieved_customer_bal ,a.date_created ,a.date_updated
FROM
loan_applications_tbl a
LEFT JOIN
topup_or_reapplication_tbl AS c
ON a.ippis = c.ippis
WHERE
(
(
(
(
a.current_loan = '0'
AND a.loan_status IN (
'Approved', 'Closed'
)
)
)
AND (
((a.loan_status='pending'
AND a.appr < 500000) OR ( a.loan_status IN ( 'corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL' ) ))
)
AND (
(
MONTH(a.date_updated) = MONTH(CURRENT_DATE)
AND YEAR(a.date_updated) = YEAR(CURRENT_DATE)
)
)
)
)
UNION ALL
SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated, b.loan_id ,b.user_unique_id ,b.loan_location ,b.ippis ,b.tel_no ,b.organisation ,b.branch ,b.loan_agree ,b.loan_type ,b.appr ,b.sold ,b.loan_status ,b.channel, b.top_up ,b.current_loan, b.retrieved_customer_bal ,b.date_created ,b.date_updated
FROM
loan_applications_tbl_dump b
LEFT JOIN
topup_or_reapplication_tbl c
ON b.ippis = c.ippis
WHERE
(
(
(
(
b.current_loan = '0'
AND b.loan_status IN (
'Approved', 'Closed'
)
)
)
AND (
((b.loan_status='pending'
AND b.appr < 500000) OR (b.loan_status IN ( 'corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL' ) ))
)
AND (
(
MONTH(b.date_updated) = MONTH(CURRENT_DATE)
AND YEAR(b.date_updated) = YEAR(CURRENT_DATE)
)
)
)
)
UNION DISTINCT
SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated , a.id ,a.user_unique_id ,a.loan_location ,a.ippis ,a.tel_no ,a.organisation ,a.branch ,a.loan_agree ,a.loan_type ,a.appr ,a.sold ,a.loan_status , a.channel, a.top_up ,a.current_loan, a.retrieved_customer_bal ,a.date_created ,a.date_updated
FROM
loan_applications_tbl a
INNER JOIN
topup_or_reapplication_tbl AS c
ON a.ippis = c.ippis
WHERE
(
(
c.current_loan = '1'
AND (
((c.status='pending'
AND c.top_up_approved < 500000) OR (c.status IN ('corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL') ))
)
AND (
(
MONTH(c.date_updated) = MONTH(CURRENT_DATE)
AND YEAR(c.date_updated) = YEAR(CURRENT_DATE)
)
)
)
)
UNION
DISTINCT SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated, b.loan_id ,b.user_unique_id ,b.loan_location ,b.ippis ,b.tel_no ,b.organisation ,b.branch ,b.loan_agree ,b.loan_type ,b.appr ,b.sold ,b.loan_status ,b.channel, b.top_up ,b.current_loan, b.retrieved_customer_bal ,b.date_created ,b.date_updated
FROM
loan_applications_tbl_dump b
INNER JOIN
topup_or_reapplication_tbl c
ON b.ippis = c.ippis
WHERE
(
(
c.current_loan = '1'
AND (
((c.status='pending'
AND c.top_up_approved < 500000) OR (c.status IN ('corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL') ))
)
AND (
(
MONTH(c.date_updated) = MONTH(CURRENT_DATE)
AND YEAR(c.date_updated) = YEAR(CURRENT_DATE)
)
)
)
)
UNION
DISTINCT SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated , a.id ,a.user_unique_id ,a.loan_location ,a.ippis ,a.tel_no ,a.organisation ,a.branch ,a.loan_agree ,a.loan_type ,a.appr ,a.sold ,a.loan_status , a.channel, a.top_up ,a.current_loan, a.retrieved_customer_bal ,a.date_created ,a.date_updated
FROM
loan_applications_tbl a
LEFT JOIN
topup_or_reapplication_tbl AS c
ON a.ippis = c.ippis
WHERE
(
(
(
a.current_loan = '1'
)
AND (
((a.loan_status='pending'
AND a.appr < 500000) OR ( a.loan_status IN ( 'corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL' ) ))
)
AND (
(
MONTH(a.date_updated) = MONTH(CURRENT_DATE)
AND YEAR(a.date_updated) = YEAR(CURRENT_DATE)
)
)
)
)
UNION
DISTINCT SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated, b.loan_id ,b.user_unique_id ,b.loan_location ,b.ippis ,b.tel_no ,b.organisation ,b.branch ,b.loan_agree ,b.loan_type ,b.appr ,b.sold ,b.loan_status ,b.channel, b.top_up ,b.current_loan, b.retrieved_customer_bal ,b.date_created ,b.date_updated
FROM
loan_applications_tbl_dump b
LEFT JOIN
topup_or_reapplication_tbl c
ON b.ippis = c.ippis
WHERE
(
(
(
b.current_loan = '1'
)
AND (
((b.loan_status='pending'
AND b.appr < 500000) OR (b.loan_status IN ( 'corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL' ) ))
)
AND (
(
MONTH(b.date_updated) = MONTH(CURRENT_DATE)
AND YEAR(b.date_updated) = YEAR(CURRENT_DATE)
)
)
)
)
UNION
DISTINCT SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated , a.id ,a.user_unique_id ,a.loan_location ,a.ippis ,a.tel_no ,a.organisation ,a.branch ,a.loan_agree ,a.loan_type ,a.appr ,a.sold ,a.loan_status , a.channel, a.top_up ,a.current_loan, a.retrieved_customer_bal ,a.date_created ,a.date_updated
FROM
loan_applications_tbl a
INNER JOIN
topup_or_reapplication_tbl AS c
ON a.ippis = c.ippis
WHERE
(
(
c.current_loan = '1'
AND (
((c.status='pending'
AND c.top_up_approved < 500000) OR (c.status IN ('corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL') ))
)
AND (
(
MONTH(c.date_created) = MONTH(CURRENT_DATE)
AND YEAR(c.date_created) = YEAR(CURRENT_DATE)
)
)
)
)
UNION
DISTINCT SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated, b.loan_id ,b.user_unique_id ,b.loan_location ,b.ippis ,b.tel_no ,b.organisation ,b.branch ,b.loan_agree ,b.loan_type ,b.appr ,b.sold ,b.loan_status ,b.channel, b.top_up ,b.current_loan, b.retrieved_customer_bal ,b.date_created ,b.date_updated
FROM
loan_applications_tbl_dump b
INNER JOIN
topup_or_reapplication_tbl c
ON b.ippis = c.ippis
WHERE
(
(
c.current_loan = '1'
AND (
((c.status='pending'
AND c.top_up_approved < 500000) OR (c.status IN ('corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL') ))
)
AND (
(
MONTH(c.date_created) = MONTH(CURRENT_DATE)
AND YEAR(c.date_created) = YEAR(CURRENT_DATE)
)
)
)
)
UNION
DISTINCT SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated , a.id ,a.user_unique_id ,a.loan_location ,a.ippis ,a.tel_no ,a.organisation ,a.branch ,a.loan_agree ,a.loan_type ,a.appr ,a.sold ,a.loan_status , a.channel, a.top_up ,a.current_loan, a.retrieved_customer_bal ,a.date_created ,a.date_updated
FROM
loan_applications_tbl a
LEFT JOIN
topup_or_reapplication_tbl AS c
ON a.ippis = c.ippis
WHERE
(
(
(
(
a.current_loan = '0'
AND a.loan_status IN (
'Approved', 'Closed'
)
)
)
AND (
((a.loan_status='pending'
AND a.appr < 500000) OR ( a.loan_status IN ( 'corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL' ) ))
)
AND (
(
MONTH(a.date_created) = MONTH(CURRENT_DATE)
AND YEAR(a.date_created) = YEAR(CURRENT_DATE)
)
)
)
)
UNION
DISTINCT SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated, b.loan_id ,b.user_unique_id ,b.loan_location ,b.ippis ,b.tel_no ,b.organisation ,b.branch ,b.loan_agree ,b.loan_type ,b.appr ,b.sold ,b.loan_status ,b.channel, b.top_up ,b.current_loan, b.retrieved_customer_bal ,b.date_created ,b.date_updated
FROM
loan_applications_tbl_dump b
LEFT JOIN
topup_or_reapplication_tbl c
ON b.ippis = c.ippis
WHERE
(
(
(
(
b.current_loan = '0'
AND b.loan_status IN (
'Approved', 'Closed'
)
)
)
AND (
((b.loan_status='pending'
AND b.appr < 500000) OR (b.loan_status IN ( 'corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL' ) ))
)
AND (
(
MONTH(b.date_created) = MONTH(CURRENT_DATE)
AND YEAR(b.date_created) = YEAR(CURRENT_DATE)
)
)
)
)
UNION
DISTINCT SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated , a.id ,a.user_unique_id ,a.loan_location ,a.ippis ,a.tel_no ,a.organisation ,a.branch ,a.loan_agree ,a.loan_type ,a.appr ,a.sold ,a.loan_status , a.channel, a.top_up ,a.current_loan, a.retrieved_customer_bal ,a.date_created ,a.date_updated
FROM
loan_applications_tbl a
LEFT JOIN
topup_or_reapplication_tbl AS c
ON a.ippis = c.ippis
WHERE
(
(
(
a.current_loan = '1'
)
AND (
((a.loan_status='pending'
AND a.appr < 500000) OR ( a.loan_status IN ( 'corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL' ) ))
)
AND (
(
MONTH(a.date_created) = MONTH(CURRENT_DATE)
AND YEAR(a.date_created) = YEAR(CURRENT_DATE)
)
)
)
)
UNION
DISTINCT SELECT
c.loan_id ,c.channel tu_channel, c.user_unique_id tu_user_unique_id ,c.ippis tu_ippis ,c.top_up_approved, c.ret_customer_bal ,c.loan_type tu_loan_type ,c.dse ,c.status ,c.current_loan tu_current_loan ,c.record_category ,c.date_created tu_date_created ,c.date_updated tu_date_updated, b.loan_id ,b.user_unique_id ,b.loan_location ,b.ippis ,b.tel_no ,b.organisation ,b.branch ,b.loan_agree ,b.loan_type ,b.appr ,b.sold ,b.loan_status ,b.channel, b.top_up ,b.current_loan, b.retrieved_customer_bal ,b.date_created ,b.date_updated
FROM
loan_applications_tbl_dump b
LEFT JOIN
topup_or_reapplication_tbl c
ON b.ippis = c.ippis
WHERE
(
(
(
b.current_loan = '1'
)
AND (
((b.loan_status='pending'
AND b.appr < 500000) OR (b.loan_status IN ( 'corrected', 'Approved By Group Head', 'NIL',
'NIL', 'NIL', 'NIL' ) ))
)
AND (
(
MONTH(b.date_created) = MONTH(CURRENT_DATE)
AND YEAR(b.date_created) = YEAR(CURRENT_DATE)
)
)
)
)
) t3 ORDER BY t3.date_updated, t3.tu_date_updated DESC LIMIT 18446744073709551615) AS t4 GROUP BY t4.ippis