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, 3 total Votes
Categories: MySQL databases logging server configuration
Added: on Jun 24, 2008 at 9:22 pm
Added By: dbdude
Searches: file mysql logging server slow

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

MySQLi Dumper

75% positive, 0 comments
– Tip added by an anonymous user on Feb 18, 2009 at 11:30 am

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

Optimize your MySQL database

100% positive, 0 comments
– Tip added by an anonymous user on Dec 07, 2008 at 5:04 pm

MySQL Decode Equivalent

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

Hide PHP server information

Categories: PHP server configuration
no ratings, 0 comments
– Tip added by an anonymous user on Aug 31, 2009 at 10:34 am

Optimizing your MySQL tables

no ratings, 0 comments
– Tip added by an anonymous user on Oct 21, 2008 at 11:18 am

Why you should use MySQL

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

Changing the port of a Grails project

100% positive, 0 comments
– Tip added by an anonymous user on Nov 04, 2009 at 7:09 am

Customize Thunderbird email folders

100% positive, 0 comments
– Tip added by an anonymous user on Oct 23, 2009 at 7:14 am

The table mysql.proc is missing, corrupt, or contains bad data

Categories: MySQL repair database
100% positive, 0 comments
– Tip added by an anonymous user on Oct 01, 2009 at 10:07 pm

Gestures to switch between tabs in Firefox

100% positive, 1 comment - last added on Jul 28, 2009 at 9:55 am
– Tip added by an anonymous user on Jul 09, 2009 at 1:02 pm

How to Repair Event ID 454 in Exchange Server

100% positive, 0 comments
– Tip added by aamani on Mar 25, 2009 at 4:13 am

PHP strftime errors in Smarty

no ratings, 0 comments
– Tip added by an anonymous user on Nov 03, 2009 at 3:04 pm

Keep screen from dimming when watching videos on a Blackberry

no ratings, 0 comments
– Tip added by an anonymous user on Oct 16, 2009 at 12:12 pm

Load balancing apache servers

no ratings, 0 comments
– Tip added by an anonymous user on Sep 29, 2009 at 7:30 am

Delete Records With Effect Using jQuery And PHP

Categories: php jQuery MySQL
no ratings, 0 comments
– Tip added by an anonymous user on Sep 27, 2009 at 7:10 am

Organizing your iPhone applications on home screen

Categories: iPhone configuration
no ratings, 0 comments
– Tip added by an anonymous user on Sep 21, 2009 at 4:38 pm

Turn off data roaming on the Blackberry Tour

no ratings, 0 comments
– Tip added by an anonymous user on Aug 16, 2009 at 8:18 am

MySQL equivalent to Oracle NVL

Categories: MySQL Oracle database
no ratings, 0 comments
– Tip added by an anonymous user on Jul 31, 2009 at 4:01 pm

Disable Location Aware Browsing in Firefox

no ratings, 0 comments
– Tip added by an anonymous user on Jul 09, 2009 at 9:33 am