使用多个联接和子查询优化MySQL查询

时间:2018-09-27 05:07:25

标签: mysql join subquery

我正在使用以下查询从10个表中获取数据,它工作正常,但速度很慢,有什么方法可以优化查询。

查询:选择emi.emi_due_date,users.usr_mobile,users.usr_id,concat_ws(“”,users.usr_fname,users.usr_mname,users.usr_lname)作为借款人,users.usr_status,users.usr_curnt_city,users.usr_email, emi.loan_id,emi.emi_show_date,sum(emi.emi_amount)-sum(ifnull(emi.settled_amount,0))作为due_amount,cb.cb_type,blr.bloan_collection_executive_id,blr.pp_allow,blr.bloan_legal_team_us,blcon。 (“”,cp.cp_fname,cp.cp_lname)作为cp_name,cp.cp_mobile,cp.cp_firm_name,cp.cp_type,bg.guarantor_name,bg.guarantor_contact,pl.ecs_date,pd.p2p_date,

(从借方付款主的WHERE贷款ID = emi.loan_id的ID限制为0,1的订单中选择工具)作为last_pmode,

(SELECT IFNULL(DATE_FORMAT(emi_show_date-INTERVAL 1 MONTH,“%m-%Y”),“”)来自emi AS e WHERE e.loan_id = emi.loan_id和e.emi_status <2 ORDER by e.emi_show_date ASC限制1)为已付费,

(将“ browser_payment_master中的付款日期选择为bp,其中bp.loan_id = emi.loan_id按bp.id dec限制1排序)作为last_emi_paid FROM emi AS emi

通过emi.loan_id = blr.bloan_id进行INNER JOINrower_loan_reg_requests AS blr

INNER JOIN用户AS用户ON users.usr_id = blr.bloan_user_id

INNER JOIN借入者rower_loan_disbursed_funds作为blf ON blf.df_bloan_id = emi.loan_id

左加入channel_partners AS cp ON cp.cp_id = users.usr_cp_referral_id

向左保留借贷人_借款人贷款作为pl.pl_bloan_id = emi.loan_id

左联接collection_bucket AS cb在cb.cb_loan_id = emi.loan_id和cb.cb_status = 1

以左bg.guarantor_borrower_id = users.usr_id的身份向左加入clienter_guarantors AS

以pd.p2p_loan_id = emi.loan_id和pd.p2p_status = 1上的pd身份加入p2p_dates

其中emi.emi_status <2和emi.emi_amount!= 0

并且(从burrower_payment_master中选择count(*)作为pm WHERE pm.loan_id = emi.loan_id

AND MONTH(pm.payment_date)=“'.date('m')。'” AND YEAR(pm.payment_date)=“'date('Y')。'”)= 0

AND(并且从s.settlement_date中选择s.settlement_date作为s.loan_id = s.loan_id限制1的日期)!=“”

按emi.loan_id分组按emi.loan_id desc排序

0 个答案:

没有答案