Suppose I have a table (MySQL) like this:
CREATE TABLE sessions (
session_id INT NOT NULL AUTO_INCREMENT,
name CHAR(12),
start INT,
end INT,
PRIMARY KEY (session_id)
)
to track the users who are logged into an application. Each user login creates an entry in this table setting the start time (as an integer counting seconds from the Unix epoch), and logout updates this table setting the end time similarly. My problem is to find the number of logged in users at five-minute intervals, for a time range (typically a day).
What I have done so far is to write a procedure that loops over 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;
This is quite time consuming; I am looking for alternative solutions for this problem. Web references, pointers – any help will do.
Thanks in advance.
I think, you’ll need the help of a number table to do a proper profiling for every point of time according to the given time range and interval between time points.
Here’s a possible solution:
The 3rd line makes sure all the timestamps being profiled are the beginnings of 5-minute intervals according to the clock scale. If you would rather like them to base off the beginning of the time range specified, change it to simply
SET t = begin_time;.This solution counts active sessions at the specified moments. If it is possible for a user to have several concurrent sessions and you would like to know how many distinct users were online, you should replace
COUNT(s.session_id)withCOUNT(DISTINCT s.name).