sql查询用union和排序后速度变慢,求帮忙优化速度

交个朋友好不好 2014-02-17 10:53:46
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表的备份表,表结构完全一样。
...全文
1849 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
查询条件还有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') 我没有写出来. 我在数据库pl/sql上查询时发现,sql中"union"改为"union all",排序"order by curr_time desc"去掉以后速度就非常快了,在1秒之内,如果不改速度是三十多秒. 现在的需求是需要排序和去重的.
流浪川 2014-02-17
  • 打赏
  • 举报
回复
不是应该在event_time上建立索引么?
  • 打赏
  • 举报
回复
t1.curr_time建立了索引,但是速度还是很慢。 有人吗?请大家帮忙顶一下。谢谢了!在线等。。。。。。。。。。。
_拙计 2014-02-17
  • 打赏
  • 举报
回复
引用 8 楼 fengfenglucky 的回复:
查过了,分析函数有几十个,请问这里要用排序和去重要用哪几个呀? 另外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
  • 打赏
  • 举报
回复
查过了,分析函数有几十个,请问这里要用排序和去重要用哪几个呀? 另外union 这个函数还需要用吗?
_拙计 2014-02-17
  • 打赏
  • 举报
回复
引用 6 楼 fengfenglucky 的回复:
求教二位分析函数在这个具体的sql中该怎么用啊? 请指教!谢谢啦
这东西 自己可以去百度吧 分析函数应用
  • 打赏
  • 举报
回复
求教二位分析函数在这个具体的sql中该怎么用啊? 请指教!谢谢啦
_拙计 2014-02-17
  • 打赏
  • 举报
回复
对于大数据去重和排序 一般用分析函数 如:
select *
  from (select a,
               b,
               c,
               row_number() over(partition by a, b, c order by a, b, c) rw
          from tab)
 where rw = 1
减少磁盘排序,增加内存排序,性能提高
sych888 2014-02-17
  • 打赏
  • 举报
回复
排序 去重,本来就是比较耗时的操作,可以分析函数改写一下,看看效果

17,090

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