We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Big lag on Model::find()

I am new with Phalcon and have the following problem:

The model is:

    class Test extends \Phalcon\Mvc\Model
    {
        public $id;
        public $label;

        public function getSource()
        {
            return 'test';
        }
    }

In the controller:

    public function indexAction()
    {
        $t1 = microtime(true);
        // Testing just connect
        $this->di['db']->connect();
        $t2 = microtime(true);
        // Get all the rows
        $t = Test::find();
        $t3 = microtime(true);
        // Output some data
        foreach ($t as $i)
        {
            print $i->id;
        }
        $t4 = microtime(true);
        // Output result
        print '<pre>Connect: ' . (round(($t2 - $t1) * 1000000) / 1000) . ' ms</pre>';
        print '<pre>Find all: ' . (round(($t3 - $t2) * 1000000) / 1000) . ' ms</pre>';
        print '<pre>Foreach: ' . (round(($t4 - $t3) * 1000000) / 1000) . ' ms</pre>';
    }

And the output is:

    1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
    Connect: 2.308 ms
    Find all: 2402.488 ms
    Foreach: 0.554 ms

I can't find why there is such lag for model::find(). Any ideas and/or suggestions on it?



43.9k

Interesting,

I've reproduced your code:

Connect: 0.893 ms
Find all: 2.729 ms
Foreach: 0.159 ms

I'm running debian stable with mysql as db for the test, on a 5 years old PCs .

Maybe you should investigate your db configuration ...

It comes to personal hosting account, Linux (I thing redhat) with MySQL 5.1.73 and I do not have access to the system.

Otherwise the Phalcon app db config is absolutely usual:

$di->set('db', function()
{
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        'host'      => 'localhost',
        'username'  => '...',
        'password'  => '...',
        'dbname'    => '...'
    ));
});

On just connect function everything is fine and more - adding:

    $result = $this->di['db']->query("select * from test");
    $t5 = microtime(true);
    print '<pre>Raw query: ' . (round(($t5 - $t4) * 1000000) / 1000) . ' ms</pre>';

resulting:

Raw query: 0.114 ms


43.9k

well, while saying "db config", I was thinking on mysql setup, not phalcon db config ...

Are you using some kind of cache system ?

BTW, for me, using raw query is more or less ten time faster as ORM.

I'm not using cache system and the problem exists in same way on the different hosting account on same server (I tested it). I agreed the raw query is much faster, but the problem rankles me... I did not found any occurrence in the web. On my local machine everything is fine too... And I wrote here so someone who faced such a problem can solution