CREATE PROCEDURE windowPeriod (IN BEGIN_TIME DATETIME)
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE minuteCount INTEGER DEFAULT 0;
DECLARE END_TIME DATETIME;
DECLARE eachMinute CURSOR FOR
select count(*) as C
from
(
SELECT *
from DATA timestamp >= BEGIN_TIME
and timestamp <=
( SELECT addtime(BEGIN_TIME, "00:20:00") )
group by user,host,timestamp
) as X;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN eachMinute;
addMinute: LOOP
FETCH eachMinute INTO minuteCount;
IF finished = 1 THEN
LEAVE addMinute;
END IF;
select CONCAT (BEGIN_TIME," ", minuteCount);
END LOOP addMinute;
CLOSE eachMinute;
I have a table DATA that contains several entries for a minute. For each row in the table, i want to calculate "how many rows within a 20 minute period starting that minute"? I write the above query, but this gets tremendously slow as the number of rows increases. Whats a better way to achieve this?
Sample entries in the table :
2022-09-10 23:22:05 Linux1
2022-09-10 23:22:05 Linux2
2022-09-10 23:26:04 Linux3
2022-09-10 23:26:04 Linux4
2022-09-10 23:33:04 Linux5
2022-09-10 23:33:04 Linux5
2022-09-10 23:35:04 Linux7
2022-09-10 23:48:04 Linux7
2022-09-10 23:52:05 Linux1
2022-09-10 23:55:05 Linux2
2022-09-10 23:56:04 Linux3
2022-09-10 23:57:04 Linux4
So if 23:22:05 is the first minute of the window, then there are 2 rows that are within the 20 minute window (Linux1 and Linux3 rows). If 23:35:04 is the first minute of the window, then there is just 1 row in the 20 minute window (Linux3 only).