Find slow queries in MySQL
Rank statement patterns by total execution time to find which queries consume the most database time.
Find slow queries in MySQL
Section titled “Find slow queries in MySQL”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_returnedFROM performance_schema.events_statements_summary_by_digestWHERE SCHEMA_NAME = DATABASE()ORDER BY SUM_TIMER_WAIT DESCLIMIT 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.
Do it in TableAI
Section titled “Do it in TableAI”- Open the target connection.
- Select Admin › Slow Queries.
- Select a statement to inspect its timing and call counts.
- 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.
Required access and statistics
Section titled “Required access and statistics”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.
Related
Section titled “Related”Use view locks when a query is waiting, or diagnose performance for the full investigation. Return to the MySQL overview.