问题描述
我已经阅读了很多关于如何在 sitepoint 和 MysqL 文档网站上使用 EXPLAIN 和索引的文档。甚至找到了一些PDF。但是我的查询似乎比我遇到的所有示例都复杂。近 1 周以来,我一直在尝试优化我的查询以提高工作效率。
截至目前,11000 条记录的执行时间为 56 秒。
有人可以帮助重写我的查询或指导我如何使用索引吗?请参阅下面的代码:
SELECT t3.*,topup_or_reapplication_tbl.loan_id,topup_or_reapplication_tbl.user_unique_id AS tu_user_unique_id,topup_or_reapplication_tbl.ippis AS tu_ippis,topup_or_reapplication_tbl.top_up_approved,topup_or_reapplication_tbl.loan_type AS tu_loan_type,topup_or_reapplication_tbl.dse,topup_or_reapplication_tbl.status,topup_or_reapplication_tbl.current_loan AS tu_current_loan,topup_or_reapplication_tbl.record_category,topup_or_reapplication_tbl.date_created AS tu_date_created,topup_or_reapplication_tbl.date_updated AS tu_date_updated
FROM (
SELECT loan_applications_tbl.id,loan_applications_tbl.user_unique_id,loan_applications_tbl.loan_location,loan_applications_tbl.ippis,loan_applications_tbl.tel_no,loan_applications_tbl.organisation,loan_applications_tbl.branch,loan_applications_tbl.loan_agree,loan_applications_tbl.loan_type,loan_applications_tbl.appr,loan_applications_tbl.sold,loan_applications_tbl.loan_status,loan_applications_tbl.top_up,loan_applications_tbl.current_loan,loan_applications_tbl.date_created,loan_applications_tbl.date_updated
FROM loan_applications_tbl
UNION ALL
SELECT loan_applications_tbl_dump.loan_id,loan_applications_tbl_dump.user_unique_id,loan_applications_tbl_dump.loan_location,loan_applications_tbl_dump.ippis,loan_applications_tbl_dump.tel_no,loan_applications_tbl_dump.organisation,loan_applications_tbl_dump.branch,loan_applications_tbl_dump.loan_agree,loan_applications_tbl_dump.loan_type,loan_applications_tbl_dump.appr,loan_applications_tbl_dump.sold,loan_applications_tbl_dump.loan_status,loan_applications_tbl_dump.top_up,loan_applications_tbl_dump.current_loan,loan_applications_tbl_dump.date_created,loan_applications_tbl_dump.date_updated
FROM loan_applications_tbl_dump ) t3
LEFT JOIN topup_or_reapplication_tbl
ON t3.ippis=topup_or_reapplication_tbl.ippis
WHERE ((topup_or_reapplication_tbl.status IN ('pending','corrected','Rejected','Processing','Captured','Reviewed','top up')
AND MONTH(CURRENT_DATE) IN (MONTH(topup_or_reapplication_tbl.date_created),MONTH(topup_or_reapplication_tbl.date_updated)
AND YEAR(CURRENT_DATE) IN (YEAR(topup_or_reapplication_tbl.date_created),YEAR(topup_or_reapplication_tbl.date_updated))
AND topup_or_reapplication_tbl.current_loan='1'
))
OR ( t3.loan_status IN ('pending','top up')
AND MONTH(CURRENT_DATE) IN (MONTH(t3.date_created),MONTH(t3.date_updated) )
AND YEAR(CURRENT_DATE) IN (YEAR(t3.date_created),YEAR(t3.date_updated))
AND (t3.current_loan='1' OR (t3.current_loan='0' AND t3.loan_status IN('Approved','Closed')))
))
-- GROUP BY t3.ippis ORDER BY t3.date_updated DESC
解决方法
你介意做一些类似伪代码的事情来帮助我更好地理解你的答案吗——彼得
SELECT {fieldset}
FROM ( SELECT {fieldset} FROM table1
UNION ALL
SELECT {fieldset} FROM table2 )
JOIN {tableset}
WHERE {conditions}
等于
( SELECT {fieldset}
FROM table1
JOIN {tableset}
WHERE {conditions} )
UNION ALL
( SELECT {fieldset}
FROM table2
JOIN {tableset}
WHERE {conditions} )
,
这可能对某些人有所帮助:
AND MONTH(CURRENT_DATE) IN (MONTH(t3.date_created),MONTH(t3.date_updated) )
AND YEAR(CURRENT_DATE) IN (YEAR(t3.date_created),YEAR(t3.date_updated))
-->
AND t3.date_created > CURDATE() - DAYOFMONTH(CURDATE())-1 DAY
AND t3.date_updated > CURDATE() - DAYOFMONTH(CURDATE())-1 DAY
也许您可以取消 updated
测试,因为它总是晚于 created
?
但也许真正的解决办法不是拥有 3 个本质上相同的表。