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表的备份表,表结构完全一样。
------解决方案--------------------
排序 去重,本来就是比较耗时的操作,可以分析函数改写一下,看看效果
------解决方案--------------------
引用:查过了,分析函数有几十个,请问这里要用排序和去重要用哪几个呀?
另外union 这个函数还需要用吗?union 本身就是并集加去重的,肯定不能用了,你参考下:会不会快点select a.curr_time, a.sms_num
from (select t.*,
row_number() over(partition by t.curr_time, t.sms_num order by t.curr_time, t.sms_num) rw
from (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')
and t1.curr_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 all
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')
and t1.curr_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')) t) a
where a.rw = 1
order by a.curr_time desc