nwalke
nwalke

Reputation: 3209

Slow Doctrine Find

I am trying to figure out why one of my doctrine finds is running so slow. I don't really know where to start, so please bear with me.

I do a pretty basic find to fetch a user object. This find is taking ~160ms. When I run the query via phpmyadmin, it takes .7ms.

$this->em->find('Entities\User', $userId)

I have already tried adding skip-name-resolve to mysql's my.cnf. The id field in the user table is indexed. I really don't know what else to try. Let me know if there is additional information I can provide.

Below is the entity file:

namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityRepository;

/** @Entity(repositoryClass = "Entities\UserRepository")
  * @Table(name="user") 
  */
class User extends \Company_Resource_AbstractEntity
{
    /** @Id @Column(type="integer") @GeneratedValue */
    protected $id;
    /** @Column(type="string") */
    protected $name;
    /** @Column(type="string") */
    protected $password;
    /** @Column(type="string") */
    protected $email;
    /** @Column(type="string") */
    protected $first_name;
    /** @Column(type="string") */
    protected $last_name;
    /** @Column(type="integer") */
    protected $password_reset;
    /** @Column(type="string") */
    protected $salt;
    /** @Column(type="integer") */
    protected $active;
    /** @Column(type="string") */
    protected $cookie_hash;

    /**
    * @ManyToOne(targetEntity="Company" , inversedBy="user")
    */
    protected $company;

    /**
    * @ManyToOne(targetEntity="Privilege" , inversedBy="user")
    */
    protected $privilege;

    /**
    * @OneToMany(targetEntity="CompanySubscription" , mappedBy="user")
    */
    protected $subscription;

    /**
    * @OneToMany(targetEntity="EquipmentEvent" , mappedBy="check_in_user")
    */
    protected $check_in;

    /**
    * @OneToMany(targetEntity="EquipmentEvent" , mappedBy="check_out_user")
    */
    protected $check_out;

    /**
    * @OneToMany(targetEntity="GroupEvent" , mappedBy="check_in_user")
    */
    protected $check_in_group;

    /**
    * @OneToMany(targetEntity="GroupEvent" , mappedBy="check_out_user")
    */
    protected $check_out_group;

    /**
    * @OneToMany(targetEntity="Maintenance" , mappedBy="submit_user")
    */
    protected $maintenance_submit;

    /**
    * @OneToMany(targetEntity="Maintenance" , mappedBy="completed_user")
    */
    protected $maintenance_complete;

    /**
    * @OneToMany(targetEntity="UserLogin" , mappedBy="user")
    */
    protected $login;
}

Abstract entity:

use \Doctrine\Common\Collections\ArrayCollection;

abstract class Company_Resource_AbstractEntity implements ArrayAccess
{
    public function offsetExists($offset)
    {
        return property_exists($this, $offset);
    }

    // The get/set functions should check to see if an appropriately named function exists before just returning the
    // property.  This way classes can control how data is returned from the object more completely.
    public function offsetGet($offset)
    {
        $property = new Zend_Filter_Word_UnderscoreToCamelCase();
        $method = 'get'. $property->filter($offset);
        return $this->{$method}();
    }

    public function offsetSet($offset, $value)
    {
        $property = new Zend_Filter_Word_UnderscoreToCamelCase();
        $method = 'set'. $property->filter($offset);
        return $this->{$method}($value);
    }

    public function offsetUnset($offset)
    {
        // can't do this
    }

    /*==-====-====-====-====-====-====-====-====-====-====-==*/

    /*
     * Provides magic method access for getFieldName() and setFieldName()
     * where field_name is a simple field and not a relation
     * A special getData implementation returns all of the current object vars
     */
    public function __call($method, $arguments)
    {
        preg_match('@^([a-z]+)(.*)@', $method, $matches);
        $action = $matches[1];
        $property = $matches[2];
        $underscore = new Zend_Filter_Word_CamelCaseToUnderscore();
        $offset = strtolower($underscore->filter($property));
        if ($action == 'get')
        {
            if ($property == 'Data')
                return get_object_vars($this);
            if ($this->offsetExists($offset))
                return $this->{$offset};
            else
                throw new Zend_Exception(sprintf("'%s' does not have property '%s'", get_class($this), $offset));
        }
        else if ($action == 'set')
        {
            if ($this->offsetExists($offset))
                return $this->{$offset} = $arguments[0];
            else
                throw new Zend_Exception(sprintf("'%s' does not have property '%s'", get_class($this), $offset));
        }
        else
            throw new Zend_Exception(sprintf("'%s' does not have method '%s'", get_class($this), $method));
    }
}

The SQL that the find produces:

SELECT t0.id AS id1, 
t0.name AS name2, 
t0.password AS password3, 
t0.email AS email4, 
t0.first_name AS first_name5, 
t0.last_name AS last_name6, 
t0.password_reset AS password_reset7, 
t0.salt AS salt8, 
t0.active AS active9, 
t0.cookie_hash AS cookie_hash10, 
t0.company_id AS company_id11, 
t0.privilege_id AS privilege_id12 
FROM user t0 WHERE t0.id = ?

Anyone see anything wrong or know where to go further with this?

Using Doctrine 2.2.2.

The explain I get when I run that query with phpmyadmin: https://i.sstatic.net/4XcTh.png

The table schema: https://i.sstatic.net/7ZZ85.jpg

Upvotes: 2

Views: 2218

Answers (1)

nwalke
nwalke

Reputation: 3209

I believe the problem with my setup was the actual number of lines in the file. Doctrine was reading through those every time. I enabled APC for the meta-cache and load time decreased dramatically after the first load. Without query or result cache, that query ACTUALLY only takes about 6 MS which is what I was aiming for all along. Wish I would have tried that sooner.

Upvotes: 1

Related Questions