/* --- Option A (MySQL ≥5.7 / 8.0): add a per-query timeout hint --- */ /*+ MAX_EXECUTION_TIME(8000) */ -- 8s budget (optional) SELECT a.id AS ID, a.eventtime AS StartTimestamp, clr.eventtime AS EndTimestamp, CONCAT( LPAD(FLOOR(TIMESTAMPDIFF(SECOND, a.eventtime, COALESCE(clr.eventtime, NOW())) / 3600), 2, '0'), ':', LPAD(FLOOR((TIMESTAMPDIFF(SECOND, a.eventtime, COALESCE(clr.eventtime, NOW())) % 3600) / 60), 2, '0'), ':', LPAD( (TIMESTAMPDIFF(SECOND, a.eventtime, COALESCE(clr.eventtime, NOW())) % 60) , 2, '0') ) AS Duration, CONCAT(REPLACE(a.displaypath, '_', '-'), ' ', SUBSTRING_INDEX(a.source, ':/alm:', -1)) AS Description, CASE a.priority WHEN 0 THEN 'Diagnostic' WHEN 1 THEN 'Low' WHEN 2 THEN 'Medium' WHEN 3 THEN 'High' WHEN 4 THEN 'Critical' ELSE 'Unknown' END AS Priority, SUBSTRING_INDEX(SUBSTRING_INDEX(aed.strValue, '/', 2), '/', -1) AS Location, COALESCE(aed.strValue, SUBSTRING_INDEX(a.source, ':/tag:', -1)) AS Tag, COALESCE(aed.strValue, a.source) AS FullTag, a.displaypath AS Device FROM alarm_events a LEFT JOIN alarm_events clr ON ( clr.eventid = a.eventid AND clr.eventtype = 1 AND clr.eventtime >= a.eventtime AND clr.eventtime = ( SELECT MIN(eventtime) FROM alarm_events ae2 WHERE ae2.eventid = a.eventid AND ae2.eventtype = 1 AND ae2.eventtime >= a.eventtime ) ) LEFT JOIN alarm_event_data aed ON ( aed.id = a.id AND aed.propname = 'myTag' ) WHERE a.eventtype = 0 AND a.displaypath NOT LIKE '%System Startup%' AND a.source NOT LIKE '%System Startup%' AND ( (a.eventtime >= :starttime AND a.eventtime < :endtime) OR (a.eventtime < :starttime AND (clr.eventtime IS NULL OR clr.eventtime >= :starttime)) ) ORDER BY CASE WHEN clr.eventtime IS NULL THEN 0 ELSE 1 END, COALESCE(clr.eventtime, a.eventtime) DESC, a.id DESC;