Analyzing the slow query log
We need to analyze the slow query log in order to identify the queries that impact a system. It does not always have to be the slowest queries that do this, because queries running more frequently with lower execution time (for example, a query that takes 1 seconds to run but runs thousands of times per minute) increase the workload on a system more than really slow queries running with less frequency (for example, a query that takes 60 seconds to run once a month). Really slow queries impact the system throughput, but queries running frequently generate most of the system workload.
Eventually, the slow query log grows in size, and the size is too big to analyze by simple inspection. In order to get a big picture on slow queries, MySQL provides a tool named mysqldumpslow to display the results in a way that’s easy to analyze. To use this tool, we need to pass the slow query log as the parameter, as shown in listing 09.
$ mysqldumpslow /var/log/mysql/mysql-slow.log
|
Listing 08
The output has the following fields:
- Count - How many times the query has been logged
- Time - Both the average time and the total time of processing time and waiting time
- Rows - Number of rows returned
We can execute this tool to discover specific problems such as “top 5 query which returned maximum rows” to find out queries with missing LIMIT clause as shown in listing 09.
$ mysqldumpslow -a -s r -t 5 /var/log/mysql/mysql-slow.log
|
Listing 09
We can also sort output by count (number of times query found in the slow query log) to find out the most frequent queries that can produce most of the server workload as shown in listing 10.
$ mysqldumpslow -a -s c -t 5 /var/log/mysql/mysql-slow.log
|
Listing 10
We can also display the top 5 queries sorted by the average query time, as shown in listing 11.
$ mysqldumpslow -t 5 -s at /var/log/mysql/localhost-slow.log
|
Listing 11
Comments