为什么我的查询加载速度比普通查询慢

时间:2013-03-19 02:12:27

标签: mysql sql

是否有一个完美的解释为什么我的查询往往加载速度慢到100secs?

是否有任何可能的重建使我的查询加载更快?

    SELECT IF(IMI.BRANCH_CODE IS NOT NULL AND IMI.PRODCODE IS NOT NULL AND IMI.POLNO IS NOT NULL,"Y","N") as STATUS,
                        NOW(),ICH.CLAIM_NO,ICH.BRANCH_CODE,
                        ICH.PRODUCT_TYPE,ICH.POLICY_NO,ICH.ASSURED_NAME,
                        ICH.AGENT_NO,ICH.INCEPTION_DT,ICH.EXPIRY_DT,
                        ICH.DT_REPORTED,IMI.BASIC_PREMIUM,ICH.CLAIM_STATUS,
                        IRPD.CLAIM_RESERVE,IRPD.TOTAL_AMT_RECOMMENDED,IRPD.DT_RECOMMENDED,
                        IMI.UW_YEAR,ICDO.PLATE_NO,ICDO.CHASSIS_NO,
                        ICDO.MOTOR_NO,IMI.LOCATION,IMI.RISKNO,IMI.SEQNO
                    FROM `ICATS_CLAIM_HEADER` ICH  
                    LEFT JOIN `ICATS_CLAIM_DETAILS_OD` ICDO ON ICH.CLAIM_NO = ICDO.CLAIM_NO
                    LEFT JOIN `ICATS_MULTIPLE_LOSS` IML ON ICH.CLAIM_NO = IML.CLAIM_NO
                    LEFT JOIN (SELECT * FROM ICATS_RESERVE_PAYMENT_DETAILS 
                                WHERE DATEDIFF(NOW(),DT_RECOMMENDED)=1 OR DATEDIFF(NOW(),RESERVE_DT)=1) IRPD 
                                ON ICH.CLAIM_NO = IRPD.CLAIM_NO
                    LEFT JOIN (SELECT POLNO,PRODCODE,BRANCH_CODE,MAX(SEQNO)as SEQNO FROM IWRITE_MOTOR_INFO 
                                GROUP BY POLNO,PRODCODE,BRANCH_CODE) IM 
                                ON ICH.POLICY_NO = IM.POLNO AND ICH.BRANCH_CODE = IM.BRANCH_CODE AND ICH.PRODUCT_TYPE = IM.PRODCODE 
                                JOIN IWRITE_MOTOR_INFO IMI 
                                ON IM.POLNO = IMI.POLNO AND IM.PRODCODE = IMI.PRODCODE AND IM.BRANCH_CODE = IMI.BRANCH_CODE AND IM.SEQNO = IMI.SEQNO
                    WHERE (DATEDIFF(CURDATE(),ICH.CLAIM_STATUS_DT)=1
                            OR DATEDIFF(NOW(),ICDO.TRANSACTION_DT)=1
                            OR DATEDIFF(NOW(),IRPD.RESERVE_DT)=1
                            OR DATEDIFF(NOW(),IRPD.DT_RECOMMENDED)=1)
                ORDER BY ICH.CLAIM_NO,IRPD.SEQUENCE_NO ASC

总体而言,这是我的整个查询。事实上,在检查要插入的所有数据时,我的查询似乎加载缓慢为100secs。

