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

标签 mysql

为什么mysql中使用UNION关键字的查询运行速度很慢?

我使用 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; 

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

提前致谢。

最佳答案

试试这个

     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; 

关于mysql - 使用 UNION 关键字的查询在 mysql 中运行速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006702/

相关文章:

Mysql查询在两列的基础上动态将行转列

mysql - SQL 标准用户名(root)和密码(空白)不起作用

php - Wordpress WP_Query 具有多个元值的元键

MySQL IFNULL 不返回默认值

php - 尝试通过 PHP/MySQL 下载 Blob

mysql - 架构定义中的 SQL 日期格式

PHP循环充当cronjob[确保只有一个实例运行]

php - 如果不存在具有相同值的行,则插入该行

mysql - 根据当前 ID 按日期获取倒数第二个 ID

c# - Visual Studio 2017 + Entity Framework + 身份 + mysql 不工作