Mysql – PHP request sometimes very slow

apache-2.2MySQLPHP

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?

Best Answer

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.