dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mysql/#21
Script #21
mysql
Performanceread-onlyMin Version: 5.7

Find Long-Running Queries in MySQL

Lists active queries running longer than 60 seconds from the processlist. Fast diagnosis for slowdowns and lock pile-ups.

#processlist#long-running#incident
mysql · sql
SELECT id, user, host, db, command, time AS seconds, state,
       LEFT(info, 200) AS query
FROM information_schema.processlist
WHERE command <> 'Sleep'
  AND time > 60
ORDER BY time DESC;
Operational Notes & Remediation Guidance

To terminate a runaway query (write action): `KILL QUERY <id>;` or terminate the connection with `KILL <id>;`.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 5.7).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.