sql查询用union和排序后速度变慢,求帮忙优化速度
sql如下:
select t1.curr_time,t1.sms_num from itf_sms t1, bomc.tfa_alarm_his t2
where t1.alarm_id = t2.s_fp_id
and t2.event_time between
to_date('2014-02-14 10:21:52', 'YYYY-MM-DD hh24:mi:ss') and
to_date('2014-02-17 10:21:57', 'YYYY-MM-DD hh24:mi:ss')
union
select t1.curr_time,t1.sms_num from itf_sms_bak t1, bomc.tfa_alarm_his t2
where t1.alarm_id = t2.s_fp_id
and t2.event_time between
to_date('2014-02-14 10:21:52', 'YYYY-MM-DD hh24:mi:ss') and
to_date('2014-02-17 10:21:57', 'YYYY-MM-DD hh24:mi:ss')
order by curr_time desc
如果查询内容使用union all连接,并不做最后的排序,速度很快。
注:itf_sms_bak是itf_sms表的备份表,表结构完全一样。
select t1.curr_time,t1.sms_num from itf_sms t1, bomc.tfa_alarm_his t2
where t1.alarm_id = t2.s_fp_id
and t2.event_time between
to_date('2014-02-14 10:21:52', 'YYYY-MM-DD hh24:mi:ss') and
to_date('2014-02-17 10:21:57', 'YYYY-MM-DD hh24:mi:ss')
union
select t1.curr_time,t1.sms_num from itf_sms_bak t1, bomc.tfa_alarm_his t2
where t1.alarm_id = t2.s_fp_id
and t2.event_time between
to_date('2014-02-14 10:21:52', 'YYYY-MM-DD hh24:mi:ss') and
to_date('2014-02-17 10:21:57', 'YYYY-MM-DD hh24:mi:ss')
order by curr_time desc
如果查询内容使用union all连接,并不做最后的排序,速度很快。
注:itf_sms_bak是itf_sms表的备份表,表结构完全一样。
...全文
相关推荐
MySQL利用UNION连接2个查询排序失效详解
这导致了查询1和查询2各自独立排序,但最终的联合结果并没有按任何特定顺序排列。如果需要对整个合并后的结果集进行排序,有两种常见的解决方法:1.
SQL语句 UNION 和 UNION ALL 使用
### SQL语句 UNION 和 UNION ALL 使用详解#### 一、SQL UNION 和 UNION ALL 的基本概念**UNION** 和 **UNION ALL** 是 SQL 语言中用于合并两个或多个
sql Union和Union All的使用方法
在SQL中,`UNION`和`UNION ALL`是用来合并多个查询结果的两种方法,它们主要用于处理来自不同数据表的数据。尽管它们的功能相似,但它们在处理重复数据时的行为有所不同。
MySQL如何使用union all获得并集排序
同时,理解如何在`UNION ALL`操作中嵌套`ORDER BY`子句,对于编写复杂查询至关重要。这种技术在处理多条件排序和复杂数据组合时非常实用,能够灵活地适应不断变化的业务需求。
sql 查询结果合并union all用法_数据库技巧
总结来说,`UNION ALL`是SQL中一个强大的工具,它允许你在查询时组合多个数据源,而不管数据是否重复。正确使用它可以极大地提高查询效率和数据处理的灵活性。


