2

I have a server (VPS) that hosts numerous PHP / MySQL websites. Most are quite similar in that they are all hand-coded websites serving text and images from MySQL databases.

Server traffic has increased a fair amount recently and the server is experiencing some slow down. As such I want to try and identify bottle necks in the server so that I can improve the server's speed.

Does anyone have any tips on how to do this? I have setup timing scripts on some of my larger sites to see how long it takes for the webpages to be created but its always a really low figure. According to the server stats the main issue seems to be CPU / MySQL usage. Is there anyway to identify queries that are taking a long time?

Thanks Chris

2
  • traffic increase usually lead to server load, so what is the problem? And explain the fair amount
    – ajreal
    Dec 9, 2011 at 9:54
  • Uhm its over 1million impressions a month. And no problem, its good, but I want to optimise my site rather than just buy a bigger server.
    – Chris
    Dec 9, 2011 at 10:01

4 Answers 4

10

If youre using mysql >= 5.1, you can use mysql_query("set profiling=1"); in your script, like this:

mysql_query("set profiling_history_size=100");
mysql_query("set profiling=1");

....
....
any mysql query
....
....

$rs = mysql_query("show profiles");
while($rd = mysql_fetch_object($rs))
{
    echo $rd->Query_ID.' - '.round($rd->Duration,4) * 1000 .' ms - '.$rd->Query.'<br />';
}

Example output:

enter image description here

2
  • What kind of result would I get from that? Can it be used around multiple queries or just one?
    – Chris
    Dec 9, 2011 at 10:13
  • Multiple query: Star the profilling at the top of your script, and print the result at the bottom of your script.
    – Zul
    Dec 9, 2011 at 10:18
4

Start from the firebug's Net tab and see what resources have slowest response.

But I can tell you even without any profiling that

serving ... images from MySQL databases

being at least one of the reasons.

4
  • Hey thanks but the actual HTML construction of the sites is fast, everything is hand coded and very lean. The issue, I believe, is in the backend scripts that I dont know how to profile :)
    – Chris
    Dec 9, 2011 at 9:58
  • what are these "backend" scripts? you didn't mention them. also, "everything is hand coded and very lean" does not sound as a profiling resilt. Dec 9, 2011 at 10:03
  • By backend scripts I mean the PHP / MySQL. The HTML, CSS and javascript is very lean. The issue is with server performance not the webpage content itself.
    – Chris
    Dec 9, 2011 at 10:12
  • it is not backend scripts it is called frontend scripts. With firebug's Net tab you can determine which is slowest. A good place to start. Dec 9, 2011 at 10:22
3

Yes, there is a way! MySQL has a built-in feature for this. You can set up a log file to log slow queries.

Other general advice would of course be to use EXPLAIN on common queries and check if everything is indexed properly.

1

XDebug has a profiling tool that gives you a lot of information about the bottlenecks in your code. Check out http://xdebug.org/docs/profiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.