Suppose I have a table (MySQL):
CREATE TABLE sessions (
session_id INT NOT NULL AUTO_INCREMENT,
name CHAR(12),
start INT,
end INT,
PRIMARY KEY (session_id)
)
to track users who are logged in to the app. Each user login creates an entry in this table, setting the start time (as an integer number of seconds in seconds from the Unix era), and logout updates this table in the same way as the end time. My problem is to find the number of registered users at five minute intervals, for a range of time (usually per day).
What I have done so far is to write a procedure that traverses the data.
SET t = begin_time;
WHILE t <= end_time DO
SELECT t, COUNT(1) FROM TABLE WHERE start <= t AND end >= t;
SET t = t + 300;
END WHILE;
It takes a lot of time; I am looking for alternative solutions to this problem. Web links, pointers - any help will do.
Thanks in advance.
source