Exporting the mysql.slow_log table into slow query log format

1 min read
Oct 20, 2011 12:00:00 AM

Using pt-query-digest is an excellent way to perform a SQL review. However, sometimes you don't have access to the slow_query_log_file. For example, when MySQL runs on Amazon RDS, the slow_query_log_file is unavailable (see the RDS FAQ).

Overcoming Slow Log Access Restrictions in Amazon RDS

To get around this, export the mysql.slow_log table. To export the data, run the following SQL command from an instance that has database access. The data is exported into a MySQL slow-log format, which pt-query-digest can analyze:

mysql -u user -p -h host.rds.amazonaws.com -D mysql -s -r -e "SELECT CONCAT( '# Time: ', DATE_FORMAT(start_time, '%y%m%d %H%i%s'), '\n', '# User@Host: ', user_host, '\n', '# Query_time: ', TIME_TO_SEC(query_time), ' Lock_time: ', TIME_TO_SEC(lock_time), ' Rows_sent: ', rows_sent, ' Rows_examined: ', rows_examined, '\n', sql_text, ';' ) FROM mysql.slow_log" > /tmp/mysql.slow_log.log 

Understanding the Export Command Options

The -s (--silent) option is used to suppress echoing of the query. The -r (--raw) option is used to disable character escaping, making \n into an actual newline; otherwise, it's echoed as '\n'.

Analyzing the Log with pt-query-digest

Once the export is complete, run pt-query-digest to do the analysis. A simple review command is:

pt-query-digest --limit 100% /tmp/mysql.slow_log.log > /tmp/query-digest.txt 

query-digest.txt is now ready for review.

MySQL Database Consulting Services

Ready to optimize your MySQL Database for the future?

On this page

Ready to unlock value from your data?

With Pythian, you can accomplish your data transformation goals and more.