下面的查询执行需要 0.6748 秒,但是当有许多用户同时尝试执行相同的活动时,进程会变得非常慢,执行大约需要 30 秒。
服务器带宽:7TB
我曾多次尝试优化查询并提高性能,但随着数据库变大,性能再次下降,我别无选择。
请协助帮助进一步优化我的查询
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