dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mysql/#25
Script #25
mysql
Performanceread-onlyMin Version: 5.7

Slowest MySQL Query Digests with Performance Schema

Groups similar queries to show total time, average time and no-index executions. Finds the costliest query patterns without enabling the slow log.

#performance_schema#digest#top-queries
mysql · sql
SELECT schema_name,
       LEFT(digest_text, 150) AS query_pattern,
       count_star AS exec_count,
       ROUND(sum_timer_wait / 1e12, 2) AS total_sec,
       ROUND(avg_timer_wait / 1e9, 2)  AS avg_ms,
       sum_rows_examined, sum_rows_sent,
       sum_no_index_used AS no_index_count
FROM performance_schema.events_statements_summary_by_digest
WHERE schema_name IS NOT NULL
ORDER BY sum_timer_wait DESC
LIMIT 20;
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 5.7).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.