Based on an advise from another developer here on stackoverflow, I have updated my query as below, but I still need to optimise it further. Can someone guide my on how best I can apply indexing to the query.
See Query Below:
SELECT 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.top_up, a.current_loan,
a.date_created, a.date_updated, c.loan_id, c.user_unique_id AS tu_user_unique_id,
c.ippis AS tu_ippis, c.top_up_approved,
c.loan_type AS tu_loan_type, c.dse, c.status, c.current_loan AS tu_current_loan,
c.record_category, c.date_created AS tu_date_created,
c.date_updated AS tu_date_updated
FROM loan_applications_tbl a
LEFT JOIN topup_or_reapplication_tbl c
ON a.ippis=c.ippis
WHERE ((c.status IN ('pending', 'corrected', 'Rejected', 'Processing', 'Captured', 'Reviewed', 'top up')
AND MONTH(CURRENT_DATE) IN (MONTH(c.date_created), MONTH(c.date_updated)
AND YEAR(CURRENT_DATE) IN (YEAR(c.date_created), YEAR(c.date_updated))
AND c.current_loan='1' ))
OR ( a.loan_status IN ('pending', 'corrected', 'Rejected', 'Processing', 'Captured', 'Reviewed', 'top up')
AND MONTH(CURRENT_DATE) IN (MONTH(a.date_created), MONTH(a.date_updated)) )
AND YEAR(CURRENT_DATE) IN (YEAR(a.date_created), YEAR(a.date_updated))
AND (a.current_loan='1'
OR (a.current_loan='0'
AND a.loan_status IN('Approved','Closed')))))
Execution time: 53s
No of records: 11000
using mysql EXPLAIN gives the screenshot below: (How do I maximise the information in the possible_keys column
I HAVE UPDATED ADDITIONAL INFORMATION BELOW:
I am using the OR between c and a for the below reasons:
a
is the parent table with 66 columns which gets populated with loan entries, if a new entry ona
has a matching/existingippis
(Unique field ona
) some columns ina
are updated/overwriten with data from the new entry, while the remaining data in the entry are inserted as new rows inc
(ippis
is not unique in tablec
). This is to keep a history of all subsequent loan requests while not giving room for redundancyWhile retrieving records I needed the big
OR
clause to enable me check botha
andc
tables for all instances of each loan records where thestatus, date and current_loan
columns match the parameters in my WHERE clause.a
will ALWAYS have a complete record in it butc
will NOT ALWAYS have a record in it, except there are more loan requests for the same unique ID.a
contains the "who is the account person such as by unique ID", and the additional / supplemental status detail FOR THE FIRST LOAN, subsequently, after the first loan "c" will be additional / supplemental status detail of the actual loan applications with the same Unique ID.if "A" is created Mar 12, and a new "c" record is created on Mar 16. The "A" record also gets the last updated stamped with Mar 16 since it has a child attachment that has some impact against it, while the new
c
record has it own created and updated time stamps. The Updated field will be blank/null for thea
record until changes are made or there is ac
record, The Updated field will be blank/null forc
record until there's some changes made toc
record
I hope this is understandable