Tip Details Add a Related Tip

Rate as: Positive Negative

Finding slow MySQL queries

To find slow queries in MySQL, you need to turn on logging in your configuration file. You can set up the logging so that every time a query takes longer than a certain amount of time, the query gets logged in a file. You can then examine the file to find your bottlenecks and fix them. Here are the steps to turn on the logging.

You need to edit your my.cnf file which is typically found in the /etc directory. Something like so should do it: vi /etc/my.cnf. Depending on how your server is set up, you may need root access to perform this edit.

Once opened, edit the my.cnf file by adding the following lines in the [msysqld] section:

log-slow-queries = /var/log/slow-query.log
long_query_time = 2

In this example, the slow queries will be logged into a file called /var/log/slow-query.log, you can set this to anything you like. In addition, this example logs any query that takes longer than 2 seconds. You can adjust this value to whatever you like.

For this to work, you need to create the file into which the logging will occur (/var/log/slow-query.log in this case) and give it update the owner of the file to be the mysql user (using the chown mysql /var/log/slow-query.log command once the file is created).

Once the log file is in place, restart your mysql server (service mysql restart) and the logging will begin.

Once you find your slow queries in the log and fix them, it's a good idea to once again update the my.cnf file to remove or comment out the lines that cause the logging to happen. The logging does increase the load on the database, so once things are in order you don't want that additional overhead. You can turn it on occasionally to make sure that your performance is still good.
Rating: 100% positive, 2 total Votes
Categories: MySQL databases logging server configuration
Added: on Jun 24, 2008 at 9:22 pm
Added By: dbdude

Comments on this Tip

Add a Comment
There are no comments for this tip
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

How to find a good and cheap web hosting?

100% positive, 0 comments
– Tip added by an anonymous user on Mar 24, 2008 at 3:15 am

MySQL Decode Equivalent

Categories: MySQL Oracle databases
no ratings, 0 comments
– Tip added by an anonymous user on Jul 10, 2008 at 5:00 pm

Why you should use MySQL

no ratings, 0 comments
– Tip added by an anonymous user on May 30, 2007 at 10:02 am

Minimize round trips to the server for best performance

100% positive, 0 comments
– Tip added by an anonymous user on Jun 18, 2008 at 4:07 pm

Importing databases in MS SQL

100% positive, 0 comments
– Tip added by an anonymous user on May 29, 2008 at 2:47 pm