Exporting the mysql.slow_log table into slow query log format
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). 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
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'.
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.
Archives
- May 2013
- March 2013
- February 2013
- January 2013
- December 2012
- November 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- November 2009
- March 2008
- November 2007
- October 2007


Comments
Thanks - this has been very useful. I wanted to do the same but with the general_log rather than the slow_log. The query I used for this was:
mysql -s -u root -p -h hostname -e"SELECT CONCAT_WS(' ',DATE_FORMAT(event_time, '%y%m%d %H:%i:%s'), thread_id, command_type, argument) FROM mysql.general_log;"> general.logGreat blog post, good to see someone actually putting the right command in one for once!!
Thanks! Saved me some time :)
Great and very usefull, thx !
Cool.
Good work, Charlie!
Reply