就项目规模、学说与 zend-db-table 的速度和性能而言,我应该何时在 Zend 项目中使用学说,何时使用 zend-db-table?
4 回答
任何 ORM 框架都会为您带来开发效率,而不是运行时效率。在这方面,Doctrine 与 Zend_Db_Table 没有什么不同。
如果您在 Doctrine 和 Zend_Db_Table 之间进行选择,请根据使编写代码更容易或更快的特性进行选择。
在一般情况下,没有 ORM 框架可以自动使数据库查询更快。如果您需要高性能的数据库查询,您应该学习编写 SQL 查询代码,并设计您的架构和索引以支持您需要运行的查询的性能。
使用你最舒服的任何东西,让你最有效率。您和您的开发人员可能是最昂贵的资源,如果需要,购买额外的硬件可能比您担心未来可能的性能考虑要便宜。
当然,您仍然需要执行基本的数据库优化,例如创建合理的索引等。但我觉得这些“优化”是不言而喻的。
如果您有很多 SQL 查询,并且缺乏知识(对缓存或查询优化等一无所知)并且没有时间使用 Zend_DB,它会更快更容易理解,并且对您来说已经足够了 - 那些缺乏知识和时间,并希望尽可能快。
但如果你想要一个杀手 ORM 学说获胜。但它需要更多的时间和精力才能有效地使用。
如果你想成为 DB 杀手,我们就跑题了。他们有区别。它不一定基于您使用的工具。(一些 DB 杀手可能使用 PDO 本身和他们自己的模型,谁知道?)
Doctrine 可以帮助您定义更好的业务逻辑和 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;
}
}