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

Model fetch performance

So I have this minimal model

class Potd extends \Phalcon\Mvc\Model
{
    /**
     * @var int $id
     */
    public $id;
}

and this code:

$items = Potd::find([
     'mode' => 2,
     'order' => 'date', // DESC
]);
echo count($items);

It works ok, but it take about 150 ms.l, while this plain code:

$st = $this->di->get('db')->prepare('SELECT * FROM potd WHERE mode = :mode ORDER BY :order');
$st->execute(['mode' => 2, 'order' => 'date']);
$res = $st->fetchAll();
$items = [];
foreach ($res as $item) {
    $model = new Potd();
    $model->id = $item['id'];
    $items[] = $model;
}

echo count($items);

runs in about 35 ms.

Actually, Phalcon is only 16% faster than gigantic symfony2 with doctrine in this particular task. Here is apache benchmark results ab -c 20 -n 1000 localhost:

Phalcon: 23.29 RPS

Symfony2: 19.80 RPS

Plain PDO: 131.18 RPS

I understand that Phalcon is a framework and it should be slower than plain php, but 5 times slower? Also, I don't know what exactly going on inside find, but it looks pretty simple for me: construct query, fetch items, fill objects. So, the question: am I doing something wrong?



125.8k
Accepted
answer
edited Jul '15

Unless you've defined the structure of the potd able, the first thing Phalcon does in a request is to query and determine the structure of each table it's querying. If you look at the query log, you'll see queries that get the structure of potd, before any "find"-type query is done. Phalcon needs to find out what columns it's going to be getting back and a bunch of other stuff.

If you enable model meta data caching, you'll see that performance increase - I'd guess by quite a bit. I usually keep it turned off in development though, as if I make a change to the database, I never remember to flush the model meta cache.



17.5k

I use the ORM and don't experience this problem. I get ajax round trips in under 100ms on a cheap Digital Ocean web server and even cheaper Digitial Ocean DB server.

edited Jul '15

@quasipickle Thanks, caching metadata to file works well: now request take 80-90ms, and I believe file caching is not a fastest one. UPD: sorry, there was a quesion about changing this behavior, but I found answer in manual.