SELECT LEFT(digest_text, 160) AS statement, schema_name, count_star AS calls, ROUND(sum_timer_wait / , 1) AS total_seconds, ROUND(avg_timer_wait / , 2) AS avg_ms, sum_rows_examined AS rows_examined, sum_rows_sent AS rows_sentFROM performance_schema.events_statements_summary_by_digestWHERE schema_name IS NOT NULLORDER BY sum_timer_wait DESCLIMIT 20Needs something on the server: performance_schema must be ON (it is by default since 5.6).
This 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 performance_schema.events_statements_summary_by_digest and returns the 20 statement shapes that spent the most total time. A digest is a statement with its literal values replaced by question marks, so thousands of calls to the same query become one row.
That is the right way to find what to tune. One query that takes 5 ms and runs a million times costs more than one that takes 3 seconds and runs twice. The slow query log shows single slow executions. The digest table shows where the time goes in total.
How to read each column
- statement: the first 160 characters of digest_text.
- schema_name: the default schema when the statement ran. Rows with no default schema are filtered out.
- calls: how many times it ran.
- total_seconds: the time added up over all calls. The list is sorted by it.
- avg_ms: the average time per call, in milliseconds.
- rows_examined and rows_sent: rows read against rows returned, added up over all calls.
The timers are kept in picoseconds. The query divides by 1e12 for seconds and by 1e9 for milliseconds.
What is normal and what is not
- rows_examined far above rows_sent (a ratio of hundreds or more is a rule of thumb) usually means a missing or unused index.
- High calls with a small avg_ms: the statement is cheap but too frequent. Look for an N+1 pattern or caching in the application.
- Low calls with a large avg_ms: a heavy report or batch job. Check whether it runs at a bad hour.
- A statement near the top that you do not recognise: ask whoever owns the schema.
What to do about it
Take the statement, replace the placeholders with real values and run EXPLAIN on it. Look for type ALL (full scan), a large rows estimate and Using filesort or Using temporary.
EXPLAIN SELECT * FROM shop.orders WHERE customer_id = 42 ORDER BY created_at DESC; -- Often the fix is a composite index that matches the filter and the sort:ALTER TABLE shop.orders ADD INDEX idx_customer_created (customer_id, created_at);If you want single slow executions as well, turn on the slow query log:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;To measure a change, clear the counters, wait for normal traffic, and run the query again:
TRUNCATE TABLE performance_schema.events_statements_summary_by_digest;Limits of this query
- The performance_schema must be ON. It is the default since MySQL 5.6, but it cannot be turned on at runtime; set performance_schema = ON in my.cnf and restart. MariaDB has it off by default.
- The counters start at zero on every restart and are cleared by TRUNCATE. A fresh server has little to show.
- The table holds a limited number of digests (performance_schema_digests_size). Once it is full, extra statements are counted in a single row with a NULL digest.
- Long statements are cut when the digest is computed, controlled by performance_schema_max_digest_length. Two long statements that differ only after the cut share a digest.
- The account needs SELECT on performance_schema.
Run it on a schedule in DataRunner
Run the query once a day and send the result to the team channel. DataRunner delivers by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets, with a CSV, Excel, PDF or HTML result. Because the counters are cumulative, compare today with yesterday to see what changed.