cPanel Config

Welcome to cPanelConfig the fastest growing cPanel configuration and troubleshooting guide on the internet. Please take the time and register. We would love to have your contribution to this completely free cPanel resource. We are updating this daily so be sure to visit us on a regular basis.

Identifying slow MySQL queries

1 Star2 Stars3 Stars4 Stars5 Stars (32 votes, average: 3.75 out of 5)
Loading ... Loading ...

Posted by yolau

MySQL can sometimes create big problems on a server when you have users abusing it.
This article will teach you how to correctly identify the queries that are creating a problem for your server.


MySQL can log those queries that are taking longer then X seconds but this future is not turned on by default.
Here’s how you turn it on:

  1. Login to your server as root
  2. Open my.cnf with your favorite editor. Example:
  3. pico /etc/my.cnf

  4. Into the [mysqld] section add the fallowing lines
  5. log-slow-queries = /var/log/mysql-slow.log
    long_query_time = 3

    This is just an example. You can use any file name that you want and you can modify the long_query_time to any value. In this example I will be logging to /var/log/mysql-slow.log any queries that are taking longer then 3 seconds.

  6. Go ahead and save the configuration.
  7. For pico: CTRL+X and YES

  8. Now we have to actually create the log file.
  9. touch /var/log/mysql-slow.log

  10. Now we are changing the owner of the file so that mysql and actually write to it.
  11. chown mysql.root /var/log/mysql-slow.log

  12. Now we restart mysql
  13. service mysql restart

    It should restart successfully. If it doesn’t check that you didn’t brake my.cnf

  14. Wait a few minutes and then examine the slow queries log
  15. A few examples on how to do it:

    cat /var/log/mysql-slow.log
    tail /var/log/mysql-slow.log
    tail -50 /var/log/mysql-slow.log

After you have identified the offending query go ahead and optimize or remove it.
Again test the results by looking at your server load and the mysql slow queries log.

After you fixed all the problems go ahead and comment the slow queries logging as it will slow your server a bit if you let it on. my.cnf should now look similar to this:

#log-slow-queries = /var/log/mysql-slow.log
#long_query_time = 3

And don’t forget to restart MySQL after this.

service mysql restart


Hope this helps !

I suggest you also read:

If you have any problems or suggestions don’t hesitate to leave a comment.

Share this:
  • Digg
  • del.icio.us
  • Slashdot
  • StumbleUpon
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • De.lirio.us
  • Furl
  • MyShare
  • Smarking
  • Technorati
  • YahooMyWeb

If you enjoyed this post, make sure you subscribe to my RSS feed!

49 Responses to “Identifying slow MySQL queries”

  1. [...] teach you how to correctly identify the queries that are creating a problem for your server.Mas: http://www.cpanelconfig.com/optimize-a-cpanel-server/identifying-slow-mysql-queries/ [...]

  2. try

    tail -F /var/log/mysql-slow.log

    if you want to keep an eye on the log file

  3. [...] Blakovitch wrote an interesting post today onHere’s a quick excerpt [...]

  4. ‘chmod mysql.root /var/log/mysql-slow.log’ should be changed to ‘chown mysql.root /var/log/mysql-slow.log’

  5. Wow… “pico” is still used? ;)

    Quick note: step 6 should be “chown”, not “chmod”.

  6. Wow, interesting article

  7. The web’s most interesting stories on Thu 13th Dec 2007

    These are the web’s most talked about URLs on Thu 13th Dec 2007. The current winner is

  8. [...] Identifying slow MySQL queries | cPanelConfig – cPanel server configuration guide [...]

  9. After you’ve found a slow query, you need to either re-write it or speed it up. Re-writing queries is very much dependent on how your software works, but speeding queries you basically need to add indexes…

    Look at the “where” clause of your query and try to find if there is a set of unique identifiers in there. For example, we can assume that a single user can only post one comment to a blog per second, therefore user ID and timestamp make a unique key.

    If you can find a unique key, create an index. I’m no MySQL expert but the syntax will be something like:

    CREATE INDEX myindex ON TABLE mytable (userid, timestamp);

    The documentation is at http://dev.mysql.com/doc/refman/5.0/en/create-index.html

    This is all very much beginners’ stuff but MySQL can scale pretty well so you should look to push it hard before shelling out on an Oracle license.

  10. [...] when you have users abusing it. This article tells you how to speed up your server.. sailor moon sexread more | digg [...]

  11. [...] Identifying slow MySQL queries | cPanelConfig – cPanel server configuration guide (tags: blog database find howto linux MySQL server sysadmin toread tutorial web webdesign webdev) [...]

  12. [...] users abusing it. This article tells you how to speed up your server.. X Men evolution ascensionread more | digg story RSS feed for comments on this post. TrackBack URI Cartoons Fans Lounge [...]

  13. thanks for the information and happy holidays!

  14. Hi…Man i love reading your blog, interesting posts ! it was a great Thursday .

  15. Hi there…Man i love reading your blog, interesting posts ! it was a great Thursday . Vanna White

  16. Why not just use tuning scripts to check your server configuration?

  17. [...] Source [C Panel Config] [...]

  18. GNC-2007-12-14 #325

    I give away three cool products more info in the show notes. Congrats to the winners more prizes to give away through the end of Sponsors: Special Promotion code 20% off on 1 Year Shared Hosting Plans use Godaddy Code…

  19. @andy

    Try the command

    describe your-sql-command-here, for instance:

    describe select * from stats where date(date)=’20071212′ order by server;

    The result is an analysis of your query, giving some insight in the performance problem.

  20. [...] users abusing it. This article tells you how to speed up your server.. wonder woman animation pornoread more | digg [...]

  21. [...] abusing it. This article tells you how to speed up your server.. futurama scene frame grab clipread more | digg [...]

  22. I would suggest us “VI”

  23. “following” is spelled wrong.
    That hurts the author’s credibility.
    And nano replaced pico a while back.

  24. I couldn’t understand some parts of this article ying slow MySQL queries | cPanelConfig – cPanel server configuration guide, but I guess I just need to check some more resources regarding this, because it sounds interesting.

  25. [...] Link: cPanelConfig [...]

  26. Hi there…Man i just love your blog, keep the cool posts comin..holy Wednesday

  27. [...] Identifying slow MySQL queries | cPanelConfig – cPanel server …An article on how to identify MySQL slow queries and how to fix them. [...]

  28. Hello…I Googled for log analysis, but found your page about viewing slow MySQL queries | cPanelConfig – cPanel server configuration guide…and have to say thanks. nice read.

  29. Hello webmaster…Man i love reading your blog, interesting posts ! it was a great Wednesday

  30. What do you mean ?

  31. Very very useful, thank you very very much :)

  32. Nice and useful, thank you

  33. [...] Más información AQUI [...]

  34. Interesting article, thanks.

  35. [...] Identifying slow MySQL queries [...]

  36. [...] Link [...]

  37. [...] Identifying slow MySQL queries [...]

  38. Very much to me has helped! Thanks you!

  39. [...] Identifying slow MySQL queries [...]

  40. [...] Identifying slow MySQL queries [...]

  41. [...] Identifying slow MySQL queries [...]

  42. [...] Identifying slow MySQL queries [...]

  43. super useful

  44. Very useful. Thanks!

  45. Yeah!! It works fine for me. Thank You !

  46. This is one of the things I’ve been meaning to do for a while now. I’ll give it a try shortly. Fingers crossed.

  47. [...] Identifying slow MySQL queries [...]

  48. very good post and useful. Thank you

  49. Interesting Article. Thanks.

Leave a Reply