SELECT id AS process_id, user, host, db, command, time AS running_seconds, state, LEFT(info, 200) AS queryFROM information_schema.processlistWHERE command <> 'Sleep' AND time > 60 AND id <> CONNECTION_ID()ORDER BY time DESCThis query is part of the "MySQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.
What the query shows
This query reads the process list and returns every session that is doing something other than sleeping and has been at it for more than 60 seconds. It leaves out its own connection. The longest comes first.
Run it when the application is slow right now, when CPU is high, or when a deploy or a migration seems stuck. It shows what is running at this moment. For the history, use the slow query log or the statements by total time query.
How to read each column
- process_id: the id you pass to KILL.
- user, host, db: who is running it, from where, and against which schema.
- command: usually Query. Binlog Dump is a replica reading the binary log and is normal.
- running_seconds: how long the session has been in its current state.
- state: what the thread is waiting for, for example Sending data, Waiting for table metadata lock or Copying to tmp table.
- query: the first 200 characters of the statement.
What is normal and what is not
An empty result is the usual one on an OLTP system. Sixty seconds is a rule of thumb; a reporting database may run legitimate queries for much longer. What to look for:
- A migration or report you know about: leave it alone.
- The same statement from many sessions: a missing index or a stuck lock.
- State Waiting for table metadata lock: a long transaction elsewhere is blocking a DDL, and everything queues behind the DDL.
- Binlog Dump rows with a large running_seconds are replicas and are expected.
What to do about it
To stop one statement and keep the connection, use KILL QUERY. KILL CONNECTION closes the whole session. InnoDB rolls back what the statement had changed, and a large write can take a while to roll back.
KILL QUERY 12345;For SELECT statements you can set a ceiling so they stop on their own, in milliseconds:
SET GLOBAL max_execution_time = 60000;To keep a record of slow statements, turn on the slow query log. These settings apply without a restart, but only new connections pick up long_query_time:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;
SET GLOBAL log_queries_not_using_indexes = 'ON';
SHOW VARIABLES LIKE 'slow_query_log_file';log_queries_not_using_indexes can be noisy. Switch it on for a short period. Put the same values in my.cnf so they survive a restart, and read the file with mysqldumpslow.
Limits of this query
- It shows only what is running when it executes. A query that took 50 seconds an hour ago is invisible.
- An idle session with an open transaction has command Sleep and is excluded, yet it can hold locks. Look for it separately in information_schema.innodb_trx.
- Without the PROCESS privilege the account sees only its own sessions.
- In MySQL 8.0 the information_schema.processlist table is deprecated in favour of performance_schema.processlist and SHOW PROCESSLIST. It still works. Reading it takes a mutex, which the performance_schema version avoids.
Be told when it finds something
Schedule the query every few minutes in DataRunner and enable the option to skip runs with no rows. You hear from it only when a query crosses 60 seconds. The result goes by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets.