Suppose I have a table like this:
CREATE TABLE foo ( gid BIGINT PRIMARY KEY, starttime BIGINT, endtime BIGINT );
This table stores the start and end times of a series of games (in the format of "seconds from the era"). Now I want to know how many games were completed in the blink of an eye. This is a natural request:
SET @t = UNIX_TIMESTAMP('2012-07-12 12:00:00'); SELECT COUNT(f.gid) FROM foo f WHERE @t BETWEEN f.starttime AND f.endtime;
The complication is that I need to do this every five minutes (each game lasts only a couple of minutes, and we are there several thousand every hour) and, probably, lasting six months. I have a procedure to iterate over the date range that interests me and generate @t for five minute intervals. The problem is that the request is too slow. I currently store all @t in a separate table that I indexed, for example:
CREATE TABLE bar ( interval BIGINT PRIMARY KEY );
So now I have a request:
SELECT b.interval, COUNT(f.gid) FROM bar b LEFT JOIN foo f ON b.interval BETWEEN f.starttime AND f.endtime GROUP BY b.interval;
This is too slow, and no amount of indexing in the "foo" table seems to help. This is, in my opinion, a standard problem, possibly using a standard request template, so appreciate any help here.
source share