zoukankan      html  css  js  c++  java
  • Oracle Sql优化之范围处理

    1.表中字段自关联与分析函数的性能比较,自关联需要扫描表两次,分析函数扫描一次即可

    ----自关联
    select v1.proj_id,v1.proj_start,v1.proj_end
    from v v1,v v2
    where v1.proj_start=v2.proj_end
    
    ----分析函数
    select proj_id,proj_start,proj_end
    from (select proj_id,proj_start,proj_end,
              lead(proj_start) over(order by proj_id) nproj_start
              from v)
    where nproj_start = proj_end

    上述处理方式稍作修改,也可以用于计算用户登录的时间间隔, lead(xxx) over(partition user order by xxx),先按照用户进行分区

    2.连续的时间段合并处理,求解合并后的时间区间。

    select groupId,min(proj_start),max(proj_end) 
     from
     (select proj_id,proj_start,proj_end,sum(status)  over(order by proj_id) groupId   
      (select proj_id,proj_start,proj_end,
              case when lag(proj_end) over(order by proj_id)=proj_start 
                      then 0 else 1 end status
       from v)
    ) group by groupId order by 1

    3.Lag和lead是一种连续数据处理函数,对于数据时区间内的判断则需要between的方式来判断

    with x0 as
      (select id,sdate,edate
                 max(edate) over (order by sdate rows between unbounded preceding and 1 preceding) as medate
        from b),
           x1 as
       (select sdate,edate,medate,case when medate>sdate then 0 else 1 end as status from x0),
           x2 as
       (select sdate,edate,sum(status) over(order by sdate) as groupId from x1)
       select groupId,min(sdate),max(edate) 
       from x2
       group by groupId
       order by groupId
  • 相关阅读:
    你的指纹还安全吗?
    看黑客如何远程黑掉一辆汽车
    利用Teensy进行em410x卡模拟以及暴力破解em410x类门禁系统
    curl命令使用
    Robots.txt
    NodeJs框架
    redis3.2 最新版本启动配置文件redis.conf详细说明
    CentOS 7 上安装 redis3.2.3安装与配置
    Rabbitmq Exchange Type 说明
    windows7下修改hosts文件无效解决办法(转)
  • 原文地址:https://www.cnblogs.com/zhulongchao/p/4536912.html
Copyright © 2011-2022 走看看