2010-05-24 83 views

回答

27

任何ORM框架讓你受益發展生產力,不執行效率。在這方面,學說與Zend_Db_Table沒有什麼不同。

如果您在Doctrine和Zend_Db_Table之間進行選擇,請根據使代碼更容易編寫或更快的功能進行選擇。

沒有ORM框架能夠自動進行數據庫查詢在一般情況下快。如果您需要高性能的數據庫查詢,您應該學會編寫SQL查詢,並根據需要運行的查詢設計架構和索引以支持性能。

+1

「沒有ORM框架可以自動創建數據庫」。對不起,我現在沒有這個鏈接,但是我已經看到了使用PHP 5.3的Doctrine 2基準測試,這表明在某些情況下,它比本機PDO運行得更快。我知道,那些基準是邪惡的,但這是不可能的? – takeshin 2010-05-25 07:25:51

+0

非常感謝。 – Yosef 2010-05-25 09:40:29

+8

@takeshin:Doctrine 2使用事務管理和緩存來提高某些場景的性能。這不會使*查詢*運行得更快。 – 2010-05-25 12:41:53

8

使用任何你最舒服,並會令你最有效的。您和您的開發人員可能是最昂貴的資源,如果需要購買額外的硬件,則可能比您不必擔心將來可能出現的性能問題更便宜。

當然,您仍然需要執行基本的數據庫優化,比如創建合理的索引等。但是我覺得那些「優化」不言而喻。

+0

查詢優化的重要考慮因素,如果學說使其自動和Zend-DB不能讓它自動。 謝謝 – Yosef 2010-05-24 21:53:12

2

如果你有很多SQL查詢和缺乏知識(對緩存或查詢優化等一無所知)以及缺乏時間使用Zend_DB,它會更快,更容易被理解,對於你來說它已經足夠好了 - 缺乏知識和時間並希望儘可能快的人。

但是如果你想要一個殺手ORM主義勝利。但它需要更多的時間和精力纔能有效地使用。

如果你想成爲一名數據庫殺手,我們要離開話題。他們有區別。並不一定基於您使用的工具。 (DB一些殺手可能使用PDO本身和自己的車型,誰知道?)

0

原則可以幫助你定義一個更好的商業邏輯和ORM,但它是在內存和CPU長期絕對業績的殺手。 Zend表格網關比Doctrine快5倍。而且你可以擴展Zend表格網關來刪除一些數據類型解析器,這將使你獲得5倍的改進,還能保留簡單的ORM的好處。這裏是我FastTablegateway類:

<?php 
namespace Application\Libraries; 

use Zend\Db\TableGateway\TableGateway; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Db\ResultSet\ResultSetInterface; 
use Zend\Db\Sql\Select; 
use Zend\Db\Sql\Sql; 

class FastTablegateway extends TableGateway{ 

    protected $mysqli_adapter = null; 

    /** 
    * Constructor 
    * 
    * @param string $table 
    * @param AdapterInterface $adapter 
    * @param Feature\AbstractFeature|Feature\FeatureSet|Feature\AbstractFeature[] $features 
    * @param ResultSetInterface $resultSetPrototype 
    * @param Sql $sql 
    * @throws Exception\InvalidArgumentException 
    */ 
    public function __construct($table, AdapterInterface $adapter, $features = null, ResultSetInterface $resultSetPrototype = null, Sql $sql = null, $mysqli = null) 
    { 
     $this->mysqli_adapter = $mysqli; 
     parent::__construct($table, $adapter, $features, $resultSetPrototype, $sql); 
    } 


    protected function executeSelect(Select $select) 
    { 
     $time = time(); 
     $selectState = $select->getRawState(); 
     if ($selectState['table'] != $this->table) { 
      throw new \Exception\RuntimeException('The table name of the provided select object must match that of the table'); 
     } 

     if ($selectState['columns'] == array(Select::SQL_STAR) 
     && $this->columns !== array()) { 
      $select->columns($this->columns); 
     } 

     //apply preSelect features 
     $this->featureSet->apply('preSelect', array($select)); 

     if(!$this->mysqli_adapter){ 
      // prepare and execute 
      $statement = $this->sql->prepareStatementForSqlObject($select); 
      $result = $statement->execute(); 
     }else{ 
      $q = $this->sql->getSqlStringForSqlObject($select); 
      //var_dump($q); 
      $result = $this->mysqli_adapter->query($q); 
      $result = is_object($result) ? $result->fetch_all(MYSQLI_ASSOC) : [] ; 
     } 

     // build result set 
     $resultSet = clone $this->resultSetPrototype; 
     //var_dump(is_object($result) ? $result->num_rows : 'A'); 
     $resultSet->initialize($result); 

     // apply postSelect features 
     //$this->featureSet->apply('postSelect', array($statement, $result, $resultSet)); 

     return $resultSet; 
    } 

} 
相關問題