INSERT INTO `ICATS_IINSURE_CLAIM_HEADER`(IINSURE_EXIST,RECORD_CREATED_DT,CLAIM_NUMBER,BRANCH_CODE,PRODCODE,POLNO,ASSURED_NAME1,
                AGENT_CODE,INCEPTION,EXPIRY,DATE_REPORTED,BASIC_PREMIUM,
                CLAIM_STATUS,RESERVE_AMOUNT,CLAIMS_PAID_AMOUNT,
                DATE_RECOMMEND_PAYMENT,UW_YEAR,PLATE_NUMBER,
                CHASSIS,ENGINE,LOCATION,RISKNO,SEQ)
            SELECT IF(IMI.BRANCH_CODE IS NOT NULL AND IMI.PRODCODE IS NOT NULL AND IMI.POLNO IS NOT NULL,"Y","N") as STATUS,
                NOW(),ICH.CLAIM_NO,ICH.BRANCH_CODE,
                ICH.PRODUCT_TYPE,ICH.POLICY_NO,ICH.ASSURED_NAME,
                ICH.AGENT_NO,ICH.INCEPTION_DT,ICH.EXPIRY_DT,
                ICH.DT_REPORTED,IMI.BASIC_PREMIUM,ICH.CLAIM_STATUS,
                IRPD.CLAIM_RESERVE,IRPD.TOTAL_AMT_RECOMMENDED,IRPD.DT_RECOMMENDED,
                IMI.UW_YEAR,ICDO.PLATE_NO,ICDO.CHASSIS_NO,
                ICDO.MOTOR_NO,IMI.LOCATION,IMI.RISKNO,IMI.SEQNO
            FROM `ICATS_CLAIM_HEADER` ICH  
            LEFT JOIN `ICATS_CLAIM_DETAILS_OD` ICDO ON ICH.CLAIM_NO = ICDO.CLAIM_NO
            LEFT JOIN `ICATS_MULTIPLE_LOSS` IML ON ICH.CLAIM_NO = IML.CLAIM_NO
            LEFT JOIN (SELECT * FROM ICATS_RESERVE_PAYMENT_DETAILS 
                        WHERE DATEDIFF(NOW(),DT_RECOMMENDED)=1 OR DATEDIFF(NOW(),RESERVE_DT)=1) IRPD 
                        ON ICH.CLAIM_NO = IRPD.CLAIM_NO
            LEFT JOIN (SELECT POLNO,PRODCODE,BRANCH_CODE,MAX(SEQNO)as SEQNO FROM IWRITE_MOTOR_INFO 
                        GROUP BY POLNO,PRODCODE,BRANCH_CODE) IM 
                        ON ICH.POLICY_NO = IM.POLNO AND ICH.BRANCH_CODE = IM.BRANCH_CODE AND ICH.PRODUCT_TYPE = IM.PRODCODE 
                        JOIN IWRITE_MOTOR_INFO IMI 
                        ON IM.POLNO = IMI.POLNO AND IM.PRODCODE = IMI.PRODCODE AND IM.BRANCH_CODE = IMI.BRANCH_CODE AND IM.SEQNO = IMI.SEQNO
            WHERE (DATEDIFF(CURDATE(),ICH.CLAIM_STATUS_DT)=1
                    OR DATEDIFF(NOW(),ICDO.TRANSACTION_DT)=1
                    OR DATEDIFF(NOW(),IRPD.RESERVE_DT)=1
                    OR DATEDIFF(NOW(),IRPD.DT_RECOMMENDED)=1)
        ORDER BY ICH.CLAIM_NO,IRPD.SEQUENCE_NO ASC

1 个答案:

答案 0 :(得分:3)

我认为最重要的原因之一是你的where子句除了函数之外什么都没有。要解决这个问题,请找到一种方法来引入变量,以便:

WHERE (DATEDIFF(CURDATE(),ICH.CLAIM_STATUS_DT)=1
OR DATEDIFF(NOW(),ICDO.TRANSACTION_DT)=1
OR DATEDIFF(NOW(),IRPD.RESERVE_DT)=1
OR DATEDIFF(NOW(),IRPD.DT_RECOMMENDED)=1)

会变成这样的东西;

where ich.claim_status_dt >= a variable 
and ich.claim_status_dt < another variable
etc

您可以将其设置为存储过程并在那里设置变量,或者,如果此查询是php / .net / coldfusion /无论应用程序的一部分,请在应用程序代码中设置变量并将其转换为参数。