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.

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+.
This comment has been removed by the author.
ReplyDeleteIEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
DeleteJavaScript Online Training in India
JavaScript Training in India
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
Hi Peter
ReplyDeleteI just wonder if I set log-output=TABLE, what would the table name be by default?
This comment has been removed by the author.
ReplyDeleteThanks for sharing! This page was very informative and I enjoyed itwordpress developer company.
ReplyDeleteWordpress Developer
Very Helpful thanks!
ReplyDeleteThis very great information, keep more share your updating. Website Designing Company Bangalore | Website Development Company Bangalore
ReplyDeleteI 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.
ReplyDeleteEngineering Colleges in Chennai, Mechanical Engineering Colleges in Chennai
I am a regular user of your post, this one also was very interesting and well written. keep sharing the great work
ReplyDeleteWeb Designing Company Bangalore | Website Designing Company Bangalore
Thanks for sharing a wonderful post. Keep Sharing more post.Web Development Company Bangalore | Website Development Company Bangalore
ReplyDeleteWeb Design Sydney: It is a great sharing...I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.Logo Design Sydney
ReplyDeleteNetguru 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.
ReplyDeleteWebsite 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
Excellent comments with interesting topics and very useful resources. Thanks for sharing...
ReplyDeleteTop web development services in bangalore
ECommerce Website developers in bangalore
oh my god... i got a great information from this blogger....
ReplyDeletethis is very needful information...thanks for sharing the great article..
Web Design Company Bangalore
Website development Company Bangalore
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.
ReplyDeleteGeneric Daklinza
ReplyDeleteDaclatasvir 60mg
Natdac 60mg
Mydekla 60mg
Daclahep 60mg
Dacihep 60mg
Hepcfix 60mg
Hi,
ReplyDeleteThanks 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
Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
ReplyDeleteSEO company in coimbatore
SEO company
web design company in coimbatore
Thanks for an insightful post.These tips are really helpful. Thanks a lot.Keep it up.Keep blogging.!!
ReplyDeletesoftware development services in lahore 2019
Nice post. It is really interesting. Thanks for sharing the post!
ReplyDeleteWeb Design Company in Tuticorin | Domain Name Registration In India
Web Design Company In India | Digital Marketing Company in India
PPC Services in India
Nice post. It is really interesting. Thanks for sharing the post!
ReplyDeleteBuy AC Online | Smart LED TV
Laptops for Sale | Best Inverter AC
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.
ReplyDeleteCard Game Development
Nice post really useful information. We are the leading website developer in dubai. Hire our website design company in dubai today for web design services in dubai
ReplyDeleteNice post it is really an interesting article.We are also providing the website design services in mumbai. We are
ReplyDeletebest website design companies in mumbai
best website designers in mumbai
DigisolHub
ReplyDeleteDigisol Hub is a Digital Marketing & Tech. Company, that provides solutions regarding website development, SEO, Social Media Marketing, Google Ads, and Graphics designing, App development, Software development, Website Development. We just want to solve the problems of the people by using the right digital marketing strategy and give them Value so that they can build their business in the Online world
This is a good time to make long term plans and it's timely.
ReplyDeleteHave fun. I have read this post, and if you will excuse me, I would like to advise you on interesting topics or tips.
You can write the following articles on this topic.
I want to read more topics on this topic!
english stories english short stories with moral value What is the factorial of 100
Need the Azure Interview Questions And Answers? Get this into the blog with Infycle Technologies for having the top interview questions and answers! Call 7504633633 or 7502633633 for having the best software development courses!
ReplyDelete