. , , .
, , . . ( fileId), . timestamp stamp, .
(http://sqlfiddle.com/#!2/53fe8/3/0). :
SELECT TIMESTAMPDIFF(MINUTE, @prevStamp, stamp) AS timediff,
@prevStamp AS stamp,
@prevStatus AS status,
@prevFileId AS fileId,
@prevStamp := stamp AS newstamp,
@prevStatus := status AS newstatus,
@prevFileId := fileId AS newfileid
FROM FileLog,
(SELECT @prevStamp := NULL,
@prevStatus := NULL,
@prevFileId := NULL) AS r
ORDER BY fileId, stamp
, , , .
, "inProgress". , , , (http://sqlfiddle.com/#!2/53fe8/6/0).
SELECT SUM(timediff) in_progress_time,
MAX(newstamp) latest_time,
fileid
FROM (
SELECT TIMESTAMPDIFF(MINUTE, @prevStamp, stamp) AS timediff,
@prevStamp AS stamp,
@prevStatus AS status,
@prevFileId AS fileId,
@prevStamp := stamp AS newstamp,
@prevStatus := status AS newstatus,
@prevFileId := fileId AS newfileid
FROM FileLog,
(SELECT @prevStamp := NULL,
@prevStatus := NULL,
@prevFileId := NULL) AS r
ORDER BY fileId, stamp
) AS tis
WHERE status = 'inProgress'
GROUP BY status, fileId
ORDER BY fileId
, .
@Kickstart . , fileId inProgress ? in_progress_time. !
, . sqlfiddle: http://sqlfiddle.com/#!2/ef1e8/23/0
SELECT SUM(timediff) in_progress_time,
MAX(newstamp) latest_time,
fileid
FROM (
SELECT IF(fileId = @prevFileId,
TIMESTAMPDIFF(MINUTE, @prevStamp, stamp),
TIMESTAMPDIFF(MINUTE, @prevStamp, CURRENT_TIMESTAMP))
AS timediff,
@prevStamp AS stamp,
@prevStatus AS status,
@prevFileId AS fileId,
@prevStamp := stamp AS newstamp,
@prevStatus := status AS newstatus,
@prevFileId := fileId AS newfileid
FROM FileLog,
(SELECT @prevStamp := NULL,
@prevStatus := NULL,
@prevFileId := NULL) AS r
ORDER BY fileId, stamp
) AS tis
WHERE status = 'inProgress'
GROUP BY status, fileId
ORDER BY fileId
0 CURRENT_TIMESTAMP, , , .
Here about this question. Despite the fact that it looks complicated, it does one pass over the data. Other approaches to this kind of reporting can do self-learning, which leads to the MySQL server having to complete the query.
Performance indicator
It may or may not help fulfill this query to create a coverage index on (fileId, stamp, status). This may allow the subquery that runs ORDER BY fileId, stampto work in a natural way, rather than requiring so-called file management.