计算Oracle DB的可用性

时间:2014-08-24 17:51:39

标签: sql oracle

我已将系统日志存储在Oracle DB中。日志包含开始/结束日期。 我需要计算这个特定系统的正常运行时间/停机时间。 问题是,如果同时有多个日志怎么办? 例如: log1 start_date:8am end_date:10am log2 start_date:9am end_date:10.30am 我想要2.5小时的停机时间。 我应该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以通过识别重叠时段然后对它们进行分组来解决此问题。这个想法是每当一个组不重叠时创建一个标志,然后做一个标志的累积和。这样的事情应该可以解决正常运行时间和停机时间:

select system_id, min(start_date) as start_date, max(start_date) as end_date
from (select l.*, sum(IsStart) over (order by start_date) as grp
      from (select l.*,
                   (case when lag(end_date) over (partition by systemid, start_date) >= start_date
                         then 0
                         else 1
                    end) as IsStart
            from logs l
           ) l
     ) l
group by system_id, grp;

然后,您可以对此进行汇总以获取所需信息,例如:

select system_id, sum(end_date - start_date) as uptime,
       max(end_date) - min(start_date) = sum(end_date - start_time) as downtime
from (select system_id, min(start_date) as start_date, max(start_date) as end_date
      from (select l.*, sum(IsStart) over (order by start_date) as grp
            from (select l.*,
                         (case when lag(end_date) over (partition by systemid, start_date) >= start_date
                               then 0
                               else 1
                          end) as IsStart
                  from logs l
                 ) l
           ) l
      group by stem_id, grp
     ) l
group by system_id;

这假设期间不重叠,您不关心没有日志记录的系统。