在UNION上使用ALL / filesort

时间:2013-01-31 00:22:58

标签: mysql database performance indexing union

所以我有这个:

EXPLAIN (SELECT f.prn AS 'prn', f.id AS 'xnyz', 'f' AS 'omnf', f.wwn, (COUNT(fr.rid) > 0 OR COUNT(frr.rid) > 0) AS 'rrr', fr.rid AS 'rid', f.xyzz
        FROM scmf f
        LEFT JOIN scmr fr
                ON fr.omnf = 'f' AND fr.xnyz = f.id
      LEFT JOIN scmr frr
        ON frr.fid = f.id
        WHERE f.prn IN (0,43570,43571,43572,43573,43574,43575) AND f.xyzz IN (490923) AND f.id IN (0,43570,43571,43572,43573,43574,43575)
        GROUP BY f.id)
        UNION
      (SELECT fi.fxyz AS 'prn', fi.iix AS 'xnyz', 'n' AS 'omnf', fi.wwn,(COUNT(fir.rid) > 0) AS 'rrr', 0 AS 'rid', fif.xyzz
      FROM scmf_item fi
      LEFT JOIN scmr fir ON fir.omnf = 'n' AND fir.xnyz = fi.iix
      LEFT JOIN scmf fif ON fif.id = fi.fxyz
        WHERE fi.fxyz IN (0,43570,43571,43572,43573,43574,43575) AND fif.xyzz IN (490923)
                GROUP BY fi.iix
                )
                ORDER BY wwn ASC;

解释显示:

1   PRIMARY f   range   PRIMARY,xyzz,prn    PRIMARY 4       7   Using where
1   PRIMARY fr  ref omnf_id omnf_id 9   const,s.f.id    1   Using index
1   PRIMARY frr ref Index1  Index1  4   s.f.id  1   Using index
2   UNION   fi  range   fxyz    fxyz    4       24  Using where; Using temporary; Using filesort
2   UNION   fif eq_ref  PRIMARY,xyzz    PRIMARY 4   s.fi.fxyz   1   Using where
2   UNION   fir ref omnf_id omnf_id 9   const,s.fi.iix  1   Using index
    UNION RESULT    <union1,2>  ALL                     Using filesort

为什么UNION子句使用filesort /的类型为ALL

这是不是很糟糕?

我可以使用索引来修复此问题吗?

0 个答案:

没有答案