select 
            70000 as req_max,
            3000 as req_min,
            t.dayName as hour, 
            n.reqCnt as req_cnt
        from (select a.day_id || lpad(b.minute_code, 3, '0') as dayId,
                     a.day_id || b.minute_name as dayName
                from dim_time_day a, dim_time_minute b
               where to_number(a.day_id || lpad(b.minute_code, 3, '0')) between
                     (select to_number(to_char(sysdate-1, 'yyyymmdd') || lpad(to_char(to_number(to_char(sysdate, 'hh24'))*12 
                              + floor(to_number(to_char(sysdate, 'mi'))/5) + 1), 3 ,'0'))
                               from dual)
                             and 
                             (select to_number(to_char(sysdate, 'yyyymmdd') ||  
                             lpad(to_char(to_number(to_char(sysdate, 'hh24'))*12 
                             + floor(to_number(to_char(sysdate, 'mi'))/5)), 3 ,'0')) from dual)) t
        left join (select t.req_cnt as reqCnt, t.partition_date as dayId
                     from fct_portal_collect_min t
                    where t.partition_date between 
                    (select to_number(to_char(sysdate-1, 'yyyymmdd') || lpad(to_char(to_number(to_char(sysdate, 'hh24'))*12 
                           + floor(to_number(to_char(sysdate, 'mi'))/5) + 1), 3 ,'0'))
                           from dual)
                           and 
                           (select to_number(to_char(sysdate, 'yyyymmdd') ||  
                           lpad(to_char(to_number(to_char(sysdate, 'hh24'))*12 
                           + floor(to_number(to_char(sysdate, 'mi'))/5)), 3 ,'0')) from dual) order by t.partition_date ) n 
      on t.dayId = n.dayId上面sql语句中between部分得数据用了两次,请问是否可以优化下呢 sql优化

解决方案 »

  1.   

    链接两个表,两个表中查询时between范围是相同的,就想看看能否优化下,让执行效率高一点
      

  2.   

    如果是要优化between后面的 效率应该不会有什么变化  因为后面2个时间 都是通过当前时间计算出来的 根本就不会耗费计算时间 只要的耗时 应该就在这里面 能不能想办法代替掉where后的
    select a.day_id || lpad(b.minute_code, 3, '0') as dayId,
           a.day_id || b.minute_name as dayName
    from dim_time_day a, dim_time_minute b
    where to_number(a.day_id || lpad(b.minute_code, 3, '0'))
      

  3.   

    WHERE CONDITION 有点复杂,会影响效率的!