We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Why is DESCRIBE query called and is there a way to reduce it?

Hey guys!

So I am trying to understand my performance.

I have a application built on INVO, phalcon 3.4.7. Empty controller, only in BaseController I call a user object.

Thing is DESCRIBE QUERY is called, even when the object is cached. And it is a majority of my loading time. I dont understand why. Whats the point of cache if 90% of the load (Describe) is still called? My understand is DESCRIBE is called because I call a user object. Can I optimize this in some way?

https://i.imgur.com/PgoRlrI.png Image

<?php

use Phalcon\Mvc\Controller;
use Phalcon\Mvc\View;
use Phalcon\Cache\Backend\File as BackFile;
use Phalcon\Cache\Frontend\Data as FrontData;

class ControllerBase extends Controller {

    public $user;

    protected function initialize() {

///////////////// TESTING!! //////////////////
        require_once APP_PATH . 'vendor/autoload.php';
        $profiler = new \Fabfuel\Prophiler\Profiler();
        $profiler->addAggregator(new \Fabfuel\Prophiler\Aggregator\Database\QueryAggregator());
        $profiler->addAggregator(new \Fabfuel\Prophiler\Aggregator\Cache\CacheAggregator());
        $logger = new \Fabfuel\Prophiler\Adapter\Psr\Log\Logger($profiler);

        $pluginManager = new \Fabfuel\Prophiler\Plugin\Manager\Phalcon($profiler);
        $pluginManager->register();

///////////////// TESTING!! //////////////////

        $this->view->setTemplateAfter('main');

        if ($this->cookies->has('remember-me') AND is_null($this->session->get('auth'))) {
            // Get the cookie

            return $this->response->redirect('session/cookie_login');
        }
        else{

            if(!is_null($this->session->get('auth'))){
                $auth = $this->session->get('auth');
                $id = $auth['id'];

                $frontCache = new FrontData(
                    [
                'lifetime' => 60, // Daily
                    ]
                );

                if (file_exists('../app/cache/' . $id . '/') == false) {
                    mkdir('../app/cache/' . $id . '/', 0777, true);
                }

                $backend = new BackFile(
                    $frontCache,
                    [
                        'cacheDir' => '../app/cache/' . $id . '/',
                    ]
                );

                $cache = new \Fabfuel\Prophiler\Decorator\Phalcon\Cache\BackendDecorator($backend, $profiler);

                $cacheKey = $id . '_user.cache';

                $results = $cache->get($cacheKey);

                if ($results == null) {

                    $auth = $this->session->get('auth');

                    $user = Users::findFirstById($auth['id']);
                    if ($user) {

                        $results = $user;
                        $this->user = $results;
                        $this->view->setVar("auth_user", $this->user);
                        $cache->save($cacheKey, $results);

                    }
                }
                else{
                    $this->user = $results;
                    $this->view->setVar("auth_user", $this->user);
                }
            }

        }

        $toolbar = new \Fabfuel\Prophiler\Toolbar($profiler);
        $toolbar->addDataCollector(new \Fabfuel\Prophiler\DataCollector\Request());

        $this->view->setVar("profiler", $profiler);
        $this->view->setVar("logger", $logger);
        $this->view->setVar("toolbar", $toolbar);

    }


8.4k
edited Dec '20

what you need is caching models metadata

documentation for 3.4 https://docs.phalcon.io/3.4/en/db-models-metadata

the default adapter is memory and you can change it to using files by changing the service

then it will be cached and don't forget to delete these files if you made changes to the schema of your tables

use Phalcon\Mvc\Model\MetaData\Files as MetaDataAdapter;

$di->setShared('modelsMetadata', function () {
    $config = $this->getConfig();
    return new MetaDataAdapter(['metaDataDir' => $config->application->cacheDir]);
});

assuming you are using INVO v0.6.0 edit the method initModelsMetadata() @ app/Services.php


use Phalcon\Mvc\Model\MetaData\Files as MetaDataAdapter;

protected function initModelsMetadata()
{
    return new MetaDataAdapter(['metaDataDir' => APP_PATH . 'cache/']);
}