Use mysqldumpslow to group similar statements in a slow query log and compare their execution count and query time. To list query patterns from highest to lowest total query time:
mysqldumpslow -s t -l /var/log/mysql/mysql-slow.log
Use the path to your existing slow query log. -s t sorts by total query time, and -l keeps the logged query times instead of subtracting lock time. The command reads the file; it does not execute the SQL in it. See the MySQL mysqldumpslow reference for other sorting options.
Consider this example:
Count: 100 Time=1.20s (120s) Lock=0.00s (0s) Rows=10.0 (1000), app[app]@localhost
SELECT id, total FROM orders WHERE customer_id = N
Count: 40 Time=1.50s (60s) Lock=0.00s (0s) Rows=10.0 (400), app[app]@localhost
SELECT id, price FROM products WHERE category = 'S'
Count: 2 Time=8.00s (16s) Lock=0.00s (0s) Rows=1.0 (2), app[app]@localhost
SELECT SUM(total) FROM orders WHERE created_at >= 'S'
N and 'S' replace numeric and string values, so similar statements can appear as one pattern. Read the first entry as follows:
The report query is slowest per execution at 8 seconds, but its two executions total 16 seconds. The customer-order lookup takes 1.20 seconds each time and totals 120 seconds across 100 executions. If your goal is to reduce cumulative query time, investigate the customer-order lookup first. If the report is delaying a critical user action, its individual response time may take priority.
Total query time is the sum across logged executions. Concurrent executions can overlap, so this is neither CPU time nor elapsed wall-clock time for the file.
A slow log covers the statements recorded under its logging settings. For ongoing analysis, Releem collects query statistics from Performance Schema, including frequently executed queries that may not appear in the slow log.
In Query Analytics, sort by Load on Total Time to find queries with high cumulative execution time, or by Avg. Execution Time to investigate slow individual patterns. Open a query to inspect its SQL and request optimization recommendations.
See how Releem Query Analytics helps you choose what to optimize.