2013-03-19 82 views
0

有沒有一個完美的解釋,爲什麼我的查詢往往加載速度慢到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 

在整體這裏是我的整個query.Just的事實,同時檢查所有的DATAS要插入我的查詢似乎加載緩慢,以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 
+3

儘量避免使用內部選擇(我可以看到一些在你的「左連接」) – SNpn 2013-03-19 02:13:53

+3

ORs臭名昭着的表現不佳 – 2013-03-19 02:14:30

+0

@SNpn,我看到通過從子查詢而不是實際表中選擇性能提高。 – 2013-03-19 02:20:29

回答

3

我覺得最signifcant的原因之一是,你的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的/任何應用程序的一部分,設置相應的變量在應用程序代碼和轉換他們參數。