什么时候应该使用 ORM 主义,什么时候应该使用 zend-db-table?

发布于 2024-09-03 06:39:34 字数 98 浏览 12 评论 0原文

就项目规模、doctrine 与 zend-db-table 的速度和性能而言,我什么时候应该在 Zend 项目中使用doctrine,什么时候应该使用 zend-db-table?

In terms of project scale, doctrine vs zend-db-table speed and performance, when should I use doctrine inside Zend project, and when zend-db-table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

定格我的天空 2024-09-10 06:39:34

任何 ORM 框架都能为您带来开发效率的好处,而不是运行时效率。在这方面,Doctrine 与 Zend_Db_Table 没有什么不同。

如果您在 Doctrine 和 Zend_Db_Table 之间进行选择,请根据使编写代码更容易或更快的功能进行选择。

在一般情况下,没有任何 ORM 框架可以自动使数据库查询更快。如果您需要高性能数据库查询,您应该学习编写 SQL 查询代码,并设计架构和索引以支持给定您需要运行的查询的性能。

Any ORM framework gives you benefit for development productivity, not runtime efficiency. Doctrine is no different from Zend_Db_Table in this regard.

If you are choosing between Doctrine and Zend_Db_Table, choose based on the features that make it easier or faster to write the code.

No ORM framework can automatically make database queries faster in the general case. If you need high-performance database queries, you should learn to code SQL queries, and design your schema and indexes to support performance given the queries you need to run.

夜还是长夜 2024-09-10 06:39:34

使用你最舒服的任何东西,会让你变得最有效率。您和您的开发人员同事可能是最昂贵的资源,如果需要的话,购买额外的硬件可能比您担心未来可能的性能考虑更便宜。

当然,您仍然需要执行基本的数据库优化,例如创建合理的索引等。但我觉得这些“优化”是不言而喻的。

Use whatever you are most comfortable with and will make you the most efficient. You and your fellow developers are probably the most expensive resource, and it's probably cheaper to buy additional hardware, if needed, than you having to worry about possible future performance considerations.

Of course, you will still need to perform basic database optimizations such as creating sensible indexes etc. But I feel that those "optimizations" go without saying.

花间憩 2024-09-10 06:39:34

如果您有大量 SQL 查询,并且缺乏知识(对缓存或查询优化等一无所知)并且缺乏时间,请使用 Zend_DB,它更快、更容易理解,并且对您来说足够好 - 那个缺乏知识和时间,想要尽可能快。

但如果你想要杀手级的 ORM 学说获胜。但需要更多的时间和精力才能有效利用。

如果你想成为数据库杀手,我们就偏离主题了。这是不同的。它不一定取决于您使用的工具。 (一些 DB 杀手可能使用 PDO 本身和他们自己的模型,谁知道呢?)

if you have a lot of SQL queries, and lack of knowledge (knowing nothing about caching or query optimization, etc.) and lack of time use Zend_DB it is faster and easier to be understood and it is good enough for YOU - the one who is in lack of knowledge and time and want to be as fast as possible.

but if you wanna a killer ORM doctrine wins. but it requires more time and energy to be used efficiently.

and if you wanna be a DB killer, we're gonna go off topic. it is different. and it is not necessarily based on what tools you use. (some DB killers probably use PDO itself and their own models, who knows?)

王权女流氓 2024-09-10 06:39:34

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

}

Doctrine helps you define a better business logic and ORM, but it is an absolute performance killer in term of memory and CPU.
Zend table gateway is 5x times faster than Doctrine. And you can extend Zend table gateway to remove some data type parser and that will give you an improvement of 5x more preserving still the benefit of and simple ORM. Here is my FastTablegateway class:

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文