user526929
user526929

Reputation: 93

very slow response from CakePHP when performing a simple DB query

I am new to CakePHP framework and is trying to setup a "Hello World" project. When working on it, I experienced a very slow response from CakePHP when performing a simple DB query.

Here are the steps that I have done:

  1. Download the CakePHP framework (2.3.0 RC1) and have it setup
  2. Create a "Test" DB with an empty table named "Tests".
  3. Edit the default AppController.php file as following:

    class AppController extends Controller {
    
        var $uses = array('Test');
    
        function say_hello() {
        $this->Test->query("select * from test where id=0");
        echo "hello";
        }
    }
    
  4. After that, I accessed the link "http://localhost/app/say_hello" and it took more than 1s to respond.

If I commented out the query statement as following:

class AppController extends Controller {

    var $uses = array('Test');

    function say_hello() {
        //$this->Test->query("select * from test where id=0");
        echo "hello";
    }
}

Then, it only took around 60ms to respond.

This doesn't seem right to me as performing a simple query on an empty table shouldn't take ~940ms. I have tried debuging with DebugKit and it showed that the ControllerAction (in this case, the say_hello action is pretty simple) was taking more than 1s of the processing time. Also note that the slowness issue was not caused by DB as DebugKit showed that there was only one query when executing the say_hello action and that query almost took 0ms to complete.

I am not sure what is causing such slowness. Can any experienced CakePHP members shed me some light on what's wrong in this case? What else I should do to troubleshoot and fix the issue?

Thanks.

Upvotes: 1

Views: 5486

Answers (2)

flyysr
flyysr

Reputation: 1

Please check that your webserver and the database server are on the same physical machine.
If they are on the same physical machine, the time it takes should be less, usually a few hundred milliseconds, If they aren't on the same physical machine, then the network delay will matter. You can dive into the source code of CakePHP framework, it uses the PDO to connect the database and call some functions of PDO, every time will takes a quite time because of the network delay time. So this solution may be unapporiate, you should write a specific API, then you PHP code to call the API instead of querying the database directly.

Upvotes: 0

Martin Samson
Martin Samson

Reputation: 4090

While in debug mode, CakePHP will refresh your database bindings, object cache and schema within a short amount of time, describing tables mapped to defined models.

Before testing a CakePHP app performance, make sure you change the debug level to 0.

Upvotes: 7

Related Questions