使用UNION关键字的查询在mysql中运行速度非常慢

时间:2013-08-02 00:08:29

标签: mysql

为什么带有UNION关键字的查询在mysql中运行速度非常慢?

我使用UNION关键字创建了一个查询,但每次执行它时,显示输出都需要很长时间。

这是我使用的查询。

SELECT process_trained AS Training_Title, 
   org_party AS Organizing_Party, 
   duration AS Duration, 
   Concat(Date_format(sptrain_from, '%d-%b-%y'), " to ", 
   Date_format(sptrain_to, '%d-%b-%y'), "") AS SpecialTraining_Date, 
   trainor AS Trainor, 
   category AS Category, 
   training_type AS Training_Type, 
   train_id, 
   Date_format(cert_date, '%d-%b-%Y') AS Date_Certified, 
   Date_format(re_certdate, '%d-%b-%Y') AS Re_Certificaton, 
   remarks, 
   filename 
FROM(SELECT *, 
               cert_date AS cdate 
        FROM   tbldirtraining 
        UNION ALL 
        SELECT *, 
               sptrain_from 
        FROM   tbldirtraining) jes 
WHERE  emp_id = '6555' 
       AND cdate <> '' 
ORDER  BY cdate; 

是否有任何可行的方法可以帮助您更快地运行此查询。

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个

     SELECT process_trained AS Training_Title, 
 org_party AS Organizing_Party, 
 duration AS Duration, 
 Concat(Date_format(sptrain_from, '%d-%b-%y'), " to ", 
 Date_format(sptrain_to, '%d-%b-%y'), "") AS SpecialTraining_Date, 
 trainor AS Trainor, 
 category AS Category, 
 training_type AS Training_Type, 
 train_id, 
 Date_format(cert_date, '%d-%b-%Y') AS Date_Certified, 
 Date_format(re_certdate, '%d-%b-%Y') AS Re_Certificaton, 
 remarks, 
 filename 
FROM  tbldirtraining 
WHERE  emp_id = '6555' 
AND cert_date <> '' 
ORDER  BY cert_date desc,sptrain_from asc;