• Call A Developer Google+
  • Like Us on Facebook
  • @calladeveloper on Twitter
  • Subscribe to our Blog
  • Skype: calladeveloper
  • Call us on Gtalk

Saturday, March 23, 2013

Enabling the Slow Query Log in MySQL without service restart - Database Optimization Basics

This is the first article in a series on MySQL Database Optimization Basics. Subscribe to be notified when new articles are added!

Today we're going to cover how to log slow queries in mysql so that you can identify problems and refactor the correct queries in order to improve the performance of your website or application. Slow queries can cause errors when people use your website or application and having them will make your code difficult to scale. The first step toward fixing them is seeing which queries are slow in your code, so let's start by learning how to enable slow query logging without restarting the mysql service:

How to enable slow query log in MySQL without restarting mysqld

In MySQL 5.1.12 and later, this is fairly straightforward and can be done without restarting mysql as follows:

*Note: this requires command line access over SSH as root, so it is only applicable to VPS and Dedicated Servers. I am working on a Centos 5 VPS with cPanel, but this will work on most Linux and UNIX versions (paths may be different).

root in [~]# touch /var/log/slow
root in [~]# chown mysql:mysql /var/log/slow
root in [~]# mysql -e 'SET GLOBAL slow_query_log=1;'
root in [~]# mysql -e 'SET GLOBAL slow_query_log_file="/var/log/slow";'
root in [~]# mysql -e 'SET GLOBAL long_query_time=2;'

If you want to leave slow query logging on (recommended), you will need to add 3 lines to my.cnf under the [mysqld] section:

slow_query_log=1
slow_query_log_file=/var/log/slow
long_query_time=2

To test if it is working correctly (you should test after changing things, I promise it's a good idea) - simply run a SELECT SLEEP(x) query where x is longer than the long_query_time you have set - the query should be logged to the file you specified for slow_query_log_file (as long as it exists and the mysql user can access it).

# mysql -e 'SELECT SLEEP(5);'

Note: you don't have to have any databases or tables to use SLEEP().

Here is what it looks like if everything worked correctly. If not, make sure that the log file you specified exists and mysql user has access, and also that your GLOBALs are set correctly.

Enable MySQL Slow Query Logging for Optimization and Tuning

Here is a summary of all of the different ways to turn on slow query logging in various versions of MySQL:

  • Before 5.1.6, start mysqld with the --log-slow-queries[=file_name] option.
  • In versions after MySQL 5.1.6, you can log a file or a table, or both. Start mysqld with the --log-slow-queries[=file_name] option and optionally use --log-output to specify the log destination.
  • After MySQL 5.1.12, use --slow_query_log[={0|1}] - 1 is on and the default slow query log file name is used.
  • After MySQL 5.1.29, use --slow_query_log[={0|1}] - the --log-slow-queries option is deprecated.
  • When using slow_query_log=1 (either in my.cnf or starting with mysqld --slow_query_log=1) use slow_query_log_file=/your/log/file or just leave it as the default ($hostname-slow.log in the mysql data folder).

More info about slow query logging at the official MySQL Reference.

If you want to enable slow query logging without restarting mysql on MySQL 5.0, you can - use this patch

Need help with MySQL Performance Tuning or Query Optimization? Call A Developer.

Peter Stolmar is a Linux Systems Administrator and technology optimization specialist. He can help you fix websites and apps, or make then run faster and scale better. Need help with some tech stuff? Find Peter Stolmar on Google+.

17 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi Peter
    I just wonder if I set log-output=TABLE, what would the table name be by default?

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanks for sharing! This page was very informative and I enjoyed itwordpress developer company.
    Wordpress Developer

    ReplyDelete
  5. I am always searching online for articles that can help me to improve my knowledge. There is clearly a lot to know about MySQL.I think you made some good points in Features also.By the way you are running a great blog. So, please keep on update like this post.
    Engineering Colleges in Chennai, Mechanical Engineering Colleges in Chennai

    ReplyDelete
  6. I am a regular user of your post, this one also was very interesting and well written. keep sharing the great work
    Web Designing Company Bangalore | Website Designing Company Bangalore

    ReplyDelete
  7. Netguru Solution India Pvt. Ltd. is one of the leading and developing Website Design and Mobile Application Development Company in Pune. We make website design and development simple by providing answers to address your issues. With our group’s joined involvement in visual communication, site advancement, search engine optimization and activity, you can be sure that whatever your venture requests, our web engineers can convey on time. We offer Website Design, Web Development, Mobile Application Development, Android App Development, iOS App Development, SEO, Internet Marketing, Web Maintenance and Hosting, Web Branding and Vector Art Solutions.
    Website Design Company In Pune
    Web Design Company In Pune
    Website Development Company In Pune
    Mobile Application Development Company In Pune
    Android App Development Company In Pune
    iOS App Development Company In Pune

    ReplyDelete
  8. Excellent comments with interesting topics and very useful resources. Thanks for sharing...
    Top web development services in bangalore
    ECommerce Website developers in bangalore

    ReplyDelete
  9. oh my god... i got a great information from this blogger....

    this is very needful information...thanks for sharing the great article..

    Web Design Company Bangalore
    Website development Company Bangalore

    ReplyDelete
  10. Shraddha IT solutions is professional best website designing and development software company in pune providing top services like Branding Logo Design , Brochure designer , Mobile app development , ecommerce web development web hosting , cms , best web development company in pune that boost your business in pune ,usa ,India.

    ReplyDelete
  11. Hi,

    Thanks for sharing a very interesting article about Enabling the Slow Query Log in MySQL without service restart - Database Optimization Basics. This is very useful information for online blog review readers. Keep it up such a great article like this.

    Regards,
    WondersMind,
    Best Web Design Company in Bangalore

    ReplyDelete
  12. Thanks for an insightful post.These tips are really helpful. Thanks a lot.Keep it up.Keep blogging.!!
    software development services in lahore 2019

    ReplyDelete
  13. I was looking for this type of valuable content and finally I got this blog. I found it very beneficial for me. You are an excellent blogger and deserves numerous compliments. Thank you so much for sharing this wonderful information.
    Card Game Development

    ReplyDelete
  14. Nice post it is really an interesting article.We are also providing the website design services in mumbai. We are
    best website design companies in mumbai
    best website designers in mumbai

    ReplyDelete