I have a situation that I\'m sure is quite common and it\'s really bothering me that I can\'t figure out how to do it or what to search for to find a relevant example/soluti
I believe this could be a simpler way to reach your goal:
SELECT
start_log.name,
MAX(start_log.ts) AS start_time,
end_log.ts AS end_time,
TIMEDIFF(MAX(start_log.ts), end_log.ts)
FROM
log AS start_log
INNER JOIN
log AS end_log ON (
start_log.name = end_log.name
AND
end_log.ts > start_log.ts)
WHERE start_log.eventtype = 'start'
AND end_log.eventtype = 'stop'
GROUP BY start_log.name
It should run considerably faster as it eliminates one subquery.