I have a PHP/Mysql script running on a apache/php4 server (php4 is just temporary, until a new Server is ready...), and I noticed some weird be behaviour.

Requests, that use some php an a few simple mysql queries, are usually quite fast. But if I send the same request to the server 10 times (one after the other, no heavy load at all) maybe 8-9 times it will finish within 50ms. But one or two of the requests will take over 5 seconds each. Nothing in between, just ~50ms or several seconds.

It seems very random (not the first request slow, and the following fast or anything like that) and I can't find the reason.

Does anyone have an idea what might cause this problem and how to fix it?

  • thats what cache does
    – G molvi
    May 30, 2011 at 15:50
  • 2
    You may want to have PHP5 before worrying about performance.
    – zneak
    May 30, 2011 at 15:51
  • 50ms, you meant seconds?
    – BeingSimpler
    May 30, 2011 at 15:53
  • 1
    no it's not what cache does, what cache does is first query slow, following fast. not same or similar queries first few fast, the one slow, again some fast etc. Maybe it's really a PHP4 thing, too bad i don't have the permission to update or move to another server on my own...
    – Ruu
    May 30, 2011 at 16:23
  • @BeingSimpler: i meant 0.05 seconds on normal queries vs. 5 secondson the slow ones
    – Ruu
    May 30, 2011 at 16:27

2 Answers 2


It's difficult to pinpoint the exact cause of a slow performing server from just a brief description, you're going to want to do some benchmarking to target the cause of the slow down. For example...

  1. It could be apache's configuration (max clients could be set to a really low number - this is doubtful however). Try apache benchmark (ab)

  2. It could be poorly constructed MySQL queries causing table locks (depending on your storage engine for each table accessed) or the MySQL connection limit being really low (check this)

  3. It could be that your PHP scripts are really intense, and your server just doesn't have the hardware to support that level of activity.

  4. EDIT: Take a look at munin which will give you a comprehensive overview of your server performance, then monitor it as you make the requests.

It's all very subjective. Maybe someone else could point out some other potential issues.

  • i know the description is very vague, because i can't see any pattern/reason for this... the mysql querys are fast (not much data because just a few datasets for testing, no big joins) and the test server (2X Pentium4 2.8ghz) is pretty much idle, cpu usage maybe 1-3%. Just randomly these very slow requests
    – Ruu
    May 30, 2011 at 16:04
  • hopefully munin can help you pinpoint the problem (updated my answer with a link)
    – Jaitsu
    May 30, 2011 at 16:06

As others point out, it can be difficult to pinpoint the problem. However you can do some checking on the execution of the script. If the problem is visible to PHP, it will most likely be between PHP or the database.

You could do some execution checking in the script by doing something like:;

function checkpoint(){
   $mtime = microtime(); 
   $mtime = explode(" ",$mtime); 
   $mtime = $mtime[1] + $mtime[0]; 
   return $mtime;

function measureGaps($data){
    $size = count($data);
    for($i=0; $i < $size - 1; $i++){
      $p = $i+1;
      $gap[]= $data[$p] - $data[$i];
    return $gap;

At each checkpoint, you can do:

$checkpointArray[] = checkpoint();

At the end of the script, call the other function:


If you don't want to do a var_dump, change the output to write to a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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