2013-07-03 82 views
-2

我的兩個查詢單獨工作速度快,但是當我加入這兩個查詢花費的時間來執行很多,我的查詢被連接時速度很慢,分開他們快速

SELECT b.*, 
     a.* 
FROM (SELECT r.vou_no, 
       r.vou_date, 
       r.lr_date, 
       r.due_date, 
       r.vac_code, 
       r.vamount, 
       r.vmfg_amt, 
       r.vter_cd, 
       r.mkt_year, 
       r.mnth_code 
     FROM ledfile AS r 
     WHERE r.mkt_year = 2012 
       AND r.mnth_code BETWEEN 201210 AND 201306 
       AND r.div_code = 1 
       AND r.vdepo_code = 21 
       AND r.vbook_cd = 80 
       AND Ifnull(r.del_tag, '') <> 'D' 
     ORDER BY r.vac_code, 
        r.vou_date, 
        r.vou_no) b, 
     (SELECT a.vac_code vcode, 
       a.vouno, 
       a.bill_date, 
       a.vamount vamt, 
       a.vou_date vdate 
     FROM rcpfile a 
     WHERE a.mkt_year = 2012 
       AND a.div_code = 1 
       AND a.vdepo_code = 21 
       AND a.vbook_cd IN (20, 21, 22, 90, 
            98, 80) 
       AND Ifnull(a.del_tag, '') <> 'D' 
     ORDER BY a.vac_code, 
        a.bill_date, 
        a.vouno) a 
WHERE a.vcode = b.vac_code 
     AND a.vouno = b.vou_no 
     AND a.bill_date = b.vou_date; 
+0

可以請您分開部分 –

+1

說明你在做什麼,不要只是放在牆上的代碼。請正確格式化。 – Pablo

+0

要在多個表上的查詢中執行更快的JOIN,在表上具有適當的索引非常重要。在查詢中加入的列必須位於索引中。 – DOK

回答

0

如何像這樣...

SELECT b.vou_no 
    , b.vou_date 
    , b.lr_date 
    , b.due_date 
    , b.vac_code 
    , b.vamount 
    , b.vmfg_amt 
    , b.vter_cd 
    , b.mkt_year 
    , b.mnth_code 
    , a.vac_code vcode 
    , a.vouno 
    , a.bill_date 
    , a.vamount vamt 
    , a.vou_date vdate 
    FROM ledfile b 
    JOIN rcpfile a 
    ON a.mkt_year = b.mkt_year 
    AND a.div_code = b.div_code 
    AND a.vdepo_code = b.vdepo_code 
    AND a.del_tag = b.del_tag 
WHERE a.vbook_cd IN (20,21,22,90,98,80) 
    AND COALESCE(b.del_tag,'') <> 'D' 
    AND b.mkt_year = 2012 
    AND b.mnth_code BETWEEN 201210 AND 201306 
    AND b.div_code = 1 
    AND b.vdepo_code = 21 
    AND b.vbook_cd = 80 
+0

當我運行這個查詢時出錯代碼:2008 MySQL客戶端內存耗盡@Strawberry – Narendra

相關問題