Slow Query Log analyzes tools

(There is an updated version of this post here). MySQL has simple but quite handy feature – slow query log, which allows you to log all queries which took over define number of seconds to execute. There is also an option to enable logging queries which do not use indexes even if they take less time (–log-queries-not-using-indexes)

Slow query log is great to spot really slow queries which are often good candidates for optimization but it has few serious problems which limits extent to which it is helpful. First – it only allows you to set slow query time in seconds, having 1 second minimum value. For most of interactive applications this is way too large – if you’re developing Web application you probably want whole page to be generated less in 1 second, which issues many queries during generation. Second – if you enable option to log queries which do not use indexes it well can be flooded with fast and efficient queries, which just happen to do full table scans – for example if you would be having drop down list of states in your application and use SELECT * FROM STATES for that it would trigger and log the query.

Taking other Approach

For our clients we often need to find a queries which impact application the most. It does not always have to be slowest queries – query taking 10ms and run 1.000 times per second puts more load on server than 10 seconds query running once per second. We of course want to get rid of really slow queries but to really optimize application throughput queries which generate most of the load need to be investigated

Patching Slow Query Logging – First thing we did is created a Patch which allows you to specify slow query time in microseconds rather than seconds and allows you to log all queries in slow query log by setting long_query_time=0 This patch is adapted version of patch by Georg Richter which was made to run with recent MySQL version. Now why do not we use general log instead ? Unfortunately general logs queries before queries are executed (and even parsed) so it can’t contain query execution information such as execution and lock times and number of rows examined.

After this patch is applied your slow query log starts to look like this:

Filtering Slow Query Log – Especially after the changes to log all queries slow query log may be growing too rapidly to follow, so we implemented slow query log filter (based on parse_mysql_slow_log by Nathanial Hendler) which allows you to filter out only queries which took more than certain amount of time or examined more than certain amount of rows. This is great as allows multiple passes across same slow query log first to fix worse queries and then come to find more optimization candidates. So “tail -f mysql-slow.log | mysql_slow_log_filter -T 0.5 -R 1000” will look at queries as they come and will print out queries taking more than 0.5 seconds to execute or having more than 1000 rows examined.

Aggregating slow query log As I already mentioned besides finding slowest queries it is important to find queries which cause largest load on the server, which is with certain level of accuracy queries which take most time to execute combined. There is a tool mysqldumpslow in MySQL distribution which kind of does the thing – unfortunately being run on slow query log it does not give us information we’re looking for because only slow queries will be looked at. The other problem with this tool is – it replaces all real values with “N”, “S” etc placeholders, which means you can’t simply copy-paste query to run EXPLAIN for it. Using this tool normally require you to keep the other window open and find query sample with real constants which matches query with placeholders to work with it.

So we came op with slow query log parser tool which works with adjusted slow query log format and which gives samples of queries after aggregation. Here is how its output looks like:

As you can see it also prints minimum and maximum execution times so you will be able to see if only in certain cases query takes long time to execute, for example if plan is different based on constants.

How to use this tool set ?

First be aware this patch to MySQL is not official and should be used with caution. We think it is pretty safe but it surely did not get as much battle testing as rest of MySQL Server. Good thing is – you do not have to run patched version all the time. You can just start it for a few hours to generate you query log and get back to unpatched version.

It is best if you generate this log for all your queries with long_query_time=0 so if serious portion of you load comes from very simple queries you would not lose this kind of info. Yes this will reduce your performance a bit and will require plenty of disk space which is another reason you might not wish to run it in this mode all the time. Happily you can change long_query_time without restarting server so it is easy to get sample of all queries for some period of time and then get back to logging only very slow queries.

Once you have created full log – parse it and check queries using EXPLAIN starting from most impacted onces. After you’ve implemented changes – repeat. Changes may help to one queries but hurt others, for example adding indexes often help SELECT queries but slow down INSERT/UPDATE ones.

Final Note: You do not have to have patched MySQL for these utilities to work. they are designed to handle standard slow query log format as well.

Update 2009-03-04: most of us at Percona now use mk-query-digest from Maatkit for slow query log analysis.

Share this post

Comments (69)

  • Dekorasyon

    Tahnk you eleman

    September 6, 2006 at 12:00 am
  • Dekorasyon


    I did not test with 5.0.18 – but you can try.

    September 6, 2006 at 12:00 am
  • Jan Willamowius

    Another way to analyse your slow queries is to generate statistics which indexes are used, just like mysql_explain_log from the standard distribution does for the general query log.

    You can get mysql_explain_slow_log on

    September 6, 2006 at 5:04 am
  • Rene Leonhardt

    Dear Peter,

    Are you going to port the patch to MySQL 5.0.26?
    That would be really great!

    Keep up the good work,

    October 13, 2006 at 1:46 pm
  • Vadim October 15, 2006 at 1:39 pm
  • Vadim October 15, 2006 at 1:40 pm
  • BQ

    Firstly, thanks for your great work!

    I used slow query log parser to analyze a slow-query-log yesterday. I seems that all of the result looking good, but I see a very-long query at last. Maybe the script has some bugs on numberic-formating? Could you help me to fix it?


    ### 2846 Queries
    ### Total time: 18446744073826.7, Average time: 6481638817.22652
    ### Taking 0.020004 to 18446744073709.074210.000159 seconds to complete
    ### Rows analyzed 0 – 762

    October 15, 2006 at 7:26 pm
  • Vadim

    Yes, it looks like bug with numbers handling.
    Could you send us part of slow-log which cause such problem ?

    October 17, 2006 at 1:11 am
  • Misha Dar

    Peter, Vadim

    I’m using MySQL 5.0.18. It’s not quite clear to me – does your patch fits that version.
    Also, can You place somewhere/how more detailed instruction how to apply the patch.


    October 25, 2006 at 7:58 am
  • Vadim


    I did not test with 5.0.18 – but you can try.
    You need to place patch into source directory and execute
    patch -p1

    October 27, 2006 at 9:33 am
  • Rene Leonhardt

    Dear Vadim,

    Thank you very much for the actualised patch file!
    Do you have already one for MySQL 5.0.27?
    It should not be very different, because only one bug has been fixed.

    Best regards,

    October 29, 2006 at 2:30 pm
  • Vadim


    I think patch for 5.0.26 should work with 5.0.27 without any problem. I did not test though.
    Will check this week or so.

    October 30, 2006 at 8:18 am
  • camka

    For Your information, I’ve resently written the similar tool for regular query log to fetch the top of most popular queries.
    You can check it out at or at for more details.

    This tool wouldn’t give you the execution time based statistics, but outputs an overview of the most frequently run queries, so you could speedup the the overall db performance by optimizing the most popular queries.

    In addition I would like to mention, that Your parser does not take into account all possible string escaping ways, like doubeling quotes, escaping with slash etc. Also would be more useful to group structures like “IN (N,N,N,N,N)” into “IN (N)”.

    November 8, 2006 at 5:04 pm
  • peter

    Thank you Camka,

    It would be great if you make your tool to support slow query log including our format with execution time and show number of examined rows and sent rows.

    Query execution time matters. Query executed most frequently may not be bottleneck.

    November 14, 2006 at 9:18 am
  • Rene L.

    Dear Peter,

    I reimplemented the mysql_slow_log_parser in PHP in order to provide an even more filtered result.
    If you want me to send it, just comment this entry, because there is no attachment function here.

    # Apply filter to log file once
    php mysql_slow_log_parser.php mysql-slow-queries.log

    -T=min_query_time Include only queries which took as long as min_query_time seconds or longer [default: 1]
    -R=min_rows_examined Include only queries which examined min_rows_examined rows or more

    -iu=include_user Include only queries which contain include_user in the user field [multiple]
    -eu=exclude_user Exclude all queries which contain exclude_user in the user field [multiple]
    -iq=include_query Include only queries which contain the string include_query (i.e. database or table name) [multiple]

    –filter-duplicates Output only unique query strings with additional statistics: max_query_time, max_rows_examined, execution count [default sorting: max_query_time, max_rows_examined]

    [multiple] options can be passed more than once to set multiple values.

    Best regards,

    January 30, 2007 at 8:23 am
  • peter


    You can publish it somewhere and post a link here. If you have a trouble finding place you can send it to us and we can publish it but it is worse as we’ll need to take care of it if you decide to update it etc.

    January 30, 2007 at 9:38 am
  • camka

    > 16. peter
    Hei, peter,

    I have finally succeeded with impementing the support for sorting by max/average/total execution time of slow queries into MyProfi. But unfortunately i have no chance to try it on patched mysql version, so i tested it just with regular slow query log. You could try the latest version of MyProfi by taking it from project’s download page. Hope it can be useful tool for measuring the database performance on query level.

    January 31, 2007 at 1:50 am
  • Rene L.

    Hello Peter,

    I created a Google Code project for the MySQL Slow Query Log filter:

    You can access the initial PHP5 script here (requires the PHP extension BCMath):

    I am planning to provide a Python version, too.

    Please submit any problems or feature requests to the issue tracker on the project page.

    February 3, 2007 at 1:55 pm
  • Rene L.

    Dear Peter,

    I finished the Python version, which is usually 3-5 times faster than the PHP5 version according to my personal tests, depending on the log size.

    Usage Examples:

    # Filter slow queries executed from other users than root for at least 3 seconds, remove duplicates and save result to file
    php mysql_filter_slow_log.php -T=3 -eu=root –no-duplicates mysql-slow-queries.log

    # Start permanent filtering of all slow queries from now on: at least 3 seconds or examining 10000 rows, exclude users root and test
    tail -f -n 0 linux-slow.log | python -T=3 -R=10000 -eu=root -eu=test &
    # (-n 0 outputs only lines generated after start of tail)

    # Stop permanent filtering
    kill ps auxww | grep 'tail -f -n 0 linux-slow.log' | egrep -v grep | awk '{print 2}'

    February 4, 2007 at 6:22 am
  • Rene L.

    Unfortunately the comments here do not translate the “greater than” and “less than” symbols on submit.

    Therefore I will use { and } as a replacement for the example above:
    php mysql_filter_slow_log.php -T=3 -eu=root –no-duplicates { linux-slow.log } mysql-slow-queries.log

    February 4, 2007 at 6:29 am
  • peter

    Thank you Rene,

    I do not think log parser performance is that critical but I expect some people will like python parser more, especially if someone is looking to do some hacking with it.

    February 5, 2007 at 12:52 pm
  • Nilnandan


    I have set long_query_time=5 in my.cnf in mysyl server.
    but although i got those queries in slow-query-log which has query_time=0 second.
    means i got that query which has taken execution time is below 5 seconds.
    How can i solve this issue? Pls help me…its urgent.

    Thanking you,
    Nilnandan Joshi

    April 7, 2007 at 4:45 am
  • rs

    what to sort by (t, at, l, al, r, ar etc)

    what’s difference between these option above?

    June 6, 2007 at 11:58 pm
  • Daniel

    Is it possible to trace every SQL statement executed inside a procedure that is called by a client:

    COMMENT ‘Generate the required number of random battles’


    The problem is that the mysql-slow.log file only contains a trace of the call to the procedure; it does not provide any trace of every subsequent statement executed by the procedure itself:

    # Time: 070607 1:23:04
    # User@Host: dbo[dbo] @ localhost []
    # Query_time: 0 Lock_time: 0 Rows_sent: 0 Rows_examined: 2 SET
    last_insert_id=27274; CALL foo();

    Is there any way to configure mysqld to trace every statement executed by MySQL?

    June 8, 2007 at 1:51 pm
  • Andrew

    Unless I’m missing something, it seems that there is a bug in all of these patches. I’m pretty sure the line in that reads:
    newtime/= (frequency * 1000000);
    instead should be:
    newtime/= (frequency / 1000000);

    Please confirm!

    June 29, 2007 at 12:04 pm
  • Andrew Schwartz

    Another issue:
    In the patch at:

    line 2192 should read:
    if ((ulong) (thd->start_timer – thd->timer_after_lock) >

    if ((ulong) (thd->start_time – thd->time_after_lock) >

    July 2, 2007 at 3:40 pm
  • Thomas Bühren

    Did you think of using a mySQL Proxy script for creating the log file instead of a patch for the mySQL server?

    This would make it much easier to use:
    1. No need to compile the mySQL server.
    2. Logging (use of mySQL Proxy) can be switched on and off by redirecting the port with iptables:

    The mySQL Proxy script could write a logfile in the slow-log format so that your tool can use it.

    July 26, 2007 at 8:06 am
  • peter

    MySQL Proxy is good but it can’t give you some information – for example number of rows examined which is quite hanly to have.

    Plus for serious web sites I think minor patch is less intrusive then putting Proxy infront to handle your queries which has unknown overhead and potential side effects.

    July 26, 2007 at 10:00 am
  • Mads


    Just tried applying the latest patch
    to the appropriate sources
    and got
    Error C2143: syntax error : missing ‘)’ before ‘;’ @ c:\mysql-5.0.41-slowmicro\sql-common\my_time.c line 1244
    The fix is – suprisingly 🙂 – to add a ‘)’ before ‘;’ at line 1244 @ my_time.c line 1244, so it is not really a big issue. Still it would be nice if you could update the patch



    August 22, 2007 at 5:14 am
  • Vadim


    Thank you for fixes, I applied them in patches!

    September 1, 2007 at 8:59 am
  • Vadim


    Thanks, I fixed that in the patches.

    September 1, 2007 at 9:00 am
  • raptor


    # User@Host: root[root] @ localhost []
    what does this — ‘[root]’ signify in the above statement in slow query log?
    Is root@localhost is not enough?

    Thanks and Regards,

    December 2, 2007 at 10:22 pm
  • Code4Gold

    Thank you so very much for this wonderful article. I have been having an incedible amount of trouble with searches on my SMF forum timing out in Apache because mysql was taking too long to run the search query. After reading this article, I was able to find my problem was that the search query was creating a temp table and populating it with topic ID’s and then building the results off that. After analyzing the long query log, I was able to write a more much more efficient SQL statement to get the job done.

    Again, thank you, thank you, thank you for posting such advanced and useful information.

    December 14, 2007 at 9:05 pm
  • huma

    Would you please help me to apply patch.slow-micro.5.0.45.diff?
    Shall I change mysql path in the patch?
    my mysql is here : /usr/local/mysql-5.0.45-linux-i686


    December 31, 2007 at 5:13 am