Skip to content

Find slow queries in MySQL

Rank statement patterns by total execution time to find which queries consume the most database time.

SELECT DIGEST_TEXT AS query_pattern,
COUNT_STAR AS calls,
SUM_TIMER_WAIT / 1000000000000 AS total_seconds,
AVG_TIMER_WAIT / 1000000000000 AS mean_seconds,
MAX_TIMER_WAIT / 1000000000000 AS max_seconds,
SUM_ROWS_EXAMINED AS rows_examined,
SUM_ROWS_SENT AS rows_returned
FROM performance_schema.events_statements_summary_by_digest
WHERE SCHEMA_NAME = DATABASE()
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 100;

These are accumulated statement summaries, rather than individual slow-log entries. MySQL groups statements by pattern and records timing in picoseconds; the query converts those times to seconds.

Compare total time with calls and mean time. A frequently executed fast statement can consume more total time than an occasional slow statement. Compare rows examined with rows returned to identify queries for a plan review.

  1. Open the target connection.
  2. Select Admin › Slow Queries.
  3. Select a statement to inspect its timing and call counts.
  4. Choose EXPLAIN to inspect its plan when available.

The view shows up to 100 patterns for the current database, ordered by total time. To use the SQL above, follow write and run SQL.

Use query plan guidance for the next check. EXPLAIN ANALYZE executes the statement; review it before confirming execution.

The account needs SELECT access to performance_schema.events_statements_summary_by_digest. Performance Schema must be enabled and configured to collect statement digests.

Check whether it is enabled:

SELECT @@performance_schema AS enabled;

For a value of 0, follow Admin data troubleshooting. An empty summary can also mean no statements were collected for this database. It does not establish that the workload is fast.

Use view locks when a query is waiting, or diagnose performance for the full investigation. Return to the MySQL overview.