2

We have processes which connect to a subsystem and we are tracking them by this timestamp. What I would like to find out is the maximum number of processes have connected in a time interval of N minutes.

This time interval is the TIMEOUT interval of a process so the countdown of the elapsed time starts when it connects. So the interval is a "floating" interval.

For simplicity, the length of this interval is 5 minutes, the connection times are in minutes and not in milliseconds.

We already have a PL/SQL function to calculate this number but I would like to know if it is possible to do it using only an SQL.

Example:

  09:10          09:15          09:20          09:25          09:30
    |              |              |              |              |
----|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|---
    |              |              |              |              |
          |  |  |           |  |     |  |  |  |     |        |
          a  b  c           a  c     a  b  c  d     a        f       <-- PROCESSES
                                              f     b                <-- PROCESSES

Here is SQL for the PROCESSES table:

create table PRCS
(
  prc_id   NUMBER(12) not null,
  prc_name VARCHAR2(25) not null
);

alter table PRCS add constraint PRCS_PK primary key (PRC_ID);
alter table PRCS add constraint PRCS_UK unique (PRC_NAME);

insert into PRCS values('1', 'a');
insert into PRCS values('2', 'b');
insert into PRCS values('3', 'c');
insert into PRCS values('4', 'd');
insert into PRCS values('5', 'e');
insert into PRCS values('6', 'f');

Here is SQL for the CONNECTIONS table:

create table CON_JOURNAL
(
  con_id   NUMBER(12) not null,
  con_date TIMESTAMP(6) not null,
  con_prc  NUMBER(12) not null
)
;
alter table CON_JOURNAL add constraint CON_PK primary key (CON_ID);
alter table CON_JOURNAL add constraint CON_UK unique (CON_DATE, CON_PRC);
alter table CON_JOURNAL add constraint CON_PRC_FK foreign key (CON_PRC) 
       references PRCS (PRC_ID);

insert into CON_JOURNAL values( '1', to_date('2013.01.09 09:12', 'yyyy.mm.dd hh24:mi'), '1');
insert into CON_JOURNAL values( '2', to_date('2013.01.09 09:13', 'yyyy.mm.dd hh24:mi'), '2');
insert into CON_JOURNAL values( '3', to_date('2013.01.09 09:14', 'yyyy.mm.dd hh24:mi'), '3');

insert into CON_JOURNAL values( '4', to_date('2013.01.09 09:18', 'yyyy.mm.dd hh24:mi'), '1');
insert into CON_JOURNAL values( '5', to_date('2013.01.09 09:19', 'yyyy.mm.dd hh24:mi'), '3');

insert into CON_JOURNAL values( '6', to_date('2013.01.09 09:21', 'yyyy.mm.dd hh24:mi'), '1');
insert into CON_JOURNAL values( '7', to_date('2013.01.09 09:22', 'yyyy.mm.dd hh24:mi'), '2');
insert into CON_JOURNAL values( '8', to_date('2013.01.09 09:23', 'yyyy.mm.dd hh24:mi'), '3');
insert into CON_JOURNAL values( '9', to_date('2013.01.09 09:24', 'yyyy.mm.dd hh24:mi'), '4');
insert into CON_JOURNAL values('10', to_date('2013.01.09 09:24', 'yyyy.mm.dd hh24:mi'), '6');

insert into CON_JOURNAL values('11', to_date('2013.01.09 09:26', 'yyyy.mm.dd hh24:mi'), '1');
insert into CON_JOURNAL values('12', to_date('2013.01.09 09:26', 'yyyy.mm.dd hh24:mi'), '2');
insert into CON_JOURNAL values('13', to_date('2013.01.09 09:29', 'yyyy.mm.dd hh24:mi'), '6');

Thank you for your help,

SK

4

1 回答 1

1

有趣的问题!:-)

要总结 5 分钟时间窗口内的连接,您可以使用分析函数的窗口子句。例如,您可以PARITITION BY con_prc按连接时间 ( ORDER BY con_date) 对单个进程的连接 ( ) 进行排序,并将窗口限制为前 5 分钟 ( RANGE INTERVAL '5' MINUTE PRECEDING):

        SELECT con_prc , 
               COUNT(*) OVER (PARTITION BY con_prc 
                              ORDER BY con_date
                              RANGE INTERVAL '5' MINUTE PRECEDING) AS connections
          FROM con_journal

然后,根据您的要求,选择每个进程的最大连接数:

SELECT con_prc, MAX(connections) AS max_con
  FROM (
        SELECT con_prc , 
               COUNT(*) OVER (PARTITION BY con_prc 
                              ORDER BY con_date
                              RANGE INTERVAL '5' MINUTE PRECEDING) AS connections
          FROM con_journal
        )
 GROUP BY con_prc;

 1  2
 2  2
 3  2
 4  1
 6  2

编辑:或者,您可以创建一个时间片表来加入时间片内的测量:

SELECT to_char(t,'HH24:MI') as t, count(con_id) as connected_sessions
  FROM con_journal j
  JOIN (SELECT TIMESTAMP '2013-01-09 09:00:00' + (level/24/60) as t
          FROM dual CONNECT BY level < 60) 
    ON t BETWEEN con_date AND con_date + INTERVAL '5' MINUTE
 GROUP BY t
 ORDER BY t;
于 2013-01-21T09:01:21.133 回答