Find your slowest MySQL queries with performance_schema
You don't need to turn on the slow log to find the queries that cost you the most. performance_schema already counts them: here's how to read it.
When a MySQL server is slow, the cause is usually a handful of queries. The trick is finding the ones that cost the most in total, not the single slowest one you happened to notice.
Use the statement digest table
MySQL normalises every statement into a digest: the same query with values replaced by ?. performance_schema keeps running totals per digest since the server started (or since the table was last truncated). It's on by default in MySQL 8.
SELECT digest_text,
count_star AS calls,
ROUND(sum_timer_wait / 1e12, 1) AS total_s,
ROUND(avg_timer_wait / 1e9, 2) AS avg_ms,
sum_rows_examined, sum_rows_sent,
sum_no_index_used, sum_created_tmp_disk_tables
FROM performance_schema.events_statements_summary_by_digest
ORDER BY sum_timer_wait DESC
LIMIT 15;
Timers are in picoseconds, hence the division. The sys schema has a friendlier view of the same data: SELECT * FROM sys.statement_analysis LIMIT 15;
What to look for
- Total time, not average. A 2 ms query called 5 million times a day costs more than a 4-second report run twice.
- Rows examined vs rows sent. If a query examines 200,000 rows to return 20, it's scanning. That almost always means a missing or unusable index.
- No index used.
sum_no_index_usedgreater than zero means at least some executions did a full scan. - Temporary tables on disk.
sum_created_tmp_disk_tablespoints to GROUP BY or ORDER BY work that spilled out of memory.
Then ask MySQL how it runs the query
Take a real example of the statement (with values) and run EXPLAIN:
EXPLAIN FORMAT=TREE
SELECT id, amount FROM orders WHERE customer_id = 42 ORDER BY created_at DESC LIMIT 20;
In the plan, Table scan on a big table, Using filesort and Using temporary are the usual warning signs. Since MySQL 8.0.18, EXPLAIN ANALYZE actually runs the query and shows real row counts and timings for each step. Use it on a replica or with care, because it executes the statement.
For the example above, an index on (customer_id, created_at) lets MySQL find one customer's orders and read them already sorted:
ALTER TABLE orders ADD INDEX idx_customer_created (customer_id, created_at);
The slow log is still useful
performance_schema gives totals, while the slow query log gives individual executions with their actual values, which is handy for reproducing a problem. A typical setting:
SET PERSIST slow_query_log = ON;
SET PERSIST long_query_time = 1;
Reset and compare
After a fix, you want to see whether it worked. You can clear the digest totals with TRUNCATE TABLE performance_schema.events_statements_summary_by_digest;. Note that this resets the numbers for everyone using them, including monitoring tools.
SQLNirvana reads this table every minute, keeps a 7-day trend per query, flags queries that suddenly got slower or changed plan, and suggests the index that fixes each one. With the --explain option it confirms suggestions against the real query plan.