2012-05-04 45 views
2

我想弄清楚爲什麼我的一個學說發現運行得如此緩慢。我不知道從哪裏開始,所以請耐心等待。緩慢教條尋找

我做了一個非常基本的查找來獲取用戶對象。這個發現是~166毫秒。當我通過phpmyadmin運行查詢時,需要.7ms。

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

我已經嘗試添加skip-name-resolve到mysql的my.cnf。用戶表中的id字段被編入索引。我真的不知道還有什麼要嘗試。如果有更多我能提供的信息,請告訴我。

下面是實體文件:

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; 
} 

摘要實體:

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)); 
    } 
} 

的發現產生的SQL:

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 = ? 

任何人看到任何錯誤或不知道去哪裏進一步與此?

使用原則2.2.2。

的解釋,我得到當我運行該查詢與phpMyAdmin:http://i.imgur.com/BQsRX.jpg

+3

如果我是你,我將啓用查詢日誌記錄,利用它可以查詢查詢日誌文件並查看正在執行的確切查詢。也許在你不知道的幕後發生了一些事情。你沒有標記RDBMS,但我假設MySQL?如果是這樣,您可以使用此處列出的步驟啓用查詢日誌記錄:http://melikedev.com/2012/03/22/mysql-enable-query-logging/。一旦找到查詢,我會從MySQL命令行運行它,查看是否存在性能差異,並調整模式以提高性能。 –

+0

原諒我的無知,但我從未設置過任何全局變量。我會在哪裏做ZendFramework應用程序?也似乎像php.net現在正在下降。 – tubaguy50035

+1

您實際上將從MySQL命令行運行這些命令。基本上,我不認爲這是一個代碼問題,導致你的緩慢,我認爲這是數據庫/架構相關。所以我想在拆解你的代碼之前首先將它作爲一個分心符來移除。 –

回答

1

我相信我的設置問題是文件中的行的實際數量:http://i.imgur.com/wWeGO.png

表架構。學說每次都在閱讀這些內容。我爲APC啓用了元高速緩存,加載時間在第一次加載後顯着下降。沒有查詢或結果緩存,該查詢實際上只需要大約6個MS,這正是我一直在追求的目標。希望我會盡早嘗試。

+1

您是否在說您認爲編譯時間期間文件大小造成緩慢? APC是操作碼緩存,只有在文件不需要在後續請求中編譯時纔有用。我懷疑這是你的緩慢的原因,但如果它對你有效... –

+0

我真的不知道它可能是什麼。 – tubaguy50035

+1

如果您有一個開發環境,您可以按照我在評論中提到的那樣查詢查詢日誌,那麼您將看到爲每個請求執行的每個查詢。我懷疑你的直覺可能是正確的,這是造成緩慢的關係之一。 –