加入时我的查询非常慢,分别很快

时间:2013-07-03 12:31:57

标签: mysql

我的两个查询分别运行得很快,但是当我加入两个查询时,需要花费大量时间来执行,

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; 

1 个答案:

答案 0 :(得分: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