日期:2014-05-17  浏览次数:20842 次

关于oracle查询的问题,sql优化!!!
我的需求是从每天的日志表内获取目前有哪些strapp_type1(一级类别), strapp_type2(二级类别)的分类,看我的sql:

select min(rownum) id,strapp_type1,strapp_type2 from (
select min(rownum) id,strapp_type1,strapp_type2 from log_dpitran_20120608 t group by strapp_type1,strapp_type2
union 
select min(rownum) id,strapp_type1,strapp_type2 from log_dpitran_20120609 t group by strapp_type1,strapp_type2
union 
select min(rownum) id,strapp_type1,strapp_type2 from log_dpitran_20120610 t group by strapp_type1,strapp_type2) a 
group by strapp_type1,strapp_type2 order by strapp_type1;


现在我数据库是保留一个月的日志表,这样如果用union来查询这30张表的话,速度就很慢,耗费大量时间,想问这sql如何优化,或者其他方式来达到要求,请高手给个sql语句,万分感谢!!!

------解决方案--------------------
方便写个存储过程吗?把子查询拆开写成表的形式会很快
------解决方案--------------------
用union all 加 distinct 试试? 别分组
select distinct strapp_type1,strapp_type2 from (
select distinct strapp_type1,strapp_type2 from log_dpitran_20120608 t union all
select distinct strapp_type1,strapp_type2 from log_dpitran_20120609 t union all 
select disctint strapp_type1,strapp_type2 from log_dpitran_20120610 t ) a